1
votes

What happens after a function call when you have a pointer from one pointer struct point to another struct that has allocated memory? I asked a question earlier about structs that lead me to asking this question: scope of struct pointers in functions

For example:

struct example{
//variables and pointers
struct clip *next;
}*Head

and then i have a pointer of datatype struct in a function:

struct example *newNode=malloc(sizeof(struct example));

And in the same function i link the first node(the head) to the second node(the new node):

Head->next=newNode;

Does the linking/pointing still hold after the function exits? I'm not sure if that makes sense but when you add a new node at the end of your linkedlist you have to go through the linkedlist in the first place to see where it ends(next pointer = NULL).

For example:

void insert_at_end(){
//We have a pointer cp that goes through the linked list, initially cp->next points to `null so we create a newnode right away`
//struct example cp and newnode gets malloc'd here

if(Head != NULL){
cp=Head;
while(cp->next !=NULL){
cp=cp->next;
}
cp->next=newNode;
else{

//We link the head to the newNode `because we don't want to change the head for each new node added.`
head=newNode;
}
}

but after each newNode added at the end of the list we exit the function so what happens when we re-enter the function and go through the linkedlist to see where it ended? How does it know what cp->next points to?

2
Code it, compile it with all warnings and debugging info (e.g. gcc -Wall -g on Linux), and step by step with the debugger (gdb on Linux), displaying the relevant pointers, to find out. Write on a chalkboard what is happening. Read several good C programming books. You need to understand what the heap is, and what malloc and free are doing.Basile Starynkevitch
not really helpful. I know my question may not be worded the best but essentially i am asking this question because i can't draw it out due to not knowing what happens to pointers after a function exits. Specifically when the cp->next points to the memory location of the next node does it get saved somehow after a function exists? Otherwise how else can we go through a linkedlist?user2122810

2 Answers

1
votes

each newNode added at the end of the list we exit the function so what happens when we re-enter the function and go through the linkedlist to see where it ended?

Your newnode is added to the end of the list. This pointer is a part of a list and is not local to your function. So, when you re-enter the function the previously added node is still there.

How does it know what cp->next points to?

It knows where that points as the location is stored in your list.


On dynamic allocation, the memory allocated stays allocated till it is explicitly deleted. So, the existence of your data is independent of the pointer, or function.

0
votes

One thing you should remember is pointers are always pass by reference and not the pass by value. So as long as you are not releasing the memory hold by pointers, they remains intact (Be careful with this). So you can always refer to the node you have added to the linked list when you reenter the function.

But you should release all the allocated pointers, when your program exits or if you don't need them.