Unformatted text preview:

CS262 Lecture 04Chapter 5 PointersZoran DuricDepartment of Computer ScienceThe Anatomy of C Memory• application's address space– read only address– read/write address– aligned address •multi-byte types– physical address2• unix like systemsThe Anatomy of C Memory• application's address space– read only address– read/write address– aligned address •multi-byte types– physical address3• win32 systemsVariables in Memory Map// fixed address: visible to other filesint global_static;// fixed address: only visible within filestatic int file_static;// parameters always stackedint foo(int auto_param){// fixed address: only visible to functionstatic int func_static;// stacked: only visible to functionint auto_i, auto_a[10];// array explicitly allocated on heapdouble *auto_d = malloc(sizeof(double)*5);// return value in register or stackedreturn auto_i;}4Pointer• A pointer stores an address in application's address space• read “pointer” as “an address pointing to”• int i=5;• int * p= & i;5Pointer6aligned memory addressprintf(“%p”,p)Declare a Pointer• examples– int *i, j; // i is a pointer but j is just int– int i, *j; // j is a pointer but i is not– int *i, *j; // both i and j are pointers– int ** i = &j; // what is this?– void * x=i;7Dereferencing• int i=5;• int * p=&i;• p=10; // this means p has address 10• *p=10; // this changes the value at address p8Dereferencing• see swap.c9const1.const int * p; //a pointer to const int•*p=0; //wrong•p=&i; p=&j; //OK2.int const * p; //same as above3.int * const p; //a const pointer to int•*p=0; //OK•p=&i; //wrong 10Pointers and Array• char A[]=”GMU”; //A[0]=’g’ is allowed• char * A=”GMU”; //ok, but A[0]=’g’ will crash• char * p=A; //array name is the pointer of its first element• p=&A[0]; //same as above• p=&A; //same as above11Pointers and Array• char a_message[] = "now is the time"; /* an array */• char * p_message = "now is the time"; /* a pointer*/12Multi-Dimensional Array• /* strcpy: copy t to s; array subscript version */ • void strcpy(char *s, char *t) • {‣int i= 0; ‣while ((s[i] = t[i]) != ’\0’) i++;• }13Pointers and Array• /* strcpy: copy t to s; pointer version */ • void strcpy(char *s, char *t) • {‣while ((*s = *t) != ’\0’) ‣{‣s++; t++;‣}• }14Pointers and Array• void strcpy(char *s, char *t) { while( *s++ = (*t)++) ; }15Multi-Dimensional Array• char * B[]={“Hello”,”World”}; //array of char *• char C[2][3]; //array of char with 6 elements• (char *) * p=B; //OK• char ** p=C; //wrong• char * p=&C[0][0]; //OK• char * p=C; //same as above• void bar(char * foo[]){...}• bar(B); //OK• bar(C); //wrong!, void bar(char foo[][3]) or bar(char *);• char * D[]=B; //wrong, char * D[]={“A”,”B”,”C”} or char ** D=B16Multi-Dimensional Array• char * name [] = {“Illegal month”, “Jan”, “Feb”, Mar”};• char name[][15]= {“Illegal month”, “Jan”, “Feb”, Mar”};•17name[0][1]=’c’;Multi-Dimensional Array• int void main(int argc, char ** argv)– >echo hello world (run command echo)18Multi-Dimensional Array• Practice: write a program to do this:19Pointer and Array• There are differences between A (array) and p (pointer)– you can’t assign values to A but can do so to p• A=p; //wrong– sizeof(A) gives you the size of the entire array– sizeof(p) gives you the size of a pointer– p++ is allowed, but A++ is not20Pointer and Array• char A[]=”GMU”;• char * p=&A[1];• printf(“%c”,p[-1]);• what has been printed? G21Operation Cost1. Integer arithmetic2. Pointer access3. Simple conditionals and loops4. Static and automatic variable access5. Array access6. Floating-point with hardware support7. Switch statements8. Function calls9. Floating-point emulation in software10. Malloc() and free()11. Library functions (sin, log, printf, etc.)12. Operating system calls (open, brk, etc.)22more costlyless


View Full Document

MASON CS 262 - Chapter 5 Pointers

Download Chapter 5 Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 5 Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 5 Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?