avi
avi

Reputation: 1877

dynamic memory allocation in C

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

Upvotes: 2

Views: 1000

Answers (6)

techno
techno

Reputation: 6500

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.

Upvotes: 1

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

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.

Upvotes: 3

Goz
Goz

Reputation: 62323

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

Upvotes: 3

Nathan Osman
Nathan Osman

Reputation: 73165

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.

Upvotes: 0

WhirlWind
WhirlWind

Reputation: 14112

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.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881497

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

Upvotes: 1

Related Questions