简体   繁体   English

malloc(sizeof(struct Node))与malloc(sizeof(nodeptr))的不同行为

[英]Different behaviour for malloc(sizeof(struct Node)) vs malloc(sizeof(nodeptr))

I was trying to allocate memory using malloc, I am not able to understand why I am getting a different result for these two malloc calls. 我试图使用malloc分配内存,但我无法理解为什么我为这两个malloc调用得到不同的结果。

The line below gives me wrong result even though with gdb I see the data is getting the correct value assigned. 即使使用gdb,下面的行也给我错误的结果,即使我看到数据正在分配正确的值。

  • nodeptr n = malloc(sizeof(nodeptr)); nodeptr n = malloc(sizeof(nodeptr));

    Value head->data: '!' 值头->数据:“!”
    Value head->eq->data: '' 值的head-> eq-> data:''

And when I do this get the correct result: 当我这样做时,将得到正确的结果:

  • nodeptr n = malloc(sizeof(struct Node)); nodeptr n = malloc(sizeof(结构节点));

    Value head->data: 'w' 值头->数据:“ w”
    Value head->eq->data: 'X' 值头-> eq-> data:'X'

I followed this post, I think I am doing it correctly. 我关注了这篇文章,我认为我做得正确。
In both ways, while allocation I get the same amount of memory, just I see the different results in the end. 在两种方式下,虽然分配时我得到的内存量相同,但最终却看到了不同的结果。

typedef struct Node
{
    struct Node *left, *right, *eq;
    char data;
    bool isEnd;
} *nodeptr;

nodeptr newNode(const char c) {
    nodeptr n = malloc(sizeof(nodeptr));
    // nodeptr n = malloc(sizeof(struct Node));
    n->data = c;
    n->left = NULL;
    n->right = NULL;
    n->left = NULL;
    n->isEnd = false;

    return n;
}

void insert(nodeptr *node, const char *str) {

    if (*node == NULL) {
        *node = newNode(*str);
    }
    nodeptr pCrawl = *node;

    if(pCrawl->data < *str) {
        insert(&pCrawl->right, str);
    } else if (pCrawl->data > *str) {
        insert(&pCrawl->left, str);
    } else {
        if(*(str+1)) {
            insert(&pCrawl->eq, str + 1);
        } else {
            pCrawl->isEnd = true;
        }
    }
}

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

    const char* const strs[5]= {
        "w.",
    };
    nodeptr head = NULL;
    for(int i = 0; i<1; i++) {
        insert(&head, strs[i]);
    }
    return 0;
    printf("Value head->data: \'%c\'\n", head->data);
    printf("Value head->eq->data: \'%c\'\n", head->eq->data);
}

The two different versions aren't allocating the same amount of memory. 两种不同的版本没有分配相同数量的内存。 sizeof(nodeptr) is the size of a pointer and sizeof(struct Node) is the size of your struct . sizeof(nodeptr)是一个指针和大小sizeof(struct Node)是你的结构的大小。 These are not the same things and they're not the same size. 这些东西不一样,大小也不一样。 On my computer these values are 8 and 32 . 在我的计算机上,这些值为832

You want to use: 您要使用:

nodeptr n = malloc(sizeof(struct Node));

or perhaps: 也许:

nodeptr n = malloc(sizeof(*n)); // size of the type that n points too

sizeof(nodeptr) == sizeof(struct Node*) != sizeof(struct Node) == sizeof(*nodeptr) sizeof(nodeptr) == sizeof(struct Node*) != sizeof(struct Node) == sizeof(*nodeptr)

  • sizeof(nodeptr) will always be the size of a pointer (like 8 bytes on a 64-bit CPU) sizeof(nodeptr)始终是指针的大小(例如64位CPU上的8个字节)
  • sizeof(struct Node) refers to the struct contents sizeof(struct Node)引用结构内容
  • sizeof(*nodeptr) is equivalent to sizeof(struct Node) with the extra dereference operator in there. sizeof(*nodeptr)等同于sizeof(struct Node)其中包含额外的解除引用运算符。

The reason it may appear to "work" (not segfault) is that malloc suballocates from a larger block of heap memory. 它看起来“起作用”(不是segfault)的原因是malloc从更大的堆内存块中进行子分配。 However, the code is writing out-of-bounds of the requested allocation, which can eventually lead to heap corruption or segfaults at some point. 但是,代码正在写超出所请求分配的界限,这最终可能在某些时候导致堆损坏或段错误。

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

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