Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is alloca completely replaceable?

Tags:

arrays

c

alloca

I've read quite a few places that alloca is obsolete and should not be used and Variable Length Arrays should be used instead.

My question is this: Is alloca completely replaceable by variable length arrays?

In my particular instance I have something that looks like this:

typedef struct { 
  int *value; 
  size_t size; 
  } some_type;

void SomeExternalFunction(some_type);

...

void foo(){
  //What I thought to do
  some_type bar;
  bar.value=alloca(sizeof(int)*10);
  SomeExternalFunction(bar);

  //what should be done without alloca
  some_type fizz;
  int tmp[10];
  fizz.value=tmp;
  SoemExternalFunction(fizz);
}

Am I missing something or is this an actual good use of alloca? Also assume for this example that for some reason I want for the value to be allocated on the stack

like image 769
Earlz Avatar asked Aug 15 '10 19:08

Earlz


People also ask

When should I use the alloca () function?

The alloca () function is machine- and compiler-dependent. For certain applications, its use can improve efficiency compared to the use of malloc (3) plus free (3). In certain cases, it can also simplify memory deallocation in applications that use longjmp (3) or siglongjmp (3). Otherwise, its use is discouraged.

How does the space allocated by alloca () get freed up?

Because the space allocated by alloca () is allocated within the stack frame, that space is automatically freed if the function return is jumped over by a call to longjmp (3) or siglongjmp (3) . The space allocated by alloca () is not automatically deallocated if the pointer that refers to it simply goes out of scope.

Why can't I use alloca () inside the list of arguments?

On many systems alloca () cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca () would appear on the stack in the middle of the space for the function arguments. This page is part of release 5.13 of the Linux man-pages project.

What is the difference between malloc and alloca?

Because of the way alloca works, the memory it allocates is freed even when an error occurs, with no special effort required. By contrast, the previous definition of open2 (which uses malloc and free) would develop a memory leak if it were changed in this way.


1 Answers

There is an important difference between VLA's and alloca: The memory alloca() returns is valid as long as the current function persists. The lifetime of the memory occupied by a VLA is valid as long as the VLA's identifier remains in scope. You can alloca() memory in a loop for example and use the memory outside the loop, a VLA would be gone because the identifier goes out of scope when the loop terminates. This means, you can do this with alloca() and enough stack space:

typedef struct node { int data; struct node *next; };
void fun()
{
 struct node *n=0;
 int d;
 /* Now we are building a single-linked list on the stack! */
 while(d=get_something()) {
  struct node *x=alloca(sizeof(*x));
  x->next=n; 
  x->data=d;
  n=x;
 }
 do_something_with(n);
} // and the whole thing is deleted here..

You can't do this with VLAs.

like image 60
Nordic Mainframe Avatar answered Sep 30 '22 14:09

Nordic Mainframe