1 / 20

EEE 243B Applied Computer Programming

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 ”);

zanta
Télécharger la présentation

EEE 243B Applied Computer Programming

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. EEE 243BApplied Computer Programming Memory Model for C programs Pointer Arithmetic §10.2, 10.4

  2. 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

  3. Outline • Memory model for a program in C • Pointer Arithmetic Prof S.P. Leblanc

  4. 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

  5. Memory model Stack } Empty space: the final frontier Heap Data Code Prof S.P. Leblanc

  6. Dude… Where’s my Var? Memory model Prof S.P. Leblanc

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Next Lecture • Dynamic Memory Allocation Prof S.P. Leblanc

More Related