是什么导致此“存储空间不足的地址”错误?

Gre*_*ice 0 c malloc pointers sizeof

    struct ListNode {
        int val;
        struct ListNode *next;
    };


   struct ListNode* test = malloc(sizeof(struct ListNode*));

   test->val = 6;

   struct ListNode* lex = malloc(sizeof(struct ListNode*));

   test->next = lex;

   return test;
Run Code Online (Sandbox Code Playgroud)

此时我应该收到一个填充的结构。相反,我得到了这个:

   Line 14: Char 18: runtime                                                    
   error: store to address   
   0x602000000118 with      
   insufficient space for an 
   object of type 'struct ListNode 
   *' (solution.c)


   0x602000000118: note: pointer   
   points here

   be be be be  00 00 00 00 00 00 
   00 00  02 00 00 00 ff ff ff 02  
   08 00 00 20 01 00 80 70  be be 
   be be
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

Joh*_*136 7

您只是为 ListNode 指针而不是实际的 ListNode 分配空间。

尝试: struct ListNode* test = malloc(sizeof(struct ListNode));