[英]Append function for C with dynamic memory allocation
Following this question , I'm trying to modify the code provided in this blog post to create an append
function using dynamic memory allocation. 在这个问题之后 ,我正在尝试修改此博客文章中提供的代码,以使用动态内存分配创建
append
功能。 This is what I have so far: 这是我到目前为止:
#include <stdio.h>
#include <stdlib.h>
typedef struct intlist_ {
int size;
int* list;
} intlist;
void append(intlist* arr, int value){
realloc((*arr).list, sizeof((*arr).list) + sizeof(int));
(*arr).size = (*arr).size + 1;
(*arr).list[(*arr).size -1] = value;
}
int main() {
intlist arr;
arr.size = 4;
arr.list = malloc(arr.size * sizeof(int));
arr.list[0] = 0;
arr.list[1] = 5;
arr.list[2] = 3;
arr.list[3] = 64;
append(&arr, 12);
for (int ii = 0; ii < arr.size; ii++)
printf("%d, ", arr.list[ii]);
free(arr.list);
return 0;
}
However the result I get is wrong: 但是我得到的结果是错误的:
clang version 7.0.0-3~ubuntu0.18.04.1 (tags/RELEASE_700/final) main.c:10:3: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result] realloc((*arr).list, sizeof((*arr).list) + sizeof(int));
clang version 7.0.0-3~ubuntu0.18.04.1(tags / RELEASE_700 / final)main.c:10:3:警告:忽略用'warn_unused_result'属性声明的函数的返回值[-Wunused-result] realloc(( * arr)。list,sizeof((* arr).list)+ sizeof(int)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 warning generated.
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~生成了~~~ 1个警告。 0, 5, 3, 64, 0, 5, 3, 64, 12,
0,5,3,64,0,5,3,64,12,
I'm using this online compiler for testing where you can also see the latest versions of the above code. 我正在使用这个在线编译器进行测试,您还可以在其中查看上述代码的最新版本。 I would appreciate if you could help me know where is my mistake and how I can solve it.
如果你能帮助我知道我的错误在哪里以及如何解决它,我将不胜感激。 Thanks for your support in advance.
感谢您的支持。
PS You may a final version of the code here in this Gist . PS你可以在这个Gist中找到代码的最终版本。
Close, but: 关闭,但是:
sizeof((*arr).list)
wont give you the size of the array. sizeof((*arr).list)
不会给你数组的大小。 Instead it will give you the size of a int*
. int*
的大小。 realloc
invalidates the original pointer, and continuing to use it is undefined behaviour. realloc
使原始指针无效,并继续使用它是未定义的行为。 Use the returned value instead. So change your realloc line to use the stored list size instead, and update the pointer with the return value: 因此,更改您的realloc行以使用存储的列表大小,并使用返回值更新指针:
(*arr).list = realloc((*arr).list, ((*arr).size + 1) * sizeof(int));
Couple of other tips: 其他一些提示:
ptr->
is the same as (*ptr).
ptr->
与(*ptr).
相同(*ptr).
but easier to read. (*arr).size
and (*arr).list
to arr->size
and arr->list
. (*arr).size
和(*arr).list
更改为arr->size
和arr->list
。 realloc
, like its brethren, is not guaranteed to succeed. realloc
并不能保证成功。 You should check the return value for null
to catch errors. null
的返回值以捕获错误。 Wrong size allocated. 分配错误的大小。
sizeof((*arr).list)
is the size of a pointer, not int
. sizeof((*arr).list)
是指针的大小,而不是int
。
Return value not used. 未使用的返回值。
realloc()
returns the new pointer. realloc()
返回新指针。
No NULL
check 没有
NULL
检查
Rather than use the error prone ptr = some_alloc(sizeof(type) * n)
, use ptr = some_alloc(sizeof *ptr * n)
而不是使用容易出错的
ptr = some_alloc(sizeof(type) * n)
,使用ptr = some_alloc(sizeof *ptr * n)
void append(intlist *arr, int value){
int *new_ptr = realloc(arr->list, sizeof *(arr->list) * (arr->size + 1u));
if (new_ptr == NULL) {
fprintf(stderr, "Out of memory\n");
exit (EXIT_FAILURE);
}
arr->list = new_ptr;
arr->list[arr->size] = value;
arr->size++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.