Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March 2014 1 Structures in C This handout is supposed to provide you with a template of how you can visually understand memory assignments done by a struct in C as it was introduced in the lecture. Let s deal with the following two definitions of the structs student and fraction : 1 /* Definition of the structs student and fraction as user - defined data type */ 2 3 typedef struct { 4 char * name ; 5 char suid [8]; 6 int numc ; 7 } student ; 8 9 typedef struct { 10 int nun ; 11 int denom ; 12 } fraction ; By using the very useful command typedef in C you can declare your own data types. In this case you declare two new data types student and fraction which are the structures as seen above. The obvious advantage of this is that now you can declare variables of the type fraction or student anywhere in your code, as if it was a normal standard basic datatype such as e.g. int, i.e. in your code you can now type something like, e.g. 1 /* Sample use of the newly defined struct data types in the code above */ 2 3 fraction bigfraction ; 4 fraction superfraction ; 5 student gradstudent ; 6 fraction * pointtoafraction ; 7 student * pointtoastudent = & gradstudent ; 1
The new variables bigfraction and superfraction in this code snippet are of type fraction which means that they are structures with memory allocated for two integers (i.e. 8 bytes) in the memory stack of the function in which these structs are defined. The new variable gradstudent is of type student, which in turn is a struct that allows memory space for a char-pointer (of type char *), a char-field with 8 entries and an integer (16 bytes altogether). pointtoafraction is declared as a pointer of type fraction (and fraction itself is of type struct with space for two integers), so this pointer now points to the base address of some reserved space (8 bytes) in the memory stack of the function from within this command is issued. The next command declares the pointer pointtoastudent and then initializes it with the address (indicated by the operator &) of struct gradstudent; so now pointtoafraction points to the base address of struct gradstudent. Note that pointtoafraction is still not initialized, not even with NULL, so it is pointing to just some undefined space in memory. This is all right, as long as you don t start de-referencing this pointer and thus overwriting the memory content that happens to be at the address to which pointtoafraction happens to point at. The members of the structs can then be accessed by the usual. and -> operators like this: 1 bigfraction. denom = 34; 2 superfraction. num = 16; 3 pointtoafraction -> denom = 45; /* Because this is a pointer, it has to be \ 4 de - referenced if you want to write something \ 5 in this memory location ; and with structs you do \ 6 this with the -> Operator. 7 */ 8 pointtoastudent -> numc = 4; If you have, for example, defined somewhere else in your code several student name strings (strings are simply fields of characters) in the following way: 1 char student1 [12] = Johnny Depp ; /* Initialized at the definition */ 2 char student2 [17] = Albert Einstein! ; /* Initialized at the definition */ and also: 1 char student3 [17]; /* NOT Initialized at the definition - This means, that later on, \ 2 an assignment like student3 = Some Name is NOT ALLOWED! \ 3 Instead, you have to use the string functions that are \ 4 provided in < string.h> for the handling of strings in C! 5 */ you could then do the following in your code: 1 strcpy ( student3, student2 ); /* Now, student3 is Albert Einstein */ 2 strcpy ( student3, New grad student ); /* Now student3 has been changed */ 3 strcpy ( pointtoastudent - >name, student3 ); /* Now the char pointer of the variable \ 4 pointtoastudent points to the base \ 5 address of student3. 6 */ 2
Hence, the command printf( Student Name: %s %p\n,pointtoastudent->name,pointtoastudent.name); prints out New grad student and the address (with control sign %p) of the memory location to which pointtoastudent.name points to. On my system for example, the printed address by the above printf statement is: 0x7fff57ed9f70 (Try it out on your own system!). 0x in C denotes a memory address, which is always provided as a hexadecimal number. Note the difference in accessing the member and the address of the variable gradstudent here: You need the operator -> for de-referencing the address of the pointer pointtoastudent.name, i.e. to actually access the content of memory at this address. 1.1 Further Remarks By using the backslash \ you can extend C-style comments to several lines without having to close the comment with */. This is often done in the way you see it in the last code snippet above at the end of page 2. As a general rule, you should always comment your code and explain what you are doing this is just good coding practices. The operator = allows you to have spaces between = and the operands it works on, so the following statements are all OK as long as they are in the same command line (i.e. as long as you haven t hit the return key and thus issued a newline command which will be noticed by the compiler): 1 variable =5; 2 variable = 17; 3 variable1 = 1890.56; 4 var = 15; Usually, you use this freedom in writing C-code for aligning all the operators in consecutive lines which makes the code more readable (also good coding practices!) as you can see in the various code snippets in this handout. 2 Structs in Memory Now, let s execute the following 7 commands and draw consecutive pictures of what each one of these commands means in terms of memory which is allocated in the function s stack in which the structure student from page 1 resides. To make it easier for you to follow the consecutive commands I always repeat the definition of the struct student. 3
2.1 Execution of First Command 2.2 Execution of Second Command 4
2.3 Execution of Third Command 2.4 Execution of Fourth Command 5
2.5 Execution of Fifth Command 2.6 Execution of Sixth Command 6
2.7 Execution of Seventh Command Don t let yourself be fooled by the crazy *(char ***) in the last assignment; this is just a reinterpretation (i.e. a cast) of what stands right of it. It is re-interpreted to now de-reference a pointerto-a-pointer-to-a-pointer to a character. If you have really understood the memory pictures including this one: Congratulations! 7