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

Alex Reynolds
97.2k59 gold badges251 silver badges356 bronze badges
asked Apr 3, 2010 at 16:57
3
  • 1
    Please indent your code with 4 spaces to format it properly. While you're at it, you might also want to indent the code itself. Commented Apr 3, 2010 at 17:06
  • 1
    Please try to compile and execute your code before posting it. This does not compile. Commented 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). Commented Apr 3, 2010 at 17:18

6 Answers 6

3

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

answered Apr 3, 2010 at 17:10
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't know you could allocate an array on the stack with more than 65535 elements.
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 ;)
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).
|
3

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.
answered Apr 3, 2010 at 17:15

Comments

1

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

answered Apr 3, 2010 at 17:09

Comments

1

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.

Dori
9251 gold badge12 silver badges20 bronze badges
answered Jul 18, 2011 at 2:58

Comments

0

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.

answered Apr 3, 2010 at 17:09

Comments

0

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.

answered Apr 3, 2010 at 17:10

3 Comments

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.
He has tagged the question C and not C++ so new int should probably be replaced by a malloc call.
My bad. And yes, I know allocating p bytes means p+4 bytes are allocated.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.