am trying to free memory allocated in a user defined function. Am planning on running the code on an embedded device, STM32F303k8 which has 64Kb of Flash and 16KB of SRAM. I havent tried out the code but i fear its not going to do what it¨s supposed to do. Its going to run out of memory because of failure to deallocate the memory assigned
I have tried to free the memory in my custom program called split. However, it doesnt even compile and always crashes at the free() function.
//This happens somewhere in the main function
// str_gprmc is a string thats trying to be split
// "$GPRMC,130133.00,A,5741.6029848,N,01158.3855831,E,11.522,170.0,270319"
for (int k = 0; k < ARRAY_SIZE(str_gprmc); k++)
{
char **arr = NULL;
// Now do the splitting please
split(str_gprmc[k], ',', &arr);
}´
// and the split function
int split(const char *ptr_original_string, char delimiter, char ***ptr_main_array)
{
// This variable holds the number of times we think we shall iterate through our array once its split
int count = 1;
// This variable holds the number of characters we have to iterate through for each split string
int split_string_len = 1;
// This variable helps us iterate through collections
int i = 0;
// Points to the first character of the whole string
char *ptrTemp_string_holder;
// Points to the first character of a split string from the main string
char *t;
ptrTemp_string_holder = ptr_original_string;
// First we count the number of times the delimiter is occurring in the string we want to split
// Iterate through the string until we reach either the Courage Return character CR : '\r', the Line Feed LF : '\n' or the NULL : '\0'
while (*ptrTemp_string_holder != '\0')
{
if (*ptrTemp_string_holder == delimiter)
count++;
ptrTemp_string_holder++;
}
// allocate size in memory for our array. Size of a character is 1 byte * count
*ptr_main_array = (char**)malloc(sizeof(char*) * count);
if (*ptr_main_array == NULL) {
exit(1);
}
ptrTemp_string_holder = ptr_original_string;
// Now start iterating through the whole unsplit string as long as we're not at the end
while (*ptrTemp_string_holder != '\0')
{
// If the pointer points to a delimiter, i.e a comma, that means we are starting to read a new string
if (*ptrTemp_string_holder == delimiter)
{
// Now allocate a memory size for a pointer to a pointer of the new string to be built
(*ptr_main_array)[i] = (char*)malloc(sizeof(char) * split_string_len);
// If its null, like some GPRMC or GPHDT results that come back empty, just exit and return back to main
if ((*ptr_main_array)[i] == NULL)
{
exit(1);
}
// Reset the token length and just move the hell on
split_string_len = 0;
i++;
}
ptrTemp_string_holder++;
split_string_len++;
}
// If we are not at a delimiter however, we just allocate a size based on our token length to a pointer of a pointer
// Or if you want, call it a pointer to an array
(*ptr_main_array)[i] = (char*)malloc(sizeof(char) * split_string_len);
// If for some unknown reason it was null, just stop the crap and return back to main...after all we got a shitty GPS device
if ((*ptr_main_array)[i] == NULL) exit(1);
i = 0;
ptrTemp_string_holder = ptr_original_string;
t = ((*ptr_main_array)[i]);
// Now that we got what we need, we rebuild back everything to formulate a pointer to a pointer of character strings
// I think then the rest is straight forward
while (*ptrTemp_string_holder != '\0')
{
if (*ptrTemp_string_holder != delimiter && *ptrTemp_string_holder != '\0')
{
*t = *ptrTemp_string_holder;
t++;
}
else
{
*t = '\0';
i++;
t = ((*ptr_main_array)[i]);
}
ptrTemp_string_holder++;
}
// Free the space that was allocated to this pointer
free(ptr_main_array);
// We return back the number of times we need to iterate to get the split components of the original string
return count;
}