c - 内存分配/重新分配问题

标签 c memory-management c99

我刚刚解决了我正在编写的当前程序的内存分配问题,但我对解决它所要做的事情并不满意。

在我的程序中,我正在构建一个结构数组,每次我想向其添加结构时,都会为该数组重新分配空间。这是我的结构的通用版本以及将结构添加到数组的函数:

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 returned was not allocate的运行时错误。 example_array 位于地址 0x100100080 处,我要分配的第一个结构位于地址 0x100100090 处,经过几次重新分配后, example_array 将耗尽空间。

最后,回答我的问题。我通过为我的 example_array 分配比我需要的更多的空间来解决这个问题,但这看起来非常不优雅。有更好的方法吗?

**编辑**

好吧,从大多数响应来看,我不应该使用指向指针的指针。因此,我尝试采用稍微不同的方式,混合 pmgcrypto 的响应。现在这是我的代码:

/* 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 );
}

再次感谢您的帮助。

最佳答案

realloc 将 NULL 作为指针值非常好......并在这种情况下执行 malloc

*p = NULL;
new = realloc(p, 42); /* same as new = malloc(42); */
if (!new) { /* error */ }
p = new;

所以,忘记calloc(无论如何,您将立即覆盖后面的零),随意将指针初始化为NULL和realloc

int main(void) {
    Example *example_array = NULL;
    add_struct_to_array(&example_array, &ex_array_size, "name", function, 1);
    /* ... */
    free(example_array);
}

关于c - 内存分配/重新分配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3742105/

相关文章:

c - 在 c99 中使用 open_memstream

c - Visual Studio 2012 中的 (char[32]){0} 有什么问题吗?

c - 使用数组的第一个元素对数组进行排序

c++ - 我的 C++ vector 元素被删除

c - 如何在 C99 中迭代 char*?

c - 关于 _Bool 使用的 VS2013 编译问题

C程序按钮长按持续计数

c++ - 循环遍历 Makefile 中的文件

python - 在pandas中,如何将一列float32值转换为float16值?

c - 我该如何处理这个段错误