I just finished the task of allocating memory with the current program that I am writing, but I am not happy with what I needed to do to fix it.
In my program, I created an array of structures, redistributing the space for the array every time I wanted to add a structure to it. Here is a general version of my structure and a function that would add the structure to the array:
typedef struct Example {
const char* name;
int (*func)(int, int);
int bool_switch;
}
int add_struct_to_array( Example **example_array, int *ex_array_size, int name, int (*func)(int, int), int bool_switch)
{
Example *new_example = (Example *) calloc( 1, sizeof( Example ) );
if( new_example != NULL ) {
new_example->name = name;
new_example->func = func;
new_example->bool_switch = bool_switch;
( *ex_array_size )++;
} else {
printf( "Errror allocating %s\n", name );
exit( -1 );
}
Example **temp_example_array = ( Example** )realloc( example_array, ( *ex_array_size ) * sizeof( Example* ) );
if( temp_example_array != NULL ) {
example_array = temp_example_array;
example_array[ ( *ex_array_size ) - 1 ] = new_example;
} else {
printf( "Reallocation failed\n" )
exit( -1 );
}
return 0;
}
And here I will call the functions (note how I initially allocate an array of structures because there was a problem)
#include "example_struct.h"
int main( int argc, char **argv )
{
int ex_array_size = 0;
Example **example_array = ( Example** )calloc( 0, sizeof( Example* ) );
add_struct_to_array( example_array, &ex_array_size, "name", &function, 1 );
...
...
add_struct_to_array( example_array, &ex_array_size, "other_name", &other_func, 0 );
example_array_free( example_array );
return 0;
}
, -, , 0 , , . , , error for object 0x100100080: pointer being reallocated was not allocated. example_array 0x100100080, , , 0x100100090, example_array .
, , . , example_array, , . ?
** EDIT **
, . , , pmg crypto. :
int add_struct_to_array( Example *example_array, int *ex_array_size, int name, int (*func)(int, int), int bool_switch)
{
Example temp_example_array = realloc( example_array, ( ( *ex_array_size ) + 1 ) * sizeof( Example ) );
if( temp_example_array != NULL ) {
example_array = temp_example_array;
Example new_example;
new_example.name = name;
new_example.func = func;
new_example.bool_switch = bool_switch;
example_array[ ( *ex_array_size ) ] = new_example;
++( *ex_array_size );
} else {
fprintf( stderr, "Error reallocating for %s", name );
exit( -1 );
}
return 0;
}
...
...
#include "example_struct.h"
int main( int argc, char **argv )
{
int ex_array_size = 0;
Example *example_array = NULL;
add_struct_to_array( example_array, &ex_array_size, "name", &func, 1 );
add_struct_to_array( ... );
...
add_struct_to_array( example_array, &ex_array_size, "other name", &other_func, 0 );
example_free( example_array );
}
realloc , .
...
...
#include "example_struct.h"
int main( int argc, char **argv )
{
int ex_array_size = 0;
Example *example_array = NULL;
add_struct_to_array( example_array, &ex_array_size, "name", &func, 1 );
add_struct_to_array( ... );
...
add_struct_to_array( example_array, &ex_array_size, "other name", &other_func, 0 );
printf( "%s\n", example_array[0].name )
example_free( example_array );
}
.