Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: local array definition versus a malloc call

Tags:

c++

arrays

c

malloc

What is the difference between this:

somefunction() {  
    ...  
    char *output;   
    output = (char *) malloc((len * 2) + 1);  
    ...  
}  

and this:

somefunction() {  
    ...  
    char output[(len * 2) + 1];  
    ...  
}  

When is one more appropriate than the other?

thanks all for your answers. here is a summary:

  1. ex. 1 is heap allocation
  2. ex. 2 is stack allocation
  3. there is a size limitation on the stack, use it for smaller allocations
  4. you have to free heap allocation, or it will leak
  5. the stack allocation is not accessible once the function exits
  6. the heap allocation is accessible until you free it (or the app ends)
  7. VLA's are not part of standard C++

corrections welcome.

here is some explanation of the difference between heap vs stack:
What and where are the stack and heap?

like image 282
Gush Avatar asked Jul 17 '10 00:07

Gush


People also ask

What is the difference between array and malloc?

The difference is that array size has to be known at compile time, and malloc can be fed with runtime information, so that is more dynamic. AND: malloced memory can be freed again when no longer needed; arrays with fixed size can not be freed.

Do you need to use malloc for arrays?

You definately have to use malloc() if you don't want your array to have a fixed size.

What is local array in C?

Local Arrays: The arrays which get initialized inside a function or block are known as local arrays. These types of arrays get memory allocated on the stack segment.

Why is malloc needed?

Malloc is used for dynamic memory allocation and is useful when you don't know the amount of memory needed during compile time. Allocating memory allows objects to exist beyond the scope of the current block. C passes by value instead of reference.


2 Answers

The first allocates memory on the heap. You have to remember to free the memory, or it will leak. This is appropriate if the memory needs to used outside the function, or if you need to allocate a huge amount of memory.

The second allocates memory on the stack. It will be reclaimed automatically when the function returns. This is the most convenient if you don't need to return the memory to your caller.

like image 91
Ned Batchelder Avatar answered Sep 22 '22 15:09

Ned Batchelder


Use locals when you only have a small amount of data, and you are not going to use the data outside the scope of the function you've declared it in. If you're going to pass the data around, use malloc.

Local variables are held on the stack, which is much more size limited than the heap, where arrays allocated with malloc go. I usually go for anything > 16 bytes being put on the heap, but you have a bit more flexibility than that. Just don't be allocating locals in the kb/mb size range - they belong on the heap.

like image 42
Mark H Avatar answered Sep 22 '22 15:09

Mark H