top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is dynamic memory allocation? How can we free the memory when variables are no longer in use?

+6 votes
692 views
posted Dec 17, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Dynamic memory allocation is where your program requests the allocation and use of memory from the operating system via the run-time library.

malloc - allocates the specified number of bytes
realloc - increases or decreases the size of the specified block of memory. Reallocates it if needed
calloc - allocates the specified number of bytes and initializes them to zero
free - releases the specified block of memory back to the system

answer Dec 17, 2013 by Satyabrata Mahapatra
0 votes

Dynamic memory allocation is where your program requests the allocation and use of memory from the operating system at run time based on the need of the process.

In C we can use malloc/calloc for allocation of the memory and free() to release it.
In C++ we can use new and delete operators to achieve the same(however malloc/calloc or free can also be used in C++)

answer Dec 17, 2013 by Salil Agrawal
Similar Questions
+4 votes
printf ("%s %d %f\n", (*ptr+i).a, (*ptr+i).b, (*ptr+i).c);
[Error] invalid operands to binary + (have 'struct name' and 'int')

where ptr is a pointer to the structure and allocated dynamic memory using malloc. Any suggestion would be helpful.

Thanks in advance.

+3 votes
main {
 int a;
 a = 10;
}

Here, when the memory will be allocated.
Compile/Run time? why ?

...