I have two very simple recursive C programs. I was checking the stack frame size assigned by the system to each recursive frame. But I came across something I did not understand.
When I create a local array with size 5 in the recursive function, then the system assigns 48 bytes to each frame.
When I create a pointer and assign the same amount of memory with
size*sizeof()
, then the system assigns 32 bytes to each frame.
The way I check is, I compile my C code into assembly and see the bytes allocated. My guess for the difference is that malloc
assigns from heap and normal array declaration assigns from the stack. So I am thinking these two memory parts might have different procedures?
I was expecting the memory allocated to be the same but they are different.
I call them from the main function.
void RecursiveFunction( int n )
{
int *point;
point = (int *)malloc(sizeof(int)*5);
if ( n > 1)
RecursiveFunction( --n );
return;
}
and
#include <stdio.h>
#include <stdlib.h>
void RecursiveFunction( int n )
{
int arr[5];
if ( recursion_times > 1)
RecursiveFunction( --n );
return;
}