malloc和结构中的指针

时间:2013-05-14 18:19:53

标签: c pointers

我有以下C代码:

typedef struct DListNode_ {
    void    *data;
    struct DListNode_ *prev;
    struct DListNode_ *next;
} DListNode;


typedef struct DList_ {
    int size;
    DListNode  *tail;
    DListNode  *head;
} DList;

void insert(DList * list, DListNode * element, int data) {
    DListNode * new_element = (DListNode *)malloc(sizeof(DListNode));
    new_element->data = &data;
    if (list->head==NULL) {
        list->head=list->tail=new_element;
        list->size++;
        return;
    }
    if(element == NULL) {
        // handle size==0?
        new_element->next=list->head;
        list->head->prev=new_element;
        list->head=new_element;
        list->size++;
    } else {
        printf("Not yet implemented!\n");
    }
}

void printNodes(DList *list) {
    DListNode * pointer = list->head;
    if (pointer!=NULL) {
        int v= *((int*)pointer->data);
        printf("Node has value: %d\n", v);
        while (pointer->next != NULL) {
            v = *((int*)pointer->data);
            printf("Node has value: %d\n", v);
            pointer=pointer->next;
        }
    }
}

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

    int e0 = 23;
    int e1 = 7;
    int e2 = 11;
    DList *list = (DList *)malloc(sizeof(DList));
    initList(list);
    assert(count(list)==0);
    insert(list, NULL, e0);
    assert(count(list)==1);

    insert(list,NULL, e1);
    assert(count(list)==2);

    insert(list,NULL, e2);
    assert(count(list)==3);
    printNodes(list);

    return 0;
}

我有一些问题:

  1. 执行 DListNode * new_element =(DListNode *)malloc(sizeof(DListNode)); 还为数据,prev,next指针分配空间,还是我手动需要在每个指针上调用malloc那些指针?
  2. 当我在每个节点中打印数据指针的内容时,即使我插入23,7和11并将数据指针设置为int的地址,它们都具有值 3 :* * new_element-> data =& data; **。
  3. (已订购C简介教科书)

    编辑:

    现在插入一个指向数据的void指针:

    // Insert data as the new head
    void insert(DList *list, DListNode *element, void *data) {
        DListNode *new_element = malloc(sizeof(DListNode));
        new_element->data = data;
        if (list->head==NULL) {
            list->head=list->tail=new_element;
            list->size++;
            return;
        }
        if(element == NULL) {
            new_element->next=list->head;
            list->head->prev=new_element;
            list->head=new_element;
            list->size++;
        } else {
            printf("Not yet implemented!\n");
        }
    }
    

    我主要做的是:

    int main(int argc, const char * argv[])
    {
        int i0=7;
        int *ip0 = malloc(sizeof(int));
        ip0 = &i0;
    
        int i1=8;
        int *ip1 = malloc(sizeof(int));
        ip1 = &i1;
    
        int *ip2 = malloc(sizeof(int));
        int i2=44;
        ip2 = &i2;
    
        DList *list = malloc(sizeof(DList));
        initList(list);
        // create some nodes
        assert(count(list)==0);
        insert(list, NULL, ip0);
        assert(count(list)==1);
    
        insert(list,NULL, ip1);
        assert(count(list)==2);
    
        insert(list,NULL, ip2);
        assert(count(list)==3);
        printNodes(list);
    
        return 0;
    }
    

    输出:

    Node has value: 44
    Node has value: 44
    Node has value: 8
    

    但它应该是:

    Node has value: 44
    Node has value: 8
    Node has value: 7
    

2 个答案:

答案 0 :(得分:3)

  1. malloc(sizeof(DListNode))仅为一个DListNode分配空间,根据定义,它由void*和两个DListNode指针组成。但它并没有初始化那些指针。

  2. 您要将data参数的地址分配给insert。这是指向insert返回后无效的临时指针。程序的行为未定义。简单的解决方案是将void *data替换为int data

答案 1 :(得分:0)

  1. 您需要手动将这些指针设置为指向malloc的位置。没有它,它们将指向不是DListNode大小的空间。

  2. 不要将数据作为指针。只需将数据设为int(它将自动分配),然后设置data = data(传递给insert的数据)。