Need some help understanding pointers and memory in C

I am writing a little code for the class, but since I have no experience in C, I'm a little unsure that I actually wrote the code. In particular, what memory looks like. Here are the relevant bits:

typedef struct listnode *Node; typedef struct listnode { void *data; Node next; Node previous; } Listnode; typedef struct listhead *LIST; typedef struct listhead { int size; Node first; Node last; Node current; } Listhead; #define HALLOCSIZE 50 static LIST hallocbuf[HALLOCSIZE]; static LIST *hallocp = hallocbuf; LIST *CreateList() { if(hallocbuf + HALLOCSIZE - hallocp >= 1) { LIST temp; temp->size = 0; temp->first = NULL; temp->last = NULL; temp->current = NULL; *hallocp = temp; return hallocp; }else return NULL; } 

So my question is: in the CreateList function, how does the program allocate memory for temp? And the code *hallocp = temp copies the temporary LIST into the hallocbuf array? I try to have all my LIST structures sit in allocated memory for hallocbuf. Is that what I do? I don't understand a bit how typedef, structs, and pointers work.

Thanks!

+1
source share
3 answers

So my question is: in the CreateList function, how does the program allocate memory for temp?

It is not, it is a problem. He should do something like this:

 temp = malloc(sizeof(Listhead)); 

And the code * hallocp = temp copies the temporary LIST to the hallocbuf array?

It copies the pointer that was saved in temp to the first hallocbuf element (assuming that hallocp not changed anywhere and still has the value for which it was initialized, pointing to hallocbuf[0] ).

As a rule, it is usually not recommended to hide the fact that LIST and Node are pointers behind typedefs. This is much clearer when memory should be allocated from freed, if it is obvious which variables are pointers, and the presence of an explicit * in the variable declaration makes this clear.

+4
source

temp is allocated in the space used for objects with "automatic storage duration" - this is usually done at run time, but you don't need to know the details. The space is freed when the block in which it was allocated exits (in your case, when you press return ).

Line *hallocp = temp; really copies the temp value to the memory pointed to by hallocp , which is equal to hallocbuf[0] .

The problem is that temp is just a pointer, and it does not point to anything. This is called a dangling pointer. This means that when you try to access what it points to, you have an error. This occurs on the following lines:

 temp->size = 0; temp->first = NULL; temp->last = NULL; temp->current = NULL; 

You cannot have your structures sit in the memory allocated for hallocbuf because it has no place for structures - it is just an array of pointers, not an array of structures.

+3
source

If the list were

 typedef struct listhead LIST; 

and you got access to temp

 temp.size = 0; ... 

then

 *hallocp++ = temp; 

would use hallocp as a pointer to the hallocbuf buffer and place the newly initialized element there. Not the best way to do this. You can replace temp with

 hallocp->size = 0; ... ++hallocp; 
0
source

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


All Articles