170 likes | 226 Vues
Learn about main memory (RAM), memory addressing, pointers, and how to utilize them in C programming. Covers memory allocation, accessing memory addresses, pointer variables, initialization, dereferencing, and more.
E N D
Today’s Schedule • Grades • Structure Examples • Memory & Pointers • Go Over Quiz
Main Memory • Main memory of computers (also called RAM or Random Access Memory) is made up of bytes. • The number of bytes in a computer with 512 MB of RAM is: 512 * 1024 * 1024 = 5,3687,0912 bytes • Each byte in the main memory has a unique binary address that can be used to refer to it. • Earlier computers used to have a 16-bit address. Nowadays, most computers have a 32-bit (or even 64-bit) addressing system. • The range of integers that can be stored in 32 bit address is 0 through 4,294,967,295. Thus, in a 32-bit machine, we can have only 4 GB of addressable main memory (since we can only represent that many bytes with unique addresses)
Notes on 4 bit Computer • The table above shows the memory addresses in a 4-bit computer. • Above the memory addresses are given in both the binary and the hexadecimal notation. • Hexadecimal (base-16) numbers are given the prefix 0x and are made up of symbols 0-9 and A-F. • Notice that with 4 bits we can only address 16 bytes. In general, with n-bits, we can address 2n bytes.
Accessing Memory Addresses in C • Every variable declared in C program is assigned a unique memory address. • The amount of memory needed depends on the type of the variable (for example, an integer variable requires 4 bytes of memory). • We can access the address of a variable using the & operator. • The program below prints the memory address of an integer variable using printf statements. • main () • { • int a; • printf (" Memory Address %p \n", &a); • } • Notice the format specifier %p that is used to display the memory address. • This format specifier causes the memory address to be displayed in the hexadecimal notation.
Pointers • A solid understanding of Pointers is essential to utilize the full power of the C programming language. • A pointer is simply a variable that can be used to hold memory addresses. • Pointers are convenient for a number of reasons: • It allows us to allocate and release memory for data structures during program execution. • It provides a mechanism by which functions can return more than one single value. • When we pass variables to functions as pointers (by means of their memory addresses), we can avoid making copy of the variables (which can be very large..for example a huge structure or array).
Pointer variable • A pointer variable is simply a variable that can be used to hold memory addresses (location) of another variable. • An integer pointer variable can be used to store the memory address of an integer variable. • A char pointer variable can be used to store the memory address of a character variable. • A float pointer variable can be used to store the memory address of a float type variable.
Declaring a pointer variable • A pointer variable should be declared before usage. Declaring an integer pointer variable p: • int *p; • * informs the compilier that variable p is a pointer variable. • int tells the compiler that variable p will be used to store memory address of integer variables (i.e. a pointer to an int).
Initializing a pointer variable • Before we can use a pointer, we should initialize it using the assignment operator. • For an integer pointer, we can only assign the address of some other integer variable. • The example below assigns the address of integer variable a to the pointer p main () { int a = 10; // integer variable initialized to value 10 int *p; // integer pointer p = &a; // store address of a in p }
What this does main () { int a = 10; // integer variable initialized to value 10 int *p; // integer pointer p = &a; // store address of a in p } p a 10
k 2 1335 1336 1337 j ptr 2 1336 1557 1843 More on Pointer int k, j, *ptr; k = 25; j = k; ptr = &k; • Recall: Variable • Name • Value • Memory location: address • Pointer • Record the address of another variable
NULL • A pointer can be initialized to a special value called NULL (internally 0 in most computers). • int *p = NULL; // p is a NULL pointer • A pointer that has the value NULL is called a NULL pointer. • Default value that points to nothing • Should initialize to NULL if wanted address is not known
Dereferencing • Pointers store memory addresses. • Can access the contents of the memory address stored in a pointer. (access the value a pointer points to) • This is called dereferencing a pointer • Done using the operator *
* Operator • Returns the value stored at an address • Place in front of a pointer to return the value stored at the pointers address int a = 7; int *p = &a; • p 0x7e473d (the location of a) • *p 7(the value stored at the location of a) • Note that if p is a pointer variable, then *p is an alias for the object to which p currently points to.
Aliases • Refer to the value of that which it points to by using the indirection operator * • Let ptr = &k; • *ptr = 2; • Same as: • k = 2; • Different from • ptr = 2; • Common mistake: forget * when dereferencing
Multiple pointers main () { int a = 10; // integer variable initialized to value 10 int *p1, *p2, *p3; // 3 integer pointers p1 = &a; // store address of a in p1 p2 = p1; // copy value in p1 (address of a) to p2 p3 = p2; // copy value in p2 (address of a) to p3 int c = *p3; // dereference p2 (value of a) and assign it to c printf (" %d %d %d \n", *p1, *p2, c); // output will be 10 10 10 }
a 10 1335 1336 1337 p1 p2 p3 1445 1557 1843 2031 2032 2033 Demonstration main () { int a = 10; // integer variable initialized to value 10 int *p1, *p2, *p3; // 3 integer pointers p1 = &a; // store address of a in p1 p2 = p1; // copy value in p1 (address of a) to p2 p3 = p2; // copy value in p2 (address of a) to p3 int c = *p3; // dereference p2 (value of a) and assign it to c printf (" %d %d %d \n", *p1, *p2, c); // output will be 10 10 10 } 1336 1336 1336 c 10 10 10 10