Common Problems with Pointers

Common Problems with Pointers - Execution • Assume the...

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

View Full Document Right Arrow Icon
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)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Printing Pointers It is useful to print pointers for debugging char*i; char buff[10]; printf("ptr=%d\n", &buff[5]) Or In hexadecimal Instead of using printf, I recommend to use fprintf(stderr, …) since stderr is unbuffered and it is guaranteed to be printed on the screen.
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Using Pointers to Optimize
Background image of page 4
Background image of page 5

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

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

Unformatted text preview: 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)) } 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++; } Using Pointers to Optimize Execution • When you increment a pointer to integer it will be incremented by 4 units because sizeof(int)==4. • Using pointers is more efficient because no indexing is required and indexing require multiplication. • Note: An optimizer may substitute the multiplication by a “<<“ operator if the size is a power of two. However, the array entries may not be a power of 2 and integer multiplication may be needed....
View Full Document

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue.

Page1 / 6

Common Problems with Pointers - Execution • Assume the...

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

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