Reputation: 49
I was learning about malloc
, calloc
, free
and realloc
in C but I don't get when should I use these functions.
For example: if I want to create a program in C that creates an array and the size of it is determined from user input:
int n;
printf("Enter THe Number of element in Array:..\n");
scanf("%d",&n);
int x[n];
for(int i = 0 ; i < n ; i++)
{
x[i] = i+ 1;
}
for(int y = 0 ; y < n ; y++)
{
printf("%d\n",x[y]);
}
output:
Enter THe Number of element in Array:..
5
1
2
3
4
5
So here I created this program without using Dynamic memory allocation, which makes things more complicated to me.
Upvotes: 0
Views: 1566
Reputation: 68013
Heap is usually much larger than the area used to accommodate automatic variables and the static storage area (global variables).
So you may use the dynamic allocation when:
but in your example
int x[n];
is not the dynamic allocation. You simply allocate the automatic array with the size set runtime. You cant change the size of it, you cant also use it when you exit the scope as its lifetime is bound to the scope it was defined in.
Upvotes: 1
Reputation: 412
you can use dynamic memory allocation to allocate variables in the run time and there are some steps you should follow them where you need to allocate the memory by using this
int* x = malloc(n * sizeof(int));
and free it after use by using this
free(x);
Upvotes: 0
Reputation: 123578
Your example uses something called a variable-length array, which can substitute for dynamic memory in some circumstances. However, VLAs are not supported in all versions of C (they were first introduced in the 1999 revision and made optional in the 2011 revision) and they cannot be members of struct
or union
types, nor may they be used as “globals” at file scope. Like fixed-size arrays, their storage is released as soon as you exit their enclosing scope (i.e., block or function). They usually cannot be arbitrarily large.
You would use dynamic memory (malloc
, calloc
, or realloc
) under the following circumstances:
Upvotes: 1
Reputation: 51894
One (very common, in fact) case is that using memory allocated locally (usually, but not necessarily, on the stack), as your example does, will not work if that memory is allocated inside a function that is called from elsewhere, because that memory will be reset - or 'lost' - when that function returns.
In such a case, you need to dynamically allocate memory (on the heap), using a malloc
(or related) call. Memory thus allocated will be 'global', and can be accessed by any other part of the program (so long as it has the pointer), until it is explicitly freed via a call to the free()
function.
Here's an example:
#include <stdio.h>
#include <stdlib.h>
int* newArray(int n)
{
// int x[n]; // Won't work: This is local (stack) memory that is lost on return
int* x = malloc(n * sizeof(int)); // Works: This is global (heap) memory
return x;
}
int main()
{
int n;
printf("Enter The Number of Elements in Array: ");
scanf("%d", &n);
int* x = newArray(n);
for (int i = 0; i < n; i++) {
x[i] = i + 1;
}
for (int y = 0; y < n; y++) {
printf("%d\n", x[y]);
}
free(x); // You MUST free memory that you've allocated with malloc
return 0;
}
(You can try uncommenting the "won't work" line and commenting out the "works" line, to see for yourself.)
Upvotes: 0