How to allocate/free the memory in C & Dangling Pointer basics

This Doc is very useful for the learner who don’t know much about C.

In general, we have two ways to allocate the memory in C

  1. Static memory allocation
  2. Dynamic memory allocation

Static memory allocation:

Static means “fixed – not changing”.

To allocate this type of memory, we should know how much memory is needed for your purpose.

Let’s say you want to store the 5 integer type of variables,  then you have the clear idea how much memory you required here. Then go for the static memory allocation like the way we use every time.

int arr[5];

Ex of static memory allocations:

Int a; char b; char arr[10];

Note:  Static memory is allocated at the time of compilation.

Freeing the memory is not in our hands in this case, once the function/program vanishes then the memory will be freed.

Dynamic memory allocation (Run time memory allocation) :

Dynamic means “not fixed- Keep on changing ”

So as per the meaning,  when our memory requirement is keep on changing then go for the dynamic way of memory allocation i.e means we don’t know prior how much memory is needed for our requirement. Based on the requirement we will know at the run time, and allocate the memory.

We have different methods in C to allocate the memory dynamically.

  1. Malloc
  2. Calloc
  3. Realloc

Click below for more details.

Malloc vs calloc vs realloc

To reallocate the memory assigned by these methods, we need to explicitly do by free();

If you don’t free the memory, it won’t be available to you till the termination of your program.

Dangling Pointer:

A pointer points to the memory location that has been deleted (freed) is called Dangling pointer.

Programmer should be careful while using the pointer, otherwise we may come across Dangling pointer cases.

Following are the scenarios where we see the Dangling pointer

Try to use the memory after freed:

 

 

char *ptr;                                // Declaring a pointer variable

ptr = (char *)malloc(20);      // allocating memory to the pointer

.

.

free(ptr);                 // freed the memory after usage.

*ptr = ‘c’;              // again try to use the freed or deleted memory location causes                                                   Dangling  pointer

Returning the local variable to other function

 

 

int *f()

{

int x;                               //local variable

return &x;                     // returning local variable address that cab be vanished                                                                  after function terminates

}

main()

{

          int *ptr;

         ptr = f();                    // Here the pointer is dangling because of getting the local                                                             variable address.

         printf(“%d”, ptr);    // program crash here

}

How to avoid the pointer becoming Dangling here???

If you just use the static variable instead of local then the pointer won’t be Dangling. Since static variables persist in memory till the program terminates.

int *f()

{

static int x;                    // static local

return &x;                     // returning local variable address that cab be vanished after                                                        function terminates

}

main()

{

int *ptr;

ptr = f();                    // Here the pointer is dangling because of getting the local variable address.

printf(“%d”, ptr);    // program won’t crash here

}

 

  • Variable goes out of scope

 

main()

{

Int *ptr;

:

:

:

{

int ch;

Ptr =&ch;

}

…………..

//here if you try to use the ptr then it will be dangling.

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s