170 likes | 292 Vues
This guide explores the core concepts of memory models in C programming, including the roles of the stack, heap, and code segments. It delves into pointer arithmetic, demonstrating how to manipulate pointers effectively. Key topics include dereferencing pointers, accessing structure fields, and the significance of static variables in functions. Through practical examples and quizzes, including the differences between heap and stack memory, this resource is perfect for both beginners and experienced programmers looking to reinforce their understanding of memory management in C.
E N D
EEE 243BApplied Computer Programming Memory model for C programs Pointer arithmetic
Review • How do we access the fields in a structure? • If I have a pointer what operator do I use to dereference and access a field at the same time? • If I declare a variable as static inside a function what does that do for me? Maj JGA Beaulieu & Capt MWP LeSauvage
Outline • Memory model for a program in C • Pointer arithmetic Maj JGA Beaulieu & Capt MWP LeSauvage
Memory model • A program in C has three main segments: • Code segment (your program) • Heap (AKA data segment) • Stack (automatic segment) Maj JGA Beaulieu & Capt MWP LeSauvage
Memory model Stack } Empty space; the final frontier Heap Code Maj JGA Beaulieu & Capt MWP LeSauvage
Dude… Where’s my Var? Memory model Maj JGA Beaulieu & Capt MWP LeSauvage
Memory model • Dude…Where is my var? int i = 0; void main (void) { int j = 0; } int Fctn () { int l = 0; static int k = 0; } Stack Heap Code Maj JGA Beaulieu & Capt MWP LeSauvage
Memory model • Dude…Where is my var? int i = 0; void main (void) { int j = 0; j = j + 5; } int Fctn () { int l = 0; static int k = 0; k = k +10; } Stack Heap Code Maj JGA Beaulieu & Capt MWP LeSauvage
Memory model • Dude…Where is my mem? void main (void) { int *iPtr; … iPtr = (int *)malloc(10 * sizeof(int)); } Stack Heap Code Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • You have already seen or even used at least one pointer arithmetic operation int *pInt = NULL; … pInt++; //Move my pointer ahead by one • Ahead by one, but by one what? Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • Pointers have types because they point to a type. This is important when we do pointer arithmetic: int aOfInts[5] = {1,2,3,4,5}; int *pInt = aOfInts; … pInt++; //This moves the pointer ahead //by two bytes • Each square is a byte • in memory. @init After pInt++ Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • And: char aOfChars[3] = {'a', 'b', 'c', 'd', 'e'}; char *pChar = aOfChars; … pChar++; //This moves the pointer // ahead by one byte • Each square is a byte • in memory. @init After pChar++ Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • The same goes for every other pointer type! • If you are pointing to the STUDENT type-defined structure: STUDENT *ptrToStu = aMilColStudent; … ptrToStu++; //This moves the pointer //ahead by sizeof(STUDENT) //bytes Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • There are only a few arithmetic operations that you can do on pointers: int *pInt; • Unary: ++pInt, --pInt • Postfix: pInt++, pInt-- • Adding an index (an int) to a pointer: pInt + 5 (advances by 5 positions) • Subtracting an index: pInt – 5 • Subtracting pointers: pInt1 – pInt2 • Gives the number of positions between two pointers – useful to calculate offsets in arrays (distance between elements) Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer arithmetic • You cannot add, multiply or divide two pointers (it makes no point to it ): • What good could come out of it? • Pointedly, something that is pointless. Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time • What is the difference between the heap and the stack for a program? • Given the program answer stack or heap: //A program int i = 0; //Where? void main (void) { int j = 0; //Where? } int Fctn () { static int k = 0;//Where? int l = 0; //Where? } Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time • Draw where the pointers are in the array: int aOfInts[5] = {1,2,3,4,5}; int *pInt = aOfInts; Int *pInt2 = NULL; pInt = pInt + 4; pInt2 = pInt--; aOfInts[0] = pInt – pInt2; //What is in //array aOfInts[0]? Maj JGA Beaulieu & Capt MWP LeSauvage