Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.

Similar presentations


Presentation on theme: "Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly."— Presentation transcript:

1 Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly with memory that has been dynamically allocated efficiently work with complex data types such as large structures, linked lists, and arrays.

2 Introduction to pointers
Definition: A pointer is a variable whose content is the address of a value in memory. The size of the pointer variable must be n bits where 2n bytes is the size of the address space. On our current Intel x86 machines, the amount of space required to hold a pointer variable is 8 bytes and is not related to the size of the entity to which it points.

3 Introduction to Pointers
To declare a pointer in a program just use the type it points to followed by *. type *identifier; Examples: int *a; short *b; unsigned char *c; double *d;

4 Introduction to Pointers
We designate a pointer with an arrow ( ) char a; a char *p; int n; 10 int *q; float x; 7.5 float *r; double y; 15.2 double *t;

5 Introduction to Pointers
You can declare a pointer and have it point to (make its value be) that location in memory for the variable called x as follows: int x = 7; // variable x of type integer int *ptr; // a pointer to an integer variable ptr = &x; // pointer points to memory location x The asterisk (*) in the declaration denotes that ptr is to be a pointer and the int indicates the type of data to which ptr will point. The address operator (&) in the third line refers to the address of x.

6 Introduction to Pointers
What is physically going in RAM? int x = 7; int *ptr = &x; 7 20000 x address 20000 90000 ptr address After the first line, let’s say the address of that location in memory of where ptr is stored is After the second line above, ptr has the address of count as it’s value (i.e. it points to the location in memory called count, which has the value of 10 in it). 7 * x ptr

7 Dereferencing a pointer
If you want to change the value that ptr is pointing to, you can write the following: *ptr = 25; same effect as, x = 25; In either case, the value at that memory location was changed to 25.

8 Pointers int a; int b; int c; int *p; int *q; int *r; a = 5; b = 3; p = &b; q = p; r = &c; a b c p q r a 5 b 3 c p q r a 5 b 3 c p q r

9 Pointers p = &a; *q = 15; *r = *p; *r = a + *q a 5 b 15 c p q r a 5 b
20 p q r

10 Dereferencing a pointer
Remember: A direct assignment to a pointer variable will change the address of the pointer, not the value at the address that it is pointing to. A pointer directly refers to an address , and the * operator on the pointer will allow you to change the value at that memory location via dereferencing (indirection).

11 Initialization of pointers
Like all variables, pointers must be initialized before they are used. int *ptr; in this case, ptr may contain a value that is interpreted as a memory address. if the memory address does not exist, you will get a run-time error (segmentation fault). if the memory address is valid, you will either get a run-time error OR ? ??? pointer to unknown location

12 Initialization of pointers
/* CPSC */ /* Example of a common error: failure to initialize */ /* a pointer before using it.. This program is */ /* is FATALLY flawed.... */ int main() { int* ptr; *ptr = 99; // ptr has not been initialized printf("val of *ptr = %d and ptr is %p \n", *ptr, ptr); return 0; }

13 Initialization of pointers
The program may appear to work at times, if the address stored in ptr just happens to be a legal address in the address space of this program. Unfortunately, it may be the address of some other variable whose value is now 99!!! This situation is commonly referred to as a loose pointer and bugs such as these may be very hard to find.

14 Minimizing latent loose pointer problems
Never declare a pointer without initializing it in the declaration. int x; int *xptr = &x; int *ptr = NULL;

15 Correct use of the pointer
/* CPSC ptr2.c */ #include <stdio.h> int main() { int y; int* ptr; ptr = &y; // assign the address of y to the pointer *ptr = 99; // assign 99 to what the pointer points to (y) printf("y = %d \n*ptr =%d \nptr = %p \naddr of ptr = %p \n", y, *ptr, ptr, &ptr); return 0; }

16 Correct use of the pointer
gcc -o p2 ptr2.c ./p2 y = 99 *ptr = 99 ptr = 0x7ffc1a709e24 addr of ptr = 0x7ffc1a709e28 Note that a is a heap resident variable and has an address far removed from the stack resident y and ptr.

17 Correct use of the pointer
In C, variables that are declared within any basic block are allocated on the runtime stack at the time the basic block is activated. /* CPSC 1070 */ #include <stdio.h> int main() { int y; int* ptr; static int a; ptr = &y; // assign the address of y to the pointer *ptr = 99; // assign 99 to what the pointer points to (y) printf("y = %d \nptr = %p \naddr of ptr = %p \n", y, ptr, &ptr); ptr = &a; printf("addr of a is %p \n", ptr); return 0; }

18 void pointer a void * is a generic type that is not associated with any type. i.e., it is not the address of a character, an integer, a float, a double, or any other type. void *vptr; // pointer to void type int *iptr = NULL; // Null pointer of type int you cannot dereference a void pointer. i.e., int a = 55; void *vpter = &a; *vptr = 100; // invalid. cannot dereference vptr.

19 Pointer compatibility
pointers have a type associated with them not just pointer types, but pointers to a specific type the size of all pointers is the same (each holds the address on a memory location) the size of the variable that the pointer points to can be different. except for a pointer to void, it is invalid to assign a pointer of one type to a pointer of another type. a void * can be assigned to a pointer to any and a pointer to any type can be assigned to a void *.

20 Pointer compatibility
Examples: int i = 55; int j = 132; float y = 83.7; void *vptr; int *p; int *q = &j; float *fptr; Valid assignments: p = vptr; vptr = fptr; fptr = &y; vptr = q;

21 Pointer compatibility
Examples: int i = 55; int j = 132; float y = 83.7; void *vptr; int *p; int *q = &j; float *fptr; vptr = &i; Invalid assignments: p = fptr; // invalid to assign a pointer to a float to a pointer to an int fptr = &i; // invalid to assign a pointer to a float to point to an int *vptr = 112; // cannot dereference a void pointer

22 Pointer compatibility
Can solve the incompatibility problem by type casting, i.e. you can “tell” the compiler the type of the data that “ptr” is pointing to,. int x; char c; char *pc; void *vptr; vptr = (int *)&x; pc = (char *)&x;


Download ppt "Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly."

Similar presentations


Ads by Google