我刚刚解决了我正在编写的程序中的内存分配问题,但我对我所做的修复工作并不满意。
在我的程序中,我正在建立一个结构体数组,并在每次想要向其添加结构体时重新分配数组的空间。以下是我的结构体的通用版本以及向数组添加结构体的函数:
在我的无知中,我认为用大小为0的数组进行分配是可以的,因为它最初是空的,之后我可以添加结构体。显然,这不起作用,我会得到关于“error for object 0x100100080: pointer being reallocated was not allocated” 的运行时错误。example_array位于地址0x100100080,并且我将分配给第一个结构体的地址为0x100100090,在几次重新分配后,example_array会用完空间。
所以,最终的问题是什么?我通过为example_array分配比需要更多的空间来解决这个问题,但这似乎非常不优雅。有更好的方法吗?
**编辑**
好的,从大多数回答的外观来看,我不应该使用指向指针的指针。因此,我正在尝试稍微不同的方法,混合pmg和crypto的响应。现在,这是我的代码:
感谢您的帮助。再次感谢。
在我的程序中,我正在建立一个结构体数组,并在每次想要向其添加结构体时重新分配数组的空间。以下是我的结构体的通用版本以及向数组添加结构体的函数:
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)
{
// first, make a new struct
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 );
}
// now, realloc the array of structs and add the new member to it
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;
}
这是我将调用函数的地方(请注意,我最初分配了结构体数组,因为问题就出在那里)。
#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 );
/* Do stuff here */
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分配比需要更多的空间来解决这个问题,但这似乎非常不优雅。有更好的方法吗?
**编辑**
好的,从大多数回答的外观来看,我不应该使用指向指针的指针。因此,我正在尝试稍微不同的方法,混合pmg和crypto的响应。现在,这是我的代码:
/* example_struct.h */
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;
}
/* main.c */
...
...
#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
也没有问题,但是我无法访问数组中的结构体。
/* main.c */
...
...
#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 ) /* Segfault */
example_free( example_array );
}
感谢您的帮助。再次感谢。