200 likes | 291 Views
EEE 243B Applied Computer Programming. Memory Model for C programs Pointer Arithmetic §10.2, 10.4. Review. What will this print? #include < stdio.h > void main (void) { int a[5]={22, 4, 61, 8, 2}; int * p; int * r; p = &a[2]; r = &a[4]; if (*p > *r) printf (“Twiddle- dee ”);
E N D
EEE 243BApplied Computer Programming Memory Model for C programs Pointer Arithmetic §10.2, 10.4
Review • What will this print? • #include <stdio.h> • void main (void) • { • int a[5]={22, 4, 61, 8, 2}; • int* p; • int* r; • p = &a[2]; • r = &a[4]; • if (*p > *r) • printf(“Twiddle-dee”); • else • printf(“Twiddle-dum”); • } Prof S.P. Leblanc
Outline • Memory model for a program in C • Pointer Arithmetic Prof S.P. Leblanc
1. Memory model • A program in C has four main segments: • Code segment • Your program • Data • Static/global data • Heap • Dynamic memory (we will discuss next class) • Stack • Automatic segment • Stores local function variables (for main() and others) Prof S.P. Leblanc
Memory model Stack } Empty space: the final frontier Heap Data Code Prof S.P. Leblanc
Dude… Where’s my Var? Memory model Prof S.P. Leblanc
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 Data Code Prof S.P. Leblanc
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 Data Code Prof S.P. Leblanc
Stack Memory model • Dude…Where's my mem? void main (void) { int* iPtr; … iPtr = (int*)malloc(10 * sizeof(int)); } Heap Data Code Prof S.P. Leblanc
Example #include <stdio.h> intmyGlobal=0; void main(void) { char localString[]="TEST"; int* pInt; static inti=100; printf("A stack address: %d\n", localString); printf("Another stack address: %d\n", &pInt); printf("A text address: %d\n", "Hello"); printf("A static (data) address: %d\n", &i); printf("A global (data) address: %d\n", &myGlobal); } Prof S.P. Leblanc
Pointer arithmetic • You have already seen or even used at least one pointer arithmetic operation INT16U* pInt= NULL; … pInt++; //Move my pointer ahead by one • Ahead by one, but by one what? Prof S.P. Leblanc
Pointer arithmetic • Pointers have types because they point to a type. This is important when we do pointer arithmetic: INT16U aOfInts[5] = {1,2,3,4,5}; INT16U* pInt= aOfInts; pInt++; //This moves the pointer //ahead by two bytes aOfInts • Each square is a byte • in memory. @init After pInt++ Where is pInt Pointing? Prof S.P. Leblanc
Pointer arithmetic • And: char aOfChars[5] = {'a', 'b', 'c', 'd', 'e'}; char* pChar= aOfChars; … pChar++; //This moves the ptr ahead one byte aOfChars a b c d e • Each square is a byte • in memory. After pInt++ @init Where is pChar Pointing? Prof S.P. Leblanc
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 Prof S.P. Leblanc
Pointer arithmetic • Only a few arithmetic operations can be performed on pointers: int* pInt; • Prefix: ++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) Prof S.P. Leblanc
Pointer arithmetic • You cannot add, multiply or divide two pointers • What good could come out of it? • Pointedly, something that is pointless. Prof S.P. Leblanc
Quiz Time • What is the difference between the heap and the stack for a program? • Where do these variables live? inti = 0; //Where? void main (void) { int j = 0; //Where? } intFctn () { static int k = 0; //Where? int l = 0; //Where? } Prof S.P. Leblanc
Quiz Time • Draw where the pointers are in the array: • intaOfInts[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]? Prof S.P. Leblanc
Solution #include <stdio.h> #include <stdlib.h> intmain(int argc, char *argv[]) { intaOfInts[5] = {1,2,3,4,5}; int* pInt= aOfInts; int* pInt2 = NULL; pInt = pInt + 4; pInt2 = pInt--; aOfInts[0] = pInt - pInt2; printf("aOfInts[0] = pInt - pInt2; results in %d\n",aOfInts[0]); system("PAUSE"); return 0; } Prof S.P. Leblanc
Next Lecture • Dynamic Memory Allocation Prof S.P. Leblanc