Do we need to build a structure?

Consider the following code:

struct Node { void* data; int ref; struct Node* next; }; typedef struct Node* NodePtr; 

I found that I get segfaults whenever I try to do something with NodePtr fields. For instance:.

 NodePtr node; node->ref = 1; 

So, I have allocated some space for NodePtr, and now it works fine. Why is this? I assume that since node is just a pointer, it has no memory for its fields.

So, I tried to initialize NodePtr:

 NodePtr node = { node->data = 0; node->next = NULL; node->ref = 0; }; 

And well, I got this error:

 error: expected รข}รข before รข;รข token 

This boils down to four questions:

  • If my guess is wrong, why doesn't it work if I don't use malloc ()?
  • Why is my initialization not working?
  • Will initializing memory struct a sentence on the stack and solve my problem?
  • If not, do I have an alternative to memory allocation for each structure I use?
+5
source share
3 answers

A struct can be assigned automatically, but you are using a pointer to a struct that will not allocate space for the struct destination. So the reason you get segfault.

The reason your initialization is incorrect is because you initialize the members of a struct , not a struct . Also you are doing it wrong.

There are two ways to initialize a struct :

  • Using a dedicated struct stack:

     struct example { int foo; }; int main() { struct example e; e.foo=1; } 
  • Using the heap allocated by struct with malloc() :

     struct example { int foo; }; int main() { struct example *e=malloc(sizeof(struct example)); e->foo=1; } 

Note that when you assign a value to the struct member from its pointer (the allocated struct heap), you should use ' -> ', but you should use the 'for the normal structure (allocated by the stack) . '.

+17
source

A pointer is not a structure. This is the number that tells C where the structure is in memory. Any variation of type NodePtr is essentially a number.

How does it make sense to set a variable of type NodePtr to a structure? The string is not a number!

When you declare it using a NodePtr node , it can be set to some undefined value, such as 0. You cannot access this memory, which leads to segfault. Instead, you can find some memory to use with malloc() and make this variable a point where its fields can be used.


To answer potrzebie's comment, it seems to work with strings, but it really is just syntactic sugar:

 #include <stdio.h> int main() { char *a = "Does this make sense?"; printf("%d\n", a); // %u is the correct one, but this is for illustrational purposes return 0; } > test.exe 4214884 
+2
source

Your assumption is correct: the pointer does not have memory for the object, which it should indicate by itself, you need to select it yourself.

In any case, as juanchopanza noted: you do not need a pointer and memory allocation if you are dealing with a local object.

Both methods:

 typedef struct Node { void* data; int ref; struct Node* next; } Node; typedef struct Node* NodePtr; int main() { NodePtr node = (NodePtr)malloc(sizeof(Node)); node->data = 0; node->next = 0; node->ref = 42; printf("%d", node->ref); Node obj = {0,42,0}; // this is not on the heap printf("%d", obj.ref); 

The other syntaxes you tried were not correct. Not even part of the language.

+1
source

Source: https://habr.com/ru/post/1204063/


All Articles