简体   繁体   中英

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. 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 . 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. So strcpy keeps copying and eventually starts reading past the end of z. Check what is in 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; 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. Freeing l destroys the head of the list your trying to update - I'm pretty sure that;s not what you intended.

When you do temp = l; you loose your refence to all the memory you just allocated, and now l and temp point to the same 'struct list'

You have 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)); but you never initialise the structure so l->nextlist->nextlist will be garbage and could point anywhere.

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. 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.

What is your code actually trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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