printf("Enter number of elements\n");
scanf("%d",&n);
int num[sizeof(int)*n];
Is this a right way to dynamically allocate array size?
The informal term dynamic allocation almost certainly refers to the formal term allocated storage , meaning heap memory returned from malloc
/ calloc
/ realloc
.
Sure, there's other "dynamic things" around, like a stack which dynamically grows and sink, but we don't call stack allocation dynamic allocation.
Therefore it is impossible to do dynamic allocation without malloc
/ calloc
/ realloc
.
What you have in your example is a variable-length array (VLA). They are allocated in run-time, typically on the stack. You use it incorrectly. You should
n
is a valid value within a certain range 1 to max, before creating the array.int num[n];
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.