views:

268

answers:

5
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 :-)

+1  A: 

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).

Alex Martelli
thanks Mr. Alex
A: 

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.

WhirlWind
A: 

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.

George Edison
1. that's C++, not C. 2. That uses p+4 bytes [assuming a 32 bit system; p+8 for 64 bits]. 3. That's on the heap, not on the stack.
Mikeage
He has tagged the question `C` and not `C++` so `new int` should probably be replaced by a `malloc` call.
TommyA
My bad. And yes, I know allocating p bytes means p+4 bytes are allocated.
George Edison
+2  A: 

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 '}').

Goz
I didn't know you could allocate an array on the stack with more than 65535 elements.
George Edison
Maybe thats a non-standard extension but I've certainly made that mistake accidentally under GCC ... I'm pretty sure its allowable, though. Regardless an array of 65535 items of 4096 byte structs will give you the same issue ;)
Goz
Thank you Mr Goz for clearing my doubt. Your answer seems to be the most satisfactory one.
But Mr. Goz, aren't even stacks stored on the heap memory??
No, not really. You could make a case to say that the stack is a single large heap allocation, but the uses are so different, it's not worth it.This kind of dynamic allocation is certainly different to using malloc. Malloc'd memory only goes away when you free it, and could be in any arbitrary spot of RAM (which has performance implications). Also, malloc is extremely slow. This, however, is very, very fast to access and allocate, with the downside that it will free itself automatically (which could be an upside).
DeadMG
Thank you Mr. MG for exploring it in further depth. It was indeed helpful.
+2  A: 

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.

  1. They are like dynamic allocation in that the size can be deferred until runtime.
  2. They are like automatic allocation in that the memory lifetime is limited to the scope it is defined in.
R Samuel Klatchko
thanks Mr. Samuel