gpt4 book ai didi

c - 我做错了什么 - C 指针

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:34 24 4
gpt4 key购买 nike

<分区>

我已经定义了 struct Node 和 createNode 函数。

typedef struct {
int r, c;
int val;
struct Node* down, *right;
} Node;

typedef Node* NodePointer;

NodePointer createNode(int r,int c,int val) {
NodePointer p = (NodePointer)malloc(sizeof(NodePointer));
if (p != NULL) {
p->r = r;
p->c = c;
p->val = val;
p->down = p->right = NULL;
}
return p;
}

然后当我尝试在主函数中使用它时

int i = 0;
NodePointer* rows = (NodePointer*)malloc(50*sizeof(NodePointer));
for (i = 0; i < 20; i++) {
rows[i] = createNode(i,i+10,i*i);
}

它抛出运行时错误,但是当我像这样更改 for 循环时(更改在 for 循环中 - 只执行一次):

for (i = 0; i < 1; i++) {        
rows[i] = createNode(i,i+10,i*i);
}

它没有抛出错误。所以我问我做错了什么;错误:

Zad1: malloc.c:2374: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com