C Memory Management Example

Real-Life Memory Management Example

To demonstrate a practical example of dynamic memory, we created a program that can make a list of any length.

Regular arrays in C have a fixed length and cannot be changed, but with dynamic memory we can create a list as long as we like : Continue reading C Memory Management Example

C Deallocate Memory

Deallocate (free) Memory

When you no longer need a block of memory you should deallocate it. Deallocation is also referred to as “freeing” the memory.

Dynamic memory stays reserved until it is deallocated or until the program ends.

Once the memory is deallocated it can be used by other programs or it may even be allocated to another part of your program.

Continue reading C Deallocate Memory

C Reallocate Memory

Reallocate Memory

If the amount of memory you reserved is not enough, you can reallocate it to make it larger.

Reallocating reserves a different (usually larger) amount of memory while keeping the data that was stored in it.

You can change the size of allocated memory with the realloc() function.

The realloc() function takes two parameters:

int *ptr2 = realloc(ptr1, size);
  • The first parameter is a pointer to the memory that is being resized.
  • The second parameter specifies the new size of allocated memory, measured in bytes.

Continue reading C Reallocate Memory

C Access Memory

Access Dynamic Memory

Dynamic memory behaves like an array, with its data type specified by the type of the pointer.

As with arrays, to access an element in dynamic memory, refer to its index number:

ptr[0] = 12;

You can also dereference the pointer to access the first element:

*ptr = 12;

C Allocate Memory

The process of reserving memory is called allocation. The way to allocate memory depends on the type of memory.

C has two types of memory: Static memory and dynamic memory.

Static Memory

Static memory is memory that is reserved for variables before the program runs. Allocation of static memory is also known as compile time memory allocation.

C automatically allocates memory for every variable when the program is compiled. Continue reading C Allocate Memory

C Memory Management

Memory management is the process of handling how much memory a program uses through different operations.

Memory in C

Understanding how memory works in C is important. When you create a basic variable, C will automatically reserve space for that variable. An int variable for example, will typically occupy 4 bytes of memory, while a double variable will occupy 8 bytes of memory. Continue reading C Memory Management

C Enumeration

C Enums

An enum is a special type that represents a group of constants (unchangeable values).

To create an enum, use the enum keyword, followed by the name of the enum, and separate the enum items with a comma:

enum Level {
  LOW,
  MEDIUM,
  HIGH
};

Note that the last item does not need a comma.

It is not required to use uppercase, but often considered as good practice.

Enum is short for “enumerations”, which means “specifically listed”.

Continue reading C Enumeration

C Structures

Structures

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.

Unlike an array, a structure can contain many different data types (int, float, char, etc.).


Continue reading C Structures