class: center, middle ### COMP26020 Programming Languages and Paradigms -- Part 1 *** # Introduction to Pointers ??? - Hello everyone, in this video I will introduce a fundamental concept in C, pointers --- # Program Memory Layout - All the program's code and data is present somewhere in memory - The area of memory accessible by the program is the **address space** - It's a large array of contiguous bytes - **Addresses** are indexes in that array
-- - Note that this is
virtual
memory: - Address space size independent from the amount of RAM - Each program gets its own address space ??? - Before explaining what a pointer is, we need to understand the how the memory that is accessible to a program looks like - All the data manipulated by a program, as well as all the code it executes, is present somewhere in memory - The area of memory that is accessible for the program to read, write and execute is called the address space - You see the address space as a very large array of contiguous bytes - Each byte has an index into this array, named an address - Addresses range from 0 to the maximum addressable byte - On x86-64 the kernel configures virtual memory from address 0 to 128TB to be accessible by each program - Note that this is virtual memory so each program has his own copy of that array - Its size is also completely independent of the actual physical memory in the machine - The address space is generally very sparse and only the bytes that are read/written are mapped to physical memory - Finally, note that we generally use hexadecimal numbers, prefixed with 0x, to indicate addresses as they can be pretty big --- # Program Memory Layout - **Address of a variable**: the address in memory of the first byte storing that variable ??? - So as I said all code executed and data manipulated by the program reside in memory - Each variable has an address which is the first byte of its memory location -- ```c int glob = 12; char string[] = "abcd"; typedef struct { int member1; float member2; } mystruct; int main(int argc, char **argv) { mystruct ms; ms.member1 = 42; ms.member2 = 4.2; printf("ms member1: %d, member2: %f\n", ms.member1, ms.member2); return 0; } ``` --- # Program Memory Layout - **Address of a variable**: the address in memory of the first byte storing that variable ```c *int glob = 12; // glob's address is 0x3300 *char string[] = "abcd"; // string's address is 0x2f0 typedef struct { int member1; float member2; } mystruct; int main(int argc, char **argv) { * mystruct ms; // ms' address is 0x123 ms.member1 = 42; ms.member2 = 4.2; printf("ms member1: %d, member2: %f\n", ms.member1, ms.member2); return 0; } ```
??? - For example here we have a global variable glob, taking 4 bytes at address 0x3300 - A five byte string is located at 0x2F0 - And we have a data structure that is 8 bytes (sum of the size of its members) located at 0x123 --- # Addresses - Use the `&` operator to get the address of a variable ```c int glob = 12; typedef struct { int member1; float member2; } mystruct; int main(int argc, char **argv) { mystruct ms = {1, 2.0}; // With modern processors an address is a 64 bits value so we need the right format // specifier: "%p", which will print the address in hexadecimal prefixed by "0x", // for example "0x12345" * printf("ms address is: %p, glob address is %p\n", &ms, &glob); return 0; } ``` .codelink[
`09-pointers-introduction/ampersand.c`
] ??? - We can get the address of a variable in C with the & operator - Let's try this example, where we use that operator to retrieve and print the address of an int variable and also a data structure - Note the use of the marker lx: an address is 64 bits on modern processors so it's a long, and we use x to specify printing in hexadecimal - So we can see that glob is at address XXXX and ms is at XXX --- # Pointers .center[***Pointer*: variable that contains an address** (possibly of another variable)] -- name:ptrintro - Declaration: `
*
;` ```c int v = 42; int *ptr = &v; ``` ??? - And now we can explain what a pointer is. - A pointer is simply a variable that holds an address, possibly the address of another variable - We can declare a pointer by indicating the type of the pointed variable, followed by star, and then the name of the pointer --
--- template: ptrintro
??? - For example here we create a pointer named `ptr` that holds the address of `v` - Because `v` is of type `int`, `ptr` is a pointer of `int`, indicated by `int *` - So we get something like this in memory. - Let's say that `v` is located at address `0xF120`. It contains 42 and takes 4 bytes because its an `int`. - Then we have ptr that is somewhere in memory and it contains the address of `v`, `0xF120` - I illustrate the relationship with the arrow --- template: ptrintro
??? - It's a pointer so its size is 64 bits -- - **Dereferencing, i.e. accessing the pointed value, done with **`*` ```c printf("pointer value: %p\n", ptr); // 0xF123 *printf("pointed value: %d\n", *ptr); // 42 ``` ??? - Now the cool thing with pointers is that we can access the pointed value through the pointer - We can read or write this value it with the star operator - As it is done on the second line here - Through ptr I can read and modify the value of v, for example here it is printed we do star ptr, it evaluates to the value of v which is an int so we use %d to print --- name:ptr # Pointers ```c int glob = 12; double glob2 = 4.4; typedef struct { int member1; double member2; } mystruct; int main(int argc, char **argv) { mystruct ms = {55, 2.23}; int *ptr1 = &glob; double *ptr2 = &glob2; mystruct *ptr3 = &ms; /* Print each pointer's value (i.e pointed address), and pointed value */ printf("ptr1 = %p, *ptr1 = %d\n", ptr1, *ptr1); printf("ptr2 = %p, *ptr2 = %f\n", ptr2, *ptr2); printf("ptr3 = %p, *(ptr3).member1 = %d, *(ptr3).member2 = %d\n", ptr3, *(ptr3).member1, *(ptr3).member2); } ``` .codelink[
`09-pointers-introduction/pointer.c`
] ??? - Here is a nice example we can run, where we have 3 pointers to int, double, and a data structure variable - We print the values of each pointer, that are the addresses of the pointed elements, and we also print the pointed elements through the pointer - Note that it also works for data structures: star ptr3 dot member1 evaluates to 55, the value of the first member of ms --- template: ptr
--- template: ptr
--- template: ptr
--- template: ptr
--- # Summary - A pointer: variable that stores an address corresponding to a memory location - Can access that location through the pointer ---- .center[Feedback form: https://bit.ly/3fKzIzr]
??? - Let's recap - A pointer is a variable that holds an address corresponding to a particular memory location - And we can access that location through the pointer - In the next video we will see how useful pointers are