在结构内重新分配数组

时间:2018-08-27 20:42:11

标签: c dynamic-arrays realloc

我正在尝试编写一个使用realloc()来扩展结构实例中指向的数组的函数,但是我似乎无法使其正常工作。

我的代码的相关部分是:

struct data_t {
  int data_size;
  uint16_t *data;
};

void extend_data(data_t container, uint16_t value) {
    // adds an additional uint16_t to the array of DATA, updates its internal
    // variables, and initialises the new uint to VALUE.

    int len_data = sizeof(*(container->data)) / sizeof(uint16_t);
    printf("LENGTH OF DATA: %d\n", len_data);

    container->data = realloc(container->data, sizeof(*(container->data))+sizeof(uint16_t));
    container->data_size++;
    container->data[container->data_size-1] = value;

    len_data = sizeof(*(container->data)) / sizeof(uint16_t);
    printf("LENGTH OF DATA: %d\n", len_data);
    printf("data_size: %d\n", container->data_size);

    return;
}

有人能看到这是什么问题吗?

4 个答案:

答案 0 :(得分:2)

修改

正如R. Sahu指出的那样,container并不是此函数的指针-当您说代码“不起作用”时,我以为您是在说您不是在扩展数组,而是什么?你在这里写的甚至都不会编译

您确定已正确复制此代码?如果是这样,“不起作用”是否表示您遇到编译时错误,运行时错误或只是意外输出?

如果您已经复制了编写的代码,那么您要做的第一件事就是将函数原型更改为

void extend_data(data_t *container, uint16_t value) {

并确保您将 pointer 传递给data_t类型,否则更新不会反映在调用代码中。

原始

在线

container->data = realloc(container->data, sizeof(*(container->data))+sizeof(uint16_t));

sizeof(*(container->data))的值为sizeof (uint16_t)container->data是指向uint16_t的指针,而不是sizeof的数组; /** * Don't assign the result of a realloc call back to the original * pointer - if the call fails, realloc will return NULL and you'll * lose the reference to your original buffer. Assign the result to * a temporary, then after making sure the temporary is not NULL, * assign that back to your original pointer. */ uint16_t *tmp = realloc(container-data, sizeof *container->data * (container->data_size + 1) ); if ( tmp ) { /** * Only add to container->data and update the value of container->data_size * if the realloc call succeeded. */ container->data = tmp; container->data[container->data_size++] = value; } 将为您提供指针对象的大小,而不是分配的元素数。您要执行的操作类似于以下内容:

locals().keys()

答案 1 :(得分:1)

您没有正确计算新尺寸。考虑一下:

typedef struct {
    size_t  size;
    int    *data;
} int_array;
#define  INT_ARRAY_INIT  { 0, NULL}

void int_array_resize(int_array *const  array,
                      const size_t      newsize)
{
    if (!array) {
        fprintf(stderr, "int_array_resize(): NULL int_array.\n");
        exit(EXIT_FAILURE);
    }
    if (!newsize) {
        free(array->data);
        array->data = 0;
        array->size = 0;
    } else
    if (newsize != array->size) {
        void *temp;

        temp = realloc(array->data, newsize * sizeof array->data[0]);
        if (!temp) {
            fprintf(stderr, "int_array_resize(): Out of memory.\n");
            exit(EXIT_FAILURE);
        }
        array->data = temp;
        array->size = newsize;
    }
}

/* int_array  my_array = INT_ARRAY_INIT;
      is equivalent to
   int_array  my_array;
   int_array_init(&my_array);
*/
void int_array_init(int_array *const array)
{
    if (array) {
        array->size = 0;
        array->data = NULL;
    }
}

void int_array_free(int_array *const array)
{
    if (array) {
        free(array->data);
        array->size = 0;
        array->data = NULL;
    }
}

关键点是newsize * sizeof array->data[0]。这是newsize类型的array->data[0]元素所需的字符数。 malloc()realloc()均以字符为单位。

如果使用int_array my_array = INT_ARRAY_INIT;初始化该类型的新结构,则只需调用int_array_resize()即可调整其大小。 (realloc(NULL, size)等效于malloc(size)free(NULL)是安全的,什么也不做。)

int_array_init()int_array_free()只是用于初始化和释放此类数组的辅助函数。


就我个人而言,每当我动态调整数组大小时,我都会保留分配的大小(size)和使用的大小(used):

typedef struct {
    size_t  size;   /* Number of elements allocated for */
    size_t  used;   /* Number of elements used */
    int    *data;
} int_array;
#define  INT_ARRAY_INIT { 0, 0, NULL }

确保至少可以添加need个元素的函数非常有用。为了避免不必要的重新分配,该函数实现了一个策略,该策略计算要分配的新大小,以“浪费”(已分配但未使用)的内存量与可能缓慢的realloc()调用次数之间取得平衡:

void int_array_need(int_array *const  array,
                    const size_t      need)
{
    size_t  size;
    void   *data;

    if (!array) {
        fprintf(stderr, "int_array_need(): NULL int_array.\n");
        exit(EXIT_FAILURE);
    }

    /* Large enough already? */
    if (array->size >= array->used + need)
        return;

    /* Start with the minimum size. */
    size = array->used + need;

    /* Apply growth/reallocation policy. This is mine. */
    if (size < 256)
        size = (size | 15) + 1;
    else
    if (size < 2097152)
        size = (3 * size) / 2;
    else
        size = (size | 1048575) + 1048577 - 8;

    /* TODO: Verify (size * sizeof array->data[0]) does not overflow. */

    data = realloc(array->data, size * sizeof array->data[0]);
    if (!data) {
        /* Fallback: Try minimum allocation. */
        size = array->used + need;
        data = realloc(array->data, size * sizeof array->data[0]);
    }
    if (!data) {
        fprintf(stderr, "int_array_need(): Out of memory.\n");
        exit(EXIT_FAILURE);
    }

    array->data = data;
    array->size = size;
}

关于应该使用哪种重新分配策略,有很多意见,但这实际上取决于用例。

余额中有三件事:realloc()呼叫的数量,因为它们可能很“慢”;如果增长了不同的数组并需要许多realloc()调用,则内存碎片;和已分配但未使用的内存量。

我上面的政策试图一次做很多事情。对于小分配(最多256个元素),它会将大小四舍五入到下一个16的倍数。这是我试图在用于小数组的内存和不多的realloc()调用之间取得很好的平衡。

对于更大的分配,将50%添加到大小中。这样可以减少realloc()调用的次数,同时将已分配但未使用/不需要的内存保持在50%以下。

对于非常大的分配,当您有2 21 个元素或更多时,大小会四舍五入到2 20 的下一个倍数,减去几个元素。这样可以将已分配但未使用的元素数限制为2 21 或200万个元素。

(为什么要减少几个元素?因为它不会损害任何系统,并且在某些系统上可能会有所帮助。某些系统,包括某些操作系统和配置上的x86-64(64位Intel / AMD) ,支持大(“大”)页面,这些页面在某些方面可能比普通页面更有效。如果使用它们来满足分配要求,我想避免这样一种情况:分配一个超大页面只是为了满足几个字节的需要C库内部需要分配元数据。)

答案 2 :(得分:0)

void extend_data(data_t container, ...

在函数container中,指针不是指针,而是值本身传递的结构,因此您不能使用->运算符。

当您处理传递的结构的本地副本时,重新分配的内存将丢失,并且在函数返回时将丢失。

sizeof(*(container.data)) / sizeof(uint16_t)

它将始终为1,因为*(uint16_t *) / sizeof(uint16_t)始终为1。

为什么:data成员是uint16_t的指针。 *data的类型为uint16_t

sizeof是在编译过程中计算的,而不是运行时计算的,它不返回malloc分配的内存量。

答案 3 :(得分:0)

似乎您没有正确使用sizeof。在您的结构中,您定义了uint16_t 指针,而不是数组。 uint16_t*数据类型的大小是系统上指针的大小。如果希望能够精确调整其大小,则需要将分配的内存的大小与指针一起存储。看来您已经有了data_size的字段。您的示例可能可以固定为

// I was unsure of the typedef-ing happening with data_t so I made it more explicit in this example
typedef struct {
    int data_size;
    uint16_t* data;
} data_t;

void extend_data(data_t* container, uint16_t value) {
    // adds an additional uint16_t to the array of DATA, updates its internal
    // variables, and initialises the new uint to VALUE.

    // CURRENT LENGTH OF DATA
    int len_data = container->data_size * sizeof(uint16_t);
    printf("LENGTH OF DATA: %d\n", len_data);

    uint16_t* tmp = realloc(container->data, (container->data_size + 1) * sizeof(uint16_t));
    if (tmp) {
        // realloc could fail and return false.
        // If this is not handled it could overwrite the pointer in `container` and cause a memory leak
        container->data = tmp;
        container->data_size++;
        container->data[container->data_size-1] = value;
    } else {
        // Handle allocation failure
    }

    len_data = container->data_size * sizeof(uint16_t);
    printf("LENGTH OF DATA: %d\n", len_data);
    printf("data_size: %d\n", container->data_size);

    return;
}
相关问题