2_pointers_gdb

2_pointers_gdb - REVIEW OF POINTERS CS252: SYSTEMS...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: REVIEW OF POINTERS CS252: SYSTEMS PROGRAMMING Charles Killian Computer Science Department Purdue University With slide credits to Gustavo Rodriguez-Rivera and others. Memory and Pointers A pointer is a variable that contains an address in memory. In a 64(32) bit architectures, the size of a pointer is 8(4) bytes independent on the type of the pointer. 0 (16EiB-1) 2 64-1 Address space p:20: 12 Char c = A ; //ascii 65 char * p = &c; c:12: 65 Ways to get a pointer value 1. Assign a numerical value into a pointer Char * p = (char *) 0x1800; *p = 5; // Store a 5 in location 0x1800; Note: Assigning a numerical value to a pointer isn't recommended and only left to programmers of OS, kernels, or device drivers Ways to get a pointer value 2. Get memory address from another variable: int *p; int buff[ 30]; p = &buff[1]; *p =78; buff[0]:100: buff[1]:104: buff[29]:216: 220: P: 96: 104 78 Ways to get a pointer value 3. Allocate memory from the heap int *p p = new int; int *q; q = (int*)malloc(sizeof(int)) Ways to get a pointer value 4. You can pass a pointer as a parameter to a function if the function will modify the content of the parameters void swap (int *a, int *b){ int temp; temp=*a; *a=*b; *b=temp; } In main: swap(&x, &y) Common Problems with Pointers When using pointers make sure the pointer is pointing to valid memory before assigning or getting any value from the location String functions do not allocate memory for you: char *s; strcpy(s, "hello"); --> SEGV(uninitialized pointer) The only string function that allocates memory is strdup (it calls malloc of the length of the string and copies it) Printing Pointers It is useful to print pointers for debugging char*i; char buff[10]; printf("ptr=%d\n", &buff[5]) Or In hexadecimal printf("ptr=0x%x\n", &buff[5]) Instead of using printf, I recommend to use fprintf(stderr, ) since stderr is unbuffered and it is guaranteed to be printed on the screen. sizeof() operator in Pointers The size of a pointer is always 4 bytes in a 32 bit architecture independent of the type of the pointer: sizeof(int)==4 bytes sizeof(char)==1 byte sizeof(int*)==4 bytes sizeof(char*)==4 bytes Using Pointers to Optimize Execution Assume the following function that adds the sum of integers in an array using array indexing. int sum(int * array, int n) { int s=0; for(int i=0; i<n; i++) { s+=array[i]; // Equivalent to //*(int*)((char*)array+i*sizeof(int)) } return s; } Using Pointers to Optimize Execution Now the equivalent code using pointers int sum(int* array, int n) { int s=0; int *p=&array[0]; int *pend=&array[n]; while (p < pend) { s+=*p; p++; } return s; } Using Pointers to Optimize Execution When you increment a pointer to integer it will be incremented by 4 units because sizeof(int)==4....
View Full Document

Page1 / 41

2_pointers_gdb - REVIEW OF POINTERS CS252: SYSTEMS...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online