Memory Management and Pointers: Tough, Confusing or Easyyy??

Aastha Patel
Women Techmakers Nagpur
4 min readJul 19, 2021

Have you ever wondered why and how memory allocation and pointers work.Being a newbie to programming I used to find this concept a bit tough which is actually not now! So, in this article we will examine a key concept, foundational to any programming language: the usage of memory !:)

It also builds upon the basic concept of pointers along with Memory Management. So, read on to learn…happy reading!!

What is memory allocation?

Memory allocation is the process of setting aside sections of memory in a program to be used to store variables, and instances of structures and classes.

Let’s understand it with an example in layman terms!

It’s like someone named ‘AASTHA’ being allocated to a hotel room from a lot of free or empty pre-existing rooms. This example probably made it very clear as to how the computer does the allocation of memory or creates a variable.

Why do we need memory allocation?

The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed.

Is memory allocation the same for all languages or different for some?

When it comes to C, memory is allocated using the functions malloc(), calloc() and realloc() and is de-allocated using free().

However, in object-oriented programming languages like C++, C# and Java, memory is dynamically allocated using the new and de-allocated using delete keywords (operators) in case of C++.

How many types of memory allocation are there?

Basically, there are three types of memory allocations. They are:

· Compile-time or Static Memory Allocation.

· Run-time or Dynamic Memory Allocation.

· Automatic memory allocation.

1.Static Memory Allocation: When the amount of memory is to be allocated beforehand i.e. at the time of compilation, it is known as Static Memory Allocation. It means that the memory for your variables is allocated when the program starts. Once the memory is allocated statically, it cannot be de-allocated during program run. So it leads to wastage of storage space.

It applies to global variables, file scope variables and variables qualified with static defined inside functions. Static memory allocation is done on the stack. The address of the variable can be found using the address of operator and can be assigned to a pointer.

2.Dynamic Memory Allocation: When the memory allocation is done at the time of execution(run time), it is known as dynamic memory allocation. . In the Dynamic allocation of memory, space is allocated by using calloc() and malloc() functions. When the value is returned by functions, it is assigned to pointer variables. You can also control the exact size of these memory locations.Also If you don’t free the memory , you’ll run into memory leaks, which may cause your application to crash, since at some point of time, the system cannot allocate more memory. Dynamic memory allocation can be done on both stack and heap.

3.Automatic Memory Allocation: It occurs for (non-static) variables defined inside functions, and is usually stored on the stack. You do not have to reserve extra memory using them, but on the other hand, have also limited control over the lifetime of this memory. Automatic variables in a function are only there until the function finishes.

The concept of pointers and memory allocation are interrelated. When you allocate memory for your own data during program execution its called dynamic memory allocation at runtime using pointers.

So, Let’s look at how they are related by understanding the concept of pointer!

What’s the pointer?

A pointer is a variable that stores a memory address or that contains the address of another variable (int, char, array, function, or any other pointer) where addresses are the location number that always contains the whole number.

Why is it called pointer?

It is called pointer because it points to a particular location in memory by storing the address of that location. Pointers are symbolic representations of addresses.

Syntax of pointer :

data type * variable name;

(here, *before the pointer indicates the compiler that variable declared as a pointer.)

Example:

int *ptr; //ptr can point to an address which holds int data

When a pointer is declared, it contains garbage value i.e. it may point to any value in the memory.

There are two operators used in the pointer. They are:

1.Address Operator (&) — It gives the value stored at a particular address

2.Indirection/ de-reference operator (*) — It cannot be used in any constant or any expression.

Let’s take a look at different types of pointers:

1.Null pointer

2.Dangling pointer

3.Generic pointer

4.Wild pointer

5.Complex pointer

6.Near pointer

7. Far pointer

8.Huge pointer

To summarize the concept, pointers allow you to refer to the same space in memory from multiple locations. This means that you can update memory in one location and the change can be seen from another location in your program. Also you can use pointers at any place where you need to obtain and pass around the address to a specific spot in memory.

So, how was it…tough…confusing…easy? Let me know in the comments section :D

--

--

Aastha Patel
Women Techmakers Nagpur

I write about- Technologies, my experience, and everything that inspires me| Software Developer | WTM Ambassador | Technophile