在函数内部使用 realloc 来增加数组
Using realloc inside a function for increasing an array
我正在尝试在函数内部调用 realloc。我第一次调用该函数似乎有效,但我第二次调用它失败了。
void insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
}
int main(){
struct laptop* arr = malloc(sizeof(struct laptop));
int len = 0;
int *plen = &len;
insert(arr, make_laptop("Dell", "XPS", "New", 500), plen);
display(arr[0]);
insert(arr, make_laptop("Macbook", "Pro", "New", 500), plen);
display(arr[1]);
return 0;
}
我的其余代码供参考:
struct laptop{
char *brand;
char *model;
char *condition;
float price;
};
struct laptop make_laptop(char *brand, char *model, char *condition, float price){
struct laptop new_laptop;
new_laptop.price = price;
new_laptop.brand = malloc(strlen(brand)+1);
strcpy(new_laptop.brand, brand);
new_laptop.model = malloc(strlen(brand)+1);
strcpy(new_laptop.model, model);
new_laptop.condition = malloc(strlen(condition)+1);
strcpy(new_laptop.condition, condition);
return new_laptop;
}
void display(struct laptop laptop){
printf("%s %s %s %.2f", laptop.brand, laptop.model, laptop.condition, laptop.price);
}
这里到底发生了什么?我有哪些解决此问题的选择?
对于初学者来说,这个内存分配在 main
struct laptop* arr = malloc(sizeof(struct laptop));
是多余的。你应该只写
struct laptop *arr = NULL;
函数接受指针arr
的值
void insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
}
即函数处理用作参数的原始指针值的副本。
要么通过引用传递指针定义函数至少像
void insert(struct laptop **arr, struct laptop new_laptop, int* plen){
*arr = realloc( (arr, (sizeof(struct laptop)*(*plen+1)));
( *arr )[*plen] = new_laptop;
(*plen)++;
}
在这种情况下,函数将被调用为
insert(&arr, make_laptop("Dell", "XPS", "New", 500), plen);
或return来自函数的新指针,如
struct laptop * insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
return arr;
}
并且该函数将被调用为
arr = insert(arr, make_laptop("Dell", "XPS", "New", 500), plen);
注意在这样的语句中使用同一个指针是不安全的
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
函数一般可以return一个空指针。结果,先前分配的内存地址将丢失。
你应该写例如
struct laptop *tmp = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
if ( tmp != NULL )
{
arr = tmp;
//...
}
我正在尝试在函数内部调用 realloc。我第一次调用该函数似乎有效,但我第二次调用它失败了。
void insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
}
int main(){
struct laptop* arr = malloc(sizeof(struct laptop));
int len = 0;
int *plen = &len;
insert(arr, make_laptop("Dell", "XPS", "New", 500), plen);
display(arr[0]);
insert(arr, make_laptop("Macbook", "Pro", "New", 500), plen);
display(arr[1]);
return 0;
}
我的其余代码供参考:
struct laptop{
char *brand;
char *model;
char *condition;
float price;
};
struct laptop make_laptop(char *brand, char *model, char *condition, float price){
struct laptop new_laptop;
new_laptop.price = price;
new_laptop.brand = malloc(strlen(brand)+1);
strcpy(new_laptop.brand, brand);
new_laptop.model = malloc(strlen(brand)+1);
strcpy(new_laptop.model, model);
new_laptop.condition = malloc(strlen(condition)+1);
strcpy(new_laptop.condition, condition);
return new_laptop;
}
void display(struct laptop laptop){
printf("%s %s %s %.2f", laptop.brand, laptop.model, laptop.condition, laptop.price);
}
这里到底发生了什么?我有哪些解决此问题的选择?
对于初学者来说,这个内存分配在 main
struct laptop* arr = malloc(sizeof(struct laptop));
是多余的。你应该只写
struct laptop *arr = NULL;
函数接受指针arr
的值
void insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
}
即函数处理用作参数的原始指针值的副本。
要么通过引用传递指针定义函数至少像
void insert(struct laptop **arr, struct laptop new_laptop, int* plen){
*arr = realloc( (arr, (sizeof(struct laptop)*(*plen+1)));
( *arr )[*plen] = new_laptop;
(*plen)++;
}
在这种情况下,函数将被调用为
insert(&arr, make_laptop("Dell", "XPS", "New", 500), plen);
或return来自函数的新指针,如
struct laptop * insert(struct laptop *arr, struct laptop new_laptop, int* plen){
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
arr[*plen] = new_laptop;
(*plen)++;
return arr;
}
并且该函数将被调用为
arr = insert(arr, make_laptop("Dell", "XPS", "New", 500), plen);
注意在这样的语句中使用同一个指针是不安全的
arr = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
函数一般可以return一个空指针。结果,先前分配的内存地址将丢失。
你应该写例如
struct laptop *tmp = realloc(arr, (sizeof(struct laptop)*(*plen+1)));
if ( tmp != NULL )
{
arr = tmp;
//...
}