The alloca() Function

Here's a function you can use when you need to forget to free memory on purpose. Continue reading →

Any memory allocated in a function is retained when the function leaves, unless you first free it. This rule applies to all memory allocation functions — except one.

Of course, memory allocated in the main() function is freed when it returns (the program exits). This is the reason why I’m often lazy and forget to free memory at the end of the main() function, a sin for which I repent. But an exception to the memory allocation/freeing rule is the alloca() function.

Like an auto variable, the alloca() function’s scope is local to the function it’s used in: Memory is allocated, but released when the next function is called. It’s not released upon return from the function (though this aspect may be undefined), but when you call another function the memory alloca() allocated is gone.

To demonstrate the alloca() function, I first wrote code that uses malloc() to allocate and use a buffer:

2024_03_09-Lesson-a.c

#include 
#include 

char *astring(void)
{
    const int size = 12;
    char *a;
    int x;

    a = malloc(size+1);
    for( x=0; x

The astring() function allocates 13 bytes of char storage, which is assigned characters and output. I don’t test the allocation for failure, which is something you must do in your own code. Here, allocating 13 bytes shouldn’t be an issue.

The main() function saves the pointer returned from astring() in variable alpha. Before outputting the string, the header() function is called. Yes, this is weird code, but it’s designed to demonstrate how the alloca() function works: A second function must be called to free the memory (according to the docs). Here’s the ouput:

Temporary allocation
Allocated string: 'AAAAAAAAAAAA'
After temporary allocation
Allocated string: 'AAAAAAAAAAAA'

To update the code with alloca() instead of malloc() these changes are made: stdlib.h is replaced by alloca.h; malloc() is replaced by alloca(); and the free() statement is removed from the main() function. You can click here to view the updated version of the code on GitHub.

Here is the output when clang builds the program:

Temporary allocation
Allocated string: 'AAAAAAAAAAAA'
After temporary allocation
Allocated string: ''

The string is zapped after the call to the header() function.

Being curious, I ran a few tests to confirm how the alloca() function works. If I don’t call the header() function, the allocated string is output:

Temporary allocation
Allocated string: 'AAAAAAAAAAAA'
After temporary allocation
Allocated string: 'AAAAAAAAAAAA'

I had assumed that the allocated storage was released when the function call returned. The man page says that alloca()‘s memory is released when another function is called, which is why the original code called the header() function.

When I tested the code with the GNU compiler, the memory doesn’t seem to be released. Here is the program’s output when built with gcc:

Temporary allocation
Allocated string: 'AAAAAAAAAAAA'
After temporary allocation
Allocated string: 'AAAAAAAAAAAA'

Though it looks like the allocated memory is intact, if you attempt to free() the pointer’s address you get an invalid pointer message as the program runs. This error message appears for programs built by both clang and gcc. So I guess the memory is released when built with gcc but something quirky is keeping the contents around. It could just be my own system.

I can’t think of an application where the alloca() function is necessary, especially given that memory can always be freed. Yet this function is available in some C libraries and must have some purpose.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow