pointers-memory

Pointers & Memory in C/C++

Initialize pointer with *

Think of & as ‘the address of’

  • is used to get the value from pointer (deferencing)

int x= 3;

int *pX = &x;
// *integer pointer named pX is set to the address of x*

int y = *pX;
// * is used alone ( no type beside it )
// * is now a dereference , so it goes to the pointer address and gets the value
// *integer named y is set to the thing pointed to by pX*

Pointer is just a addressPointer is just a address

How I visualize pointersalt text

Declaration: To declare a pointer variable, use the asterisk (*) symbol before the variable name. For example:

int *ptr; // declare a pointer to an integer variable

Assignment: To assign a memory address to a pointer variable, use the address-of operator (&) with the variable whose address you want to store. For example:

Dereferencing: To access the value of the variable pointed to by a pointer, use the asterisk (*) symbol before the pointer variable name. For example:

Pointer Arithmetic: You can perform arithmetic operations on pointers such as addition, subtraction, etc. For example:

Null Pointer: A null pointer is a pointer that does not point to any memory location. To initialize a pointer as a null pointer, use the constant value "NULL" or "0". For example:

Pointer to Pointer: A pointer to a pointer is a pointer variable that holds the address of another pointer variable. For example:

Passing Pointers to Functions: Pointers can be passed to functions as arguments. When a pointer is passed to a function, the function can modify the value of the pointed-to variable. For example:

Advantages of using pointers in C:

  1. Dynamic memory allocation

  2. Accessing array elements

  3. Passing parameters to functions

  4. Returning multiple values from functions

  5. Implementing complex data structures

  6. Interacting with hardware


Call by value

  • Variable passed into arguement is copied

  • Function does not affect the original variable

alt text

Call by reference

  • Address to variable is passed into arguement

  • Function affect original variable

alt text
alt text

Pointer to array


Pointer to function

Callbacks

Callback is a function passed as an arguement into another function which can be executed later

callbacks can make code more modular, flexible, and maintainable.


Application memory

Portion of memory used by application

Categories

  • Code

  • Data

  • Stack

  • Heap

The Call Stack

Stack

  • Limited size

  • Variables declared within a function, function arguments, and return addresses are stored in stack

  • Too much spaced used in stack results in stack overflow

  • Implements stack data structure

Heap

  • Size can increase dynamically

  • Memory that is manually allocated and managed by the programmer

  • UNRELATED to heap data structure


Dynamic memory allocation

In C , we use these functions to dynamically allocate memory in the heap

malloc()

Allocate memory of a certain size , each byte is not initialized

calloc()

Allocate memory of a certain size , initialize the block of memory to 0

realloc()

Allocate memory of a certain size , initialize the block of memory to 0

free()

Memory allocated on the heap using malloc, calloc, or **realloc**remains allocated until it is explicitly freed using the **free()**function.

Using pointers in the stack causing errors

This happens when the memory location the pointer is pointing to has been deallocated.

Might happen when the memory location has been overwritten

Solution:

Use pointers in the heap!

Data has to be explicitly deallocated, so no overwritting of data


Memory Leak

Happens when memory in the heap is not deallocated after use

→ can cause program to crash

To Avoid : Manage memory allocation and deallocation

eg. using free( ) in C to deallocate memory after it is used

Last updated