1 / 18

Variables and Objects, pointers and addresses:

Learn about variables, data containers, memory overflow, data types, pointers, memory addressing, and more in C/C++. Explore the fundamental concepts and best practices for handling data effectively.

rhoades
Download Presentation

Variables and Objects, pointers and addresses:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 01000001 01000001 01000100 Variables and Objects, pointers and addresses: • 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 'A' 16916 Chapter 3, Slide 1

  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; Chapter 3, Slide 2

  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). Chapter 3, Slide 3

  4. 01000001 010001… 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 Chapter 3, Slide 4

  5. …010001 01000001 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 1 00000000 Chapter 3, Slide 5

  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; Chapter 3, Slide 6

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

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

  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. Memory addressing: byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 Pointers: a pointer value is in essence an address, a pointer variable is in essence a data container to hold an address. 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”: But how a pointer can “know” what is at a particular address? Chapter 3, Slide 11

  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. Chapter 3, Slide 12

  13. Chapter 3, Slide 13

  14. Chapter 3, Slide 14

  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) 00000000 00000001 while in a little endian machine a short with value 1 looks like this: (Intel, most communication hardware) 00000001 00000000 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. Chapter 3, Slide 15

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

  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; Chapter 3, Slide 17

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

More Related