【问题标题】:how to do dynamic allocation in boundless array如何在无限数组中进行动态分配
【发布时间】:2020-03-19 16:24:23
【问题描述】:

嗯,我想改变我的结构的编写方式,目前我使用数组,我需要限制它的使用,但我想要一种方法来创建一个动态数组,它的大小是完成读取的大小,而不总是编辑数组值。

当前代码:

struct sr_flag {
    int value_flag;
};

struct er_time {
    int value_time;
};


struct se_option {
    struct sr_flag flag[50];
    struct er_time time[50];
};


struct read_funcs
    struct se_option *option;
    void (*option_func) (void);
    ...
}

struct read_funcs func_;
struct read_funcs *func;

int sr_flags(int i, int fg, int val) {

    if(i < 0)
        return 0;

    return func->option[i].flag[fg].value_flag = val;
}


void option_func(void) {
    struct se_option fnc;

    fnc.option = malloc(500 * sizeof(*(fnc.option)));
}

void read_fnc() {
    func = &func_;

    func->option = NULL;
    func->option_func = option_func;
}

我寻找一种方法来删除数组数量 [50],而不是每次执行 sr_flags 函数时都会提高限制

示例:sr_flags 函数执行 1x 如果执行 2x[1] /strong> 将是 [2]

我也考虑用 option_func 函数做同样的事情

我尝试使用以下更不成功

struct se_option {
    struct sr_flag *flag;
    struct er_time time[50];
};

int sr_flags(int i, int fg, int val) {

    if(i < 0)
        return 0;

    func->option[i].flag = malloc(1 * sizeof(*(func->option[i].flag)));

    return func->option[i].flag[fg].value_flag = val;
}

int main () {

    for(int i < 0; i < 10; i++)
        sr_flags(i, 1, 30);

    return 0;

}

【问题讨论】:

  • 您是否尝试将数组的大小每次迭代增加 1?
  • @Nina 是的,就是这样
  • 你能提供一个最小的可重现的例子吗? stackoverflow.com/help/minimal-reproducible-example
  • @Nina 我举了一个例子来说明我想要它的样子。
  • 看看我编辑的答案。

标签: c memory dynamic-memory-allocation allocation


【解决方案1】:

我不能 100% 确定您想要什么,但我认为您只想调用 realloc 并根据您提供的数量增加大小。这很容易做到,至于你想要的数组值我不确定,所以我只使用了占位符值。

#include <stdio.h>
#include <stdlib.h>
struct sr_flag {
    int value_flag;
};

struct er_time {
    int value_time;
};


struct se_option {
    struct sr_flag* flag;
    struct er_time* time;
};

void allocateflags(struct se_option* options, int size, int val){
    options->flag = realloc(options->flag, size*sizeof(struct sr_flag));
    struct sr_flag* flag = options->flag+size-1;
    flag->value_flag = val;
}
void allocatetime(struct se_option* options,int size, int val){

    options->time = realloc(options->time, size*sizeof(struct er_time));
    struct er_time* time = options->time+size-1;
    time->value_time = val;
}
void displayflagvalues(struct se_option* options,int size){
    for(int index = 0; index < size ; ++index){
        printf("flag: %i\n",options->flag[index].value_flag);
    }
}
void displaytimevalues(struct se_option* options, int size){
    for(int index = 0; index < size ; ++index){
        printf("time: %i\n",options->time[index].value_time);
    }
}
int main(){
    struct se_option options = {0};
    for(int index = 0; index < 10; ++index){
        allocateflags(&options, index,index);
        allocatetime(&options, index,index);
    }


    displayflagvalues(&options, 10);
    displaytimevalues(&options,10);
    return 0;
}

代码创建了一个 se_option 结构,其中 sr_flag 和 er_time 指针为空。然后有两个函数,一个是 allocateflags,另一个是 allocatetime,它们都使用您提供的大小调用 realloc。当您调用 realloc 时,所有先前的内存都被复制到新数组中。 free 也会被 realloc 自动调用。 这一步

struct sr_flag* flag = options->flag+size-1;
    flag->value_flag = val;
struct er_time* time = options->time+size-1;
    time->value_time = val;

有点多余,但它只是为了显示最新的数组可以保存该值。如果您了解指针算术,那么它所做的就是将指针递增到最后一个位置,然后减去 1 结构大小并设置该值。基本上在指针中设置最终数组的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多