简体   繁体   English

使用memcpy列表中的分段错误

[英]Segmentation fault in a list with memcpy

I'm trying to copy a list into another list with memcpy, but I'm getting a segmentation fault everytime I try to access the value I copied. 我正在尝试使用memcpy将列表复制到另一个列表中,但每次尝试访问我复制的值时,我都会遇到分段错误。

I've already tried moving pointers around, but the problem still occurs. 我已经尝试过移动指针,但问题仍然存在。 create_list creates a new head node for the list and returns it. create_list为列表创建一个新的头节点并返回它。 Here is some of the code: 以下是一些代码:

/* The n variable shows the number of elements in the list for the head */
struct list {
    union{
        void *data;
        struct {
            unsigned num;
            List *end;
        };
    };
    List *node;
};
List *
create_list()
{
    List *head;

    head = malloc(sizeof(List));
    if (head == NULL)
        return NULL;
    head->num = 0;
    head->end = NULL;
    head->node = NULL;

    return head;
}
int
cpy_list(List *l1, List **l2)
{
    List *iter;
    void *data_aux;

    *l2 = create_list();
    iter = l1->node;
    while (iter != l1->end) {
        memcpy(&data_aux, iter->data, sizeof(iter->data));
        //printf("data_aux: %s\n", data_aux);
        insert_list(*l2, data_aux, NULL);
        //printf("iter->data: %s\n", iter->data);
        iter = iter->node;
    }

    return 1;
}

void
print_list(List *head)
{
    List *iter;
    iter = head->node;
    printf("[");
    while (iter != head->end) {
        printf("\"%s\",", iter->data);
        iter = iter->node;
    }
    printf("\"%s\"", iter->data);
    printf("]");
    printf("\n");
}

int
main(int argc, char *argv[])
{

    List *l1, *l2;
    char *str[] = {"Test0", "Test1", "Test2", "Test3", "Test4"};
    void *data_aux;
    l1 = create_list();

    for (int k = 0; k < 5; k++) {
        insert_list(l1 ,str[k], NULL);
    }
    printf("l1: ");
    print_list(l1);

    cpy_list(l1, &l2);
    print_list(l2);

    return 0;
}

memcpy(&data_aux, iter->data, sizeof(iter->data));

I'll assume this is a typo problem (let me know if it is not and you intended to use it like that). 我会假设这是一个拼写错误的问题(如果不是,请告诉我,你打算像那样使用它)。 &data_aux will return the address of variable data_aux , not the address pointed by data_aux . &data_aux将返回变量data_aux的地址,而不是data_aux 指向的地址。 This code is likely causing a stack overflow as you are probably writing data beyond the boundaries of the local variable data_aux (which have the size of a pointer - 4 bytes on x86 or 8 bytes on x64). 此代码可能导致堆栈溢出,因为您可能正在写入超出局部变量data_aux边界的数据(其大小为指针 - x86上为4个字节或x64上为8个字节)。 If iter->data have a significant size you will corrupt the stack and have undefined behavior. 如果iter->data具有相当大的大小,则会破坏堆栈并具有未定义的行为。

What you probably want is to allocate a buffer to be pointed by data_aux . 你可能想要的是分配一个由data_aux指向的data_aux Something like: 就像是:

data_aux = malloc(sizeof(iter->data));

And then pass data_aux instead of &data_aux in your call to memcpy . 然后在调用memcpy传递data_aux而不是&data_aux

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

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