C Programming

Alloca 3 C Function

Space that is periodically released is allocated with the alloca command. The caller’s stack frame is allocated the length bytes of memory by the alloca() method. Once the code that invoked alloca() has returned to its caller, this interim memory is immediately released. Within this guide, we will be discussing the use of alloca() to assign the memory space to a variable without freeing up at the last:

  • #include <alloca.h>
  • void *alloca(size_t size);

Return Value

A reference to the starting point of the assigned region is returned by the alloca() method. The script behaves in an undefinable way if the assignment results in a stack overflow.

Some Confirmations

POSIX.1-2001 does not provide this method. The alloca() method is known to have been present in 32V, 3BSD, PWB, PWB.2, and 4BSD. It is documented in 4.3BSD’s manual webpage. The GNU variant is used by Linux.

Explanation

The alloca() function depends on the compiling program, as well as the target system. When contrasted to using malloc(3) plus free, the use of it can make it more efficient in some scenarios (3). Under some circumstances, it may also make space deallocation in programs that employ longjmp(3) or siglongjmp easier (3). However, it is not instructed to consume it.

Suppose the memory allotted by alloca() is reserved inside the stack frame. If somehow the method return is skipped over by a function call to longjmp(3) or siglongjmp, the storage is instantly released (3). Free(3) memory that alloca() has allotted should not be attempted! Alloca() calls are often translated using an inline script by gcc(1).

Bugs

If the stack frame might not be expanded, there seems to be no fault message. Although, if the application tries to use the unassigned memory after one botched placement, it is qualified to obtain a SIGSEGV signal. Since this stack memory allocated by alloca() might display on the stack inside the midst of the region for the method parameters on several platforms, alloca() must not be used within the list of parameters of a method call.

Example 01

Let’s get started with our first examples of using the alloca() function of C. First, we need to create a C file using the touch instruction within the current working directory of the Kali Linux system. After that, you can try to open it within any editor of your choice to add a C script within it. We are using the Vim editor to open the “allocEx.c” file.


Within the empty file, we have added the following code. This code started with the “stdio.h” header to make use of the standard input and output stream in the program. The “alloca.h” header has been used to make use of the alloc() C function in this code. This code contains a single main() function that has been initializing an integer variable named “Number” allocated with the memory of size 4.

The “for” loop has been initialized and used to increment the value of the variable “Number” by 1 at each index. The next “for” loop has been used to display each index value of variable Number using the printf() function statement. This code has been completed here. We have saved it and it’s ready for compilation.


Now, we have been compiling this C file with the GCC compiler and created its object file named “allocEx.out” within the same working directory, i.e., “works”.


After that, we executed the newly made object file of this code within the “./” instruction. The output has been displaying a total of 4 values for the particular variable, i.e., size 4 memory allocation to the variable Number.


Let’s change the iteration number within the “for” loop, i.e., change 4 to 5. The following code has been updated. Let’s save it before compiling it.


We have compiled the modified code with the GCC compiler to override its object file, as well.


On execution, we have the segmentation fault. It’s because we have been initializing the variable size to 5 and using the “<” sign to iterate the variable “Number” up to 5 which are a total of 6 memory locations, i.e., should be a total of 5.

Example 02

Let’s take another example for assigning the periodically released memory to the pointer stack. We have been using the same headers with the addition of the “stdlib.h” header. The main() method has been set up with the declaration of some variables and a pointer. The printf and scanf statements are used to display and get the input number from the user while the alloca() function will be allocating the memory to the pointer variable “Number” using that particular number added by a user. If the memory assigned to the variable “Number is NULL”, i.e., no memory allocated, then the printf() will display some error message related to it. Plus the exit(0) function will terminate this program.

The “for” loop is using the printf function to display the current index number and asks you to input the value for a particular index using the scanf function. Regardless of the value you have added, it will be incremented by the index number and saved to the “sum” variable The printf() function will be display the “sum” of all the iterations.


We have compiled it and created its object file.


Executed its object file and it has been asking for the size of memory allocation, i.e., 4. Total of 4 values have been added by the user and summed up by the program and displayed below:

Conclusion

This is all about the use of the alloca() function to assign the free-up space to a particular variable, especially some stacks. You can see that we haven’t used the “free()“ function to free up the allocated space as we usually do in the malloc() function.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.