I am using C for this project to create a bunch of dynamically created arrays.
They are generated as explained [here][1]
This works fine.
'
However, when I try the code below to free up the array(s), I get a "segmentation error( Core Duped)".
I am using the listing below to create a "my_struct".
typedef struct
{
uint32_t** block;
uint32_t** valid;
uint8_t block_size; //Bytes per block
uint8_t level;
}my_struct;
my_struct L1, L2;
Thereafter, at a later point, the pointers "block" and "valid" are allocated dynamic memory using the function below where they are successively passed as parameters (arr_ptr):
void Generate2DArray (uint32_t** arr_ptr, uint32_t row, uint32_t column)
{
uint32_t* temp;
uint32_t i = 0;
uint32_t j = 0;
arr_ptr = (uint32_t**)malloc(row* sizeof(uint32_t*));
if(arr_ptr == NULL)
{
printf("MALLOC 1 FAILS \n ");
}
temp = (uint32_t*)malloc(row* column* sizeof(uint32_t));
if(temp == NULL)
{
printf("MALLOC 2 FAILS \n ");
}
for (i = 0; i < row; i++)
{
arr_ptr[i] = temp + (i * column);
}
}
All this works fine so far.
Now, when I try to "free" the memory near the end of the code, using the listing below, I get an error message saying "Segmentation Fault (Core dumped)"
void FreeMemory(uint32_t** arr_ptr, uint32_t rows)
{
uint32_t i = 0;
for ( i = 0; i < rows; i++)
{
free(arr_ptr[i]);
}
free(arr_ptr);
}
Please provide any suggestions as to where am I going wrong. I have gone through this post as well and my code seems to be compliant with it.
Thanks!!