int main()
{
int p;
scanf("%d",&p);
fun()
{
int arr[p]; // isn't this similar to dynamic memory allocation??
}
}
//if not then what other objective is achieved using malloc and calloc??
//Someone please throw some light :-)
-
1Please indent your code with 4 spaces to format it properly. While you're at it, you might also want to indent the code itself.Thomas– Thomas2010年04月03日 17:06:26 +00:00Commented Apr 3, 2010 at 17:06
-
1Please try to compile and execute your code before posting it. This does not compile.3Doubloons– 3Doubloons2010年04月03日 17:09:43 +00:00Commented Apr 3, 2010 at 17:09
-
Sorry Mr. Alex but the above code does compile successfully on my dev-C++ 4.9.9.2 and on gcc 4.3.2 compilers. When I used the function sizeof(arr), it gave me a positive result which was (4*p).avi– avi2010年04月03日 17:18:48 +00:00Commented Apr 3, 2010 at 17:18
6 Answers 6
Creating an arbitrary amount on the stack is dangerous. You do not know how large the stack is (Well you can probably set it from the compiler) but if you create a 256Meg array as you have defined you WILL cause a stack overflow. IF you do it with malloc then you will create on the heap. This won't, then, touch the stack and won't affect the running of your application. Creating on the heap means that you are limited by the free memory on your system. If there is none the malloc returns NULL, if there is enough you are good to go. You also have the bonus of being able to free the memory whenever you like rather then when the allocation drops out of scope (ie passes the next '}').
6 Comments
This is an example of variable-length-arrays (VLAs). It's part of C99 and some compilers have support it as an extension to previous C standards.
VLAs are halfway between dynamic and automatic allocation.
- They are like dynamic allocation in that the size can be deferred until runtime.
- They are like automatic allocation in that the memory lifetime is limited to the scope it is defined in.
Comments
Runtime-sized allocation of arrays is legal in C99 (but not in the older C89 standard, which some badly-maintained compilers might still constrain you to); the popular gcc has long implemented it as a non-standard extension.
malloc still has the advantage that the memory can be deallocated (with free) exactly when you want it to be -- so you can usefully return a malloced array as your function's result, and the caller will free it when done with it. Runtime-sized arrays' lifetime is determined by lexical scope -- handy when it works for you, but an excessive constraint sometimes;-).
Of course, style-guides that constrain malloc and free to happen within the same function would give up on this crucial difference. There are others, though, such as the ability to realloc a malloc-ed array if you need to change its size (which also doesn't apply to runtime-sized arrays).
Comments
Dynamic Memory allocation is the process of allocating the memory during runtime.
int arr[p]
This code creates a static array in memory of size p,whose size can't be altered at runtime,but with dynamic memory allocation functions you can alter the size by using the realloc funntion.
Comments
This was added in C99. It certainly reduces the need for calloc() and malloc() by using the compiler for variable allocations. You might still use calloc() and malloc() where you need to support older compilers or in situations where you want to allocate more dynamic data structures.
Comments
You can only do this with gcc and not with VC++.
What you probably want to do is:
int * arr = new int[p];
When values are allocated on the stack, you must specify a constant.
3 Comments
C and not C++ so new int should probably be replaced by a malloc call.