contiguous chunks of memory
play

Contiguous chunks of memory Arrays/strings: pointers and memory - PowerPoint PPT Presentation

Contiguous chunks of memory Arrays/strings: pointers and memory allocation int * a = new int[100]; Why not rely solely on string and vector classes? In C++ allocate using array form of new how are string and vector implemented? 0


  1. Contiguous chunks of memory Arrays/strings: pointers and memory allocation int * a = new int[100]; ● Why not rely solely on string and vector classes? In C++ allocate using array form ● of new ➤ how are string and vector implemented? 0 1 32 33 98 99 int * a = new int[100]; ➤ lower level access can be more efficient (but be leery of double * b = new double[300]; a is a pointer claims that C-style arrays/strings required for efficiency) *a is an int ● new [] returns a pointer to a ➤ real understanding comes when more levels of abstraction a[0] is an int (same as *a) block o f memory are understood a[1] is an int ➤ how big? where? a+1 is a pointer size of chunk can be set at ● ● string and vector classes insulate programmers from a+32 is a pointer runtime, not the case with inadvertent attempts to access memory that’s not accessible *(a+1) is an int (same as a[1]) int a[100]; *(a+99) is an int cin >> howBig; ➤ what is the value of a pointer? int a[howBig]; *(a+100) is trouble ➤ what is a segmentation violation? a+100 is valid for comparison delete [] a; // storage returned ● Duke CPS 108 7.1 Duke CPS 108 7.2 C-style contiguous chunks of memory Address calculations: sizeof(…)? int * a = new int[100]; int * a = (int *) x is a pointer, what is x+33? ● In C, malloc is used to allocate ● malloc(100*sizeof(int)); ➤ a pointer, but where? memory int * a = (int *) 0 1 32 33 98 99 ➤ what does calculation 0 1 32 33 98 99 malloc(100 * sizeof(int)); a[33] is the same as *(a+33) depend on? double * d = (double *) if a is 0x00a0, then a+1 is malloc(200 * sizeof(double)); a is a pointer 0x00a4, a+2 is 0x00a8 result of adding an int to a ● *a is an int (think 160, 164, 168) pointer depends on size of ● malloc must be cast, is NOT type- a[0] is an int (same as *a) object pointed to safe (returns void *) double * d = new double[200]; a[1] is an int a+1 is a pointer ➤ void * is ‘generic’ type, can be ● result of subtracting two cast to any pointer type a+32 is a pointer 0 1 33 199 pointers is an int: *(a+1) is an int (same as a[1]) *(d+33) is the same as d[33] *(a+99) is an int free(d); // return storage ● if d is 0x00b0, then d+1 is (d + 3) - d == _______ *(a+100) is trouble 0x00b8, d+2 is 0x00c0 a+100 is valid for comparison (think 176, 184, 192) 7.3 7.4 Duke CPS 108 Duke CPS 108

  2. More pointer arithmetic What is a C-style string? char * a = new int[44]; address one past the end of an ● array of char terminated by sentinel ‘\0’ char ● char * begin = a; array is ok for pointer char * end = a + 44; ➤ sentinel char facilitates string functions comparison only ➤ ‘\0’ is nul char, unfortunate terminology ➤ how big an array is needed for string “hello”? ● what about *(begin+44)? 0 1 15 16 42 43 while (begin < end) ● a string is a pointer to the first character just as an array is a what does begin++ mean? ● { pointer to the first element *begin = ‘z’; ➤ char * s = new char[6]; begin++; // *begin++ = ‘z’ ● how are pointers compared } using < and using == ? ➤ what is the value of s? of s[0]? ● char * string functions in <cstring> (or <string.h> ) what is value of end - begin? ● Duke CPS 108 7.5 Duke CPS 108 7.6 C style strings/string functions More string functions (from < cstring>) what about relational operators ● ● strlen is the # of characters in a what’s “wrong” with this code? strcpy copies strings ● ● <, ==, etc.? string ➤ who supplies storage? int countQs(char * s) ➤ same as # elements in char can’t overload operators for ● // pre: ‘\0’ terminated ➤ what’s wrong with s = t? array? pointers, no overloaded // post: returns # q’s char s[5]; { operators in C int strlen(char * s) char t[6]; int count=0; // pre: ‘\0’ terminated char * h = “hello”; ● strcmp (also strncmp) // post: returns # chars for(k=0;k < strlen(s);k++) strcpy(s,h); // trouble! { if (s[k]==‘q’) count++; ➤ return 0 if equal strcpy(t,h); // ok int count=0; return count; ➤ return neg if lhs < rhs while (*s++) count++; } char * strcpy(char* t,char* s) return count; //pre: t, target, has space ➤ return pos if lhs > rhs } //post: copies s to t,returns t { if (strcmp(s,t)==0) // equal ● Are these less cryptic? ● how many chars examined for int k=0; if (strcmp(s,t) < 0)// less 10 character string? while (t[k] = s[k]) k++; while (s[count]) count++; if (strcmp(s,t) > 0)// ???? // OR, is this right? return t; solution? ● } char * t = s; while (*t++); strncpy copies n chars (safer?) ● return t-s; 7.7 7.8 Duke CPS 108 Duke CPS 108

  3. Arrays and pointers Microsoft question ● These definitions are related, but not the same ● Write atoi, write itoa, which is harder? int a[100]; int * ap = new int[10]; ● Questions? Issues? Problems? ● both a and ap represent ‘arrays’, but ap is an lvalue int atoi(const char * sp); char * itoa(int num); ● arrays converted to pointers for function calls: char s[] = “hello”; // prototype: int strlen(char * sp); ● Difference between const char * p and char * const p cout << strlen(s) << endl; ➤ one is a pointer to a constant character ● multidimensional arrays and arrays of arrays int a[20][5]; ➤ one is a constant pointer to a character int * b[10]; for(k=0; k < 10; k++) b[k] = new int[30]; Duke CPS 108 7.9 Duke CPS 108 7.10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend