Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.

Similar presentations


Presentation on theme: "Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is."— Presentation transcript:

1 Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is the code stored in the container to evaluate a variable is to fetch the code from the container and interpret it properly to store a value in a variable is to code the value and store the code in the container size of a variable is the size of its container 01000001 01000100 'A''A' 16916

2 Chapter 3, Slide 2 overflow = a code longer than the size of the container is being stored at a data container: Problems with overflows in run-time: (1) if the whole part X of the memory belongs to the running program, then (a) if X does not contain any data important for the rest of the execution of the program, then the program runs fine and there is no apparent problem;

3 Chapter 3, Slide 3 (b) if X does contain important data which get overridden by the 100101100000001011010100 tail of the binary code, but by pure chance, it does not change anything (as the data stored therein just happened to be the same), then the program runs fine and there is no apparent problem; (c) if X does contain important data which get overridden and thus changed, then (i) incorrect results may be produced or (ii) the program may crash with all kinds of possible error messages. (2) if all or part of X belongs to some other process, then the program is terminated by the operating system for a memory access violation (the infamous UNIX segmentation fault error).

4 Chapter 3, Slide 4 01000001010001… C/c++ Compilers take care of “right overflow” for variables by truncating the code (of course warnings should be produced by the compiler!): char i;.. i = 123456789; printf("%d\n",i); displays 21 on the screen

5 01000001…010001 C/C++ compilers take care of “left overflow” for variables by truncating the code char i;.. i = 255;.. i++; printf("%d\n",i); displays 0 on the screen 00000000 1 Chapter 3, Slide 5

6 Chapter 3, Slide 6 innate data types: char, unsigned char --- 1 byte short, unsigned short --- 2 bytes int, unsigned int --- 4 bytes long, unsigned long --- 4 bytes float --- 4 bytes double --- 8 bytes only char and unsigned char do not depend on the platform. The values shown are typical for a 32-bit architecture. The size of a variable (or value) can be calculated (in compile-time) by the operator sizeof exp Complex “data containers” - structures, records struct { char a; int b; } x;

7 Chapter 3, Slide 7 memory of a structure is contiguous! However the “placement” may differ: improper placement (from the access point of view):

8 Chapter 3, Slide 8 A proper placement (from the access point of view):

9 Who creates the padding? The compiler! Who knows the size of a structure? The compiler? So, if we need to know the actual size of a structure, we use the sizeof operator! From data point of view, objects are like structures and classes are like struct constructs. Pointers: a pointer value is in essence an address, a pointer variable is in essence a data container to hold an address. byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 Memory addressing: Chapter 3, Slide 9

10 However, pointers know “data type” of whatever they reference, with the exception of void* which is just a plain address. A pointer “knows” what kind of object is at the end of the arrow! Chapter 3, Slide 10

11 Nameless “data containers”: Chapter 3, Slide 11 But how a pointer can “know” what is at a particular address?

12 Chapter 3, Slide 12 What is stored in the four bytes at addresses 802340.. 802343 ? (a) four characters 'A' 'B' 'C' 'D' (b) two shorts 16961, 17475 (c) long 1145258561 (d) float 781.035217 (e) nobody can tell Of course, (e) is the right answer.

13 Chapter 3, Slide 13

14 Chapter 3, Slide 14

15 Chapter 3, Slide 15 For the actual values of anything but char’s, the byte order is important: on a big endian machine a short with value 1 looks like this: (Mac, JVM, TCP/IP NBO) 0000000000000001 while in a little endian machine a short with value 1 looks like this: (Intel, most communication hardware) 0000000100000000 Normally (with variables), we do not have to worry about it, the compiler knows it, and thus storing and fetching of values is done appropriately. We need to be aware of it when “messing up” with pointers and storing/fetching values through indirection.

16 Chapter 3, Slide 16 int AmBigEndian() { long x = 1; return !(*((char *)(&x))); } 0000000000000001  returns 1 on a big endian machine 00000000  returns 0 on a little endian machine 00000000 0000000100000000

17 Chapter 3, Slide 17 Setting pointers: (1) through dynamic allocation ( malloc, new ) (2) through the address operator & (3) through calculation -- e.g. traversing linked data structures (4) through assignment with a special value -- e.g. memory mapped I/O’s etc. Indirection operator * *p means “evaluate the data container p points to” or if on the left-hand side of an assignment statement (as an l-value), it means “store at the data container p points to”. x = x + *p; *p = x;

18 Chapter 3, Slide 18 End of slides for chapter 3


Download ppt "Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is."

Similar presentations


Ads by Google