简体   繁体   English

strcpy分割错误

[英]strcpy segmentation fault

i have a list of quads and they have a label starting from 1. the backpatch is taking a list structure which points at some quads. 我有一个四边形列表,并且它们的标签从1开始。backpatch采用了指向某些四边形的列表结构。 i want backpatch to update those quads, putting z on the char * fourth and then emptying l so i can put other quads later.I get seg.fault in backpatch's strcpy although I have allocated memory for the char * z and char * fourth . 我想backpatch更新那些四边形,将z放在char *上,然后清空l,这样我以后可以放其他四边形。尽管我已经为char * zchar * fourth four分配了内存,但在backpatch的strcpy seg.fault了seg.fault。 Does anybody know why does that happens? 有人知道为什么会这样吗?

struct quad {
char *label; //5
char *first; //30
char *second;
char *third;
char *fourth;
struct quad *next;
};

struct list {
    struct quad *quadlist;
    struct list *nextlist;
};


void backpatch(struct list *l, char * z) {
struct list *temp = (struct list*) malloc(sizeof (struct list));
temp->nextlist = (struct list*) malloc(sizeof (struct list));
temp->quadlist = (struct quad*) malloc(sizeof (struct quad));
temp->quadlist->fourth = (char*)malloc(30 * sizeof (char));
l->nextlist = (struct list*) malloc(sizeof (struct list));
temp = l;
//z=(char*)malloc(sizeof(struct list))
while (temp->nextlist != NULL) {

    strcpy(temp->quadlist->fourth, z);
    temp = l->nextlist;
}
strcpy(temp->quadlist->fourth, z);

free(temp);
free(l);

}

even if i only keep the 即使我只保留

while (l->nextlist != NULL) {

strcpy(l->quadlist->fourth, z);
l = l->nextlist;
}
strcpy(l->quadlist->fourth, z);
free(l);

part, its also seg.fault... 部分,它也存在段错误...

from the comment: 从评论:

//z=(char*)malloc(sizeof(struct list))

it looks like you allocated memory for z but it's become a non-NULL terminated string. 看起来您为z分配了内存,但它已成为一个非NULL终止的字符串。 So strcpy keeps copying and eventually starts reading past the end of z. 因此,strcpy会继续复制,并最终开始读取z的末尾。 Check what is in z befor strcpy 检查z befor strcpy中的内容

You create temp and allocate memory for it and its components but you the throw it away when you do temp = l; 您创建temp并为其及其组件分配内存,但是当temp = l;时,您将其丢弃temp = l; So all of these calls leak memory as you never use what you allocate or free it. 因此,所有这些调用都会泄漏内存,因为您从不使用分配或释放的内存。 The two calls to free at the end of the function are wrong, temp no longer points to the memory you allocated and you don't free the other memory allocated inside the temp structure. 函数末尾的两次free调用是错误的,temp不再指向您分配的内存,并且您不释放temp结构内分配的其他内存。 Freeing l destroys the head of the list your trying to update - I'm pretty sure that;s not what you intended. 释放l会破坏您尝试更新的列表的开头-我很确定那不是您想要的。

When you do temp = l; 当你做temp = l; you loose your refence to all the memory you just allocated, and now l and temp point to the same 'struct list' 您松开了对刚分配的所有内存的引用,现在l和temp指向相同的“结构列表”

You have struct quad *next; 你有struct quad *next; in the quad structure but as your list structure links the quads into a list what is this for? 在四边形结构中,但是当您的列表结构将四边形链接到列表中时,这是做什么用的?

Your while loop looks wrong to me - temp->nextlist is the result of yours call to malloc in l->nextlist = (struct list*) malloc(sizeof (struct list)); 你的while循环对我来说似乎是错误的temp->nextlist是您在l->nextlist = (struct list*) malloc(sizeof (struct list));调用malloc的结果l->nextlist = (struct list*) malloc(sizeof (struct list)); but you never initialise the structure so l->nextlist->nextlist will be garbage and could point anywhere. 但是您永远不会初始化结构,因此l-> nextlist-> nextlist将是垃圾,可能指向任何地方。

I'd suggest that you stop looking at backpatch and write a function that displays your data, doing that will have two effects, you'll know that the structure is correct and you'll have a better understanding of how the structure links together. 我建议您不要再看backpatch,而是编写一个显示数据的函数,这样做会产生两种效果,您将知道结构是正确的,并且将更好地了解结构如何链接在一起。 You need to get your head around how the structures and pointers combine to make a linked list like this. 您需要了解结构和指针如何组合以创建这样的链接列表。 Google for C Linked List Implementation and read some existing code that implements linked lists. Google for C链表实现,并阅读一些实现链表的现有代码。

What is your code actually trying to do? 您的代码实际上想做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM