Data Representation and Storage. Some definitions (in C)

Similar documents
Data Representation and Storage

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

We would like to find the value of the right-hand side of this equation. We know that

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS429: Computer Organization and Architecture

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Midterm CSE 131B Spring 2005

Character Strings. String-copy Example

Section Notes - Week 1 (9/17)

Computer Systems Programming. Practice Midterm. Name:

CS C Primer. Tyler Szepesi. January 16, 2013

Dynamic Memory: Alignment and Fragmentation

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

More about BOOLEAN issues

First of all, it is a variable, just like other variables you studied

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

INTRODUCTION 1 AND REVIEW

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS24 Week 2 Lecture 1

ECE 15B COMPUTER ORGANIZATION

Pointers and Arrays 1

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Type Checking. Prof. James L. Frankel Harvard University

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Computer Systems Principles. C Pointers

17. Instruction Sets: Characteristics and Functions

Work relative to other classes

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Computer Organization & Systems Exam I Example Questions

Kurt Schmidt. October 30, 2018

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

Data Types. Data Types. Integer Types. Signed Integers

Intermediate Programming, Spring 2017*

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

EL2310 Scientific Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS240: Programming in C

Structures and Pointers

Do not start the test until instructed to do so!

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Final CSE 131B Spring 2004

Survey. Motivation 29.5 / 40 class is required

CS61C : Machine Structures

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Dynamic Memory Allocation and Command-line Arguments

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CS429: Computer Organization and Architecture

Midterm Exam Review Slides

Advanced Systems Programming

Chapter 3. Fundamental Data Types

Heap Arrays. Steven R. Bagley

5.Coding for 64-Bit Programs

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

A Fast Review of C Essentials Part I

C++ Structures Programming Workshop 2 (CSCI 1061U)

Week 3 Lecture 2. Types Constants and Variables

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Data Type Fall 2014 Jinkyu Jeong

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Dynamic Data Structures. CSCI 112: Programming in C

ME964 High Performance Computing for Engineering Applications

CS Programming In C

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

EL6483: Brief Overview of C Programming Language

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

CSE 230 Intermediate Programming in C and C++

a data type is Types

CSCI2467: Systems Programming Concepts

12 CREATING NEW TYPES

Arrays and Memory Management

Types, Variables, and Constants

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Data Types and Variables in C language

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

ECE264 Fall 2013 Exam 3, November 20, 2013

Operators in C. Staff Incharge: S.Sasirekha

High Performance Computing in C and C++

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CS61C : Machine Structures

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

THE FUNDAMENTAL DATA TYPES

Final CSE 131B Spring 2005

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Transcription:

Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain how C data types arranged in memory. Sizes Alignment Endianness 9/6/2016 CS61 Fall 2016 1 Some definitions (in C) Object: A region of storage Distinct objects never overlap Objects may have multiple names Aliases: Multiple names for the same object Different pointers to the same object are called aliases of each other. Example: int foo; int *name1, *name2; name1 = &foo; name2 = &foo; 9/6/2016 CS61 Fall 2016 2 1

More Definitions Definition: Allocates an object and creates a name for it. Examples: int foo; char bar; float baz; Declaration: Alerting the compiler that there exists an object of some name/type, but does not necessarily allocate the space for it. Example: extern int errno; int func(void); 9/6/2016 CS61 Fall 2016 3 Object Sizes Every object in C has a size. You can get the size of an object using sizeof Examples: printf( An integer has size %zu\n, sizeof(int)); int x; printf( An integer has size %zu\n, sizeof(x)); sizeof returns a value of type size_t The size of an object determines the values it can hold. Example: A char is 8 bits the maximum value it can contain is 255. Why? 9/6/2016 CS61 Fall 2016 4 2

Fundamental Types C has a set of built-in or fundamental types: int, unsigned int signed and unsigned integers (4 bytes) long, unsigned long signed and unsigned longs (8 bytes) short, unsigned short signed short integer (2 bytes) char character (1 byte) float, double, long double usually, 4 bytes, 8 bytes, and 16 bytes respectively These are all sizes on x86-64; some of them differ for x86-32 9/6/2016 CS61 Fall 2016 5 Screen Capture Run the vars1 and vars2 programs notice a couple of things: We have some BIG hex numbers! (If you are not comfortable with hex, check out the short video that goes over binary and hex.) When we run vars1 (which prints out the addresses of shorts), we see that the addresses are all even. When we run vars2 (which prints out the addresses of ints), we see that the addresses are all multiples of 4. 9/6/2016 CS61 Fall 2016 6 3

Binary and Hex in One Slide Data in a computer are stored in bits (binary digits). A bit can be either 0 or 1. Larger data types (e.g., char, int, short, etc) can be expressed as a string of binary digits, where each bit is a different power of two: 101011 = 2 5 + 2 3 + 2 1 + 2 0 = 32 + 8 + 2 + 1 = 43 This can be tedious with 64 bit numbers Hexadecimal represents data in base 16 1 hex digit = 4 bits (4 bits can represent up to 16 values) An int (4 bytes) can be expressed with 8 hex digits: 0x00001A2B = 1 * 16 3 + A * 16 2 + 2 * 16 1 + B * 16 0 = 4096 + 10 * 256 + 2 * 16 + 11 = 4096 + 2560 + 32 + 11 = 6699 9/6/2016 CS61 Fall 2016 7 Screen Capture We ran vars3 and vars4 and noticed that even though we tossed some char variables in the mix, the addresses of the shorts were still even and the addresses of the ints were still divisible by 4. Why is this??? 9/6/2016 CS61 Fall 2016 8 4

Alignment C has strict rules about where variables can be placed. The alignment, A, of a type, T, is a size such that the address of every object of type T is a multiple of A. Shorts will always have even addresses (A = 2) Ints will always have addresses that are a multiple of 4. Longs will have addresses that are a multiple of?. Sizeof(T) is always a multiple of alignof(t) Alignment of a struct is the maximum alignment of all the components in the struct. 9/6/2016 CS61 Fall 2016 9 Structs Comparable to records in other languages. Similar to the data part of classes. Lets you group together a set of objects that you want associated with one another. Example declaration: struct point { int x; int y; int z; }; Example definition: struct point p; struct student{ char name[22]; unsigned int age; char house[15]; }; struct student eddie; 9/6/2016 CS61 Fall 2016 10 5

Derived Types These are types that you (the programmer) build from the fundamental types (or from other derived types). There are three primary derived types: Arrays: a collection of contiguously allocated objects of the same type. Structs: a collection of fields Unions: a way to store different data types in the same memory location. 9/6/2016 CS61 Fall 2016 11 More Structs You can combine declaration and definition: struct point { int x; int y; int z; }p; struct student{ char name[22]; unsigned int age; char house[15]; } eddie; Frequently, we create typedefs for structures: typedef struct { int x; int y; int z; } point; point p; typedef struct { char name[22]; unsigned int age; char house[15]; } student; student eddie; 9/6/2016 CS61 Fall 2016 12 6

Arrays Defined using [] char carray[10]; // an array of 10 characters int iarrary[52]; // an array of 52 integers Array elements are laid out contiguously in memory. All the elements of the array are the same type. Elements accessed by index: carray[3] = a ; iarray[51] = 1234; 9/6/2016 CS61 Fall 2016 13 Alignment, Arrays & Pointer Arithmetic (Oh my) Pointers, types, and arrays in C are kind of magical! Key concept: Given a pointer, P, to something of type T, P + i is identical to &P[i]. Corollary: if P is a pointer, &P[0] == P Example: int *ip = malloc(10 * sizeof(int)); ip == &ip[0]; &ip[4] == ip + 4; ip[6] == *(ip + 6); 9/6/2016 CS61 Fall 2016 14 7

More Pointer Magic A pointer is an address. Let s say that P is a pointer and its value (address) is 0x601060. If P is of type int * (pointer to an integer), then: &P[0] = 0x601060 &P[1] = 0x601064 P + 1 = 0x601064 9/6/2016 CS61 Fall 2016 15 From Thursday s Survey How Challenging was the Treasure Hunt? Comfort with C About right Easy Neutral Challenging 9/6/2016 CS61 Fall 2016 16 8