1 / 17

EEE 243B Applied Computer Programming

EEE 243B Applied 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?

phiala
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

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

  3. Outline • Memory model for a program in C • Pointer arithmetic Maj JGA Beaulieu & Capt MWP LeSauvage

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

  5. Memory model Stack } Empty space; the final frontier Heap Code Maj JGA Beaulieu & Capt MWP LeSauvage

  6. Dude… Where’s my Var? Memory model Maj JGA Beaulieu & Capt MWP LeSauvage

  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 Code Maj JGA Beaulieu & Capt MWP LeSauvage

  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 Code Maj JGA Beaulieu & Capt MWP LeSauvage

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

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

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

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

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

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

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

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

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

More Related