Pointers Lecture Notes PDF

Document Details

ComfyViolin

Uploaded by ComfyViolin

University of Khartoum

Tags

pointers c++ programming memory allocation computer science

Summary

These lecture notes cover pointers in C++. The document details the significance of memory addresses and how pointers manage memory. It explores dynamic memory allocation and how pointers relate to arrays and functions.

Full Transcript

Lecture (3)  Introduction to Pointers  Pointers Basics and Arithmetic  Dynamic Memory Allocation  Pointers and Arrays  Pointers and Functions 2  The memory of your computer can be imagined as a succession of memory cells.  As soon...

Lecture (3)  Introduction to Pointers  Pointers Basics and Arithmetic  Dynamic Memory Allocation  Pointers and Arrays  Pointers and Functions 2  The memory of your computer can be imagined as a succession of memory cells.  As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address).  In some cases, we may be interested in knowing the address where our variable is being stored during runtime in order to operate with relative positions to it. 3  A pointer is a variable that stores the memory address of another variable. It points to the location in the memory.  Pointers give access to memory → memory management, data structures. 4  The address that locates a variable within memory is what we call a reference to that variable. x = &y;  The & “address-of” operator is used to get the address of a variable. 5  A pointer is created with the * operator. The address of the variable you're working with is assigned to the pointer.  For example: *ptr gives the value of the variable that ptr points to.  A pointer variable points to a data type (like int or string) of the same type.  Referencing: When pointer is allocated the address of the function to be associated with it then this process is referred to as referencing.  Dereferencing: When we use the (*)operator to get the value stored in the pointer. 6 7 Incrementing and Decrementing Pointers  For example, If a pointer holds the address 1004 and we decrement the pointer, then the pointer will be decremented by 4 or 8 bytes (size of the integer), and the pointer will now hold the address 1000.  It depends on the size of the data type the pointer points to. 8 Incrementing and Decrementing Pointers 9 Addition/Subtraction of Constant to/from Pointers 10 Subtraction of Two Pointers of the Same Type Comparison of Pointers Comparison to NULL !! 11  Dynamic memory allocation refers to performing memory allocation manually by a programmer at runtime.  For normal variables like “int a”, “char str”, etc, memory is automatically allocated and deallocated.  For dynamically allocated memory like “int *p = new int”, it is the programmer’s responsibility to deallocate memory when no longer needed. 12  The new operator denotes a request for memory allocation. 13  The delete operator denotes a request for deallocate dynamically allocated memory.  Can you write an example for delete operator to free the dynamically allocated?? 14  From Last lecture: arrays are the data structure that stores the data in contiguous memory locations.  We can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or pointers to arrays.  It is the pointer to the first element of the array instead of the whole array, but we can access the whole array using pointer arithmetic. 15 Pointers to Multidimensional Arrays !! 16 Every function’s code resides in memory, so every function has an address like all other variables in the program. The function pointer is used to point functions, similarly, the pointers are used to point variables. It is utilized to save a function’s address. The function pointer is either used to call the function or it can be sent as an argument to another function. 17  Function pointer used to call the function 18  Passing a function pointer as a parameter 19

Use Quizgecko on...
Browser
Browser