is that possible ?

chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
suppose we have the following code snippet:

struct node
{
int data;
struct node *next;
}
struct node * returnNewNode(struct node * head, int value)
--------------------------------------------------------------------
is it in someway possible the "ReturnSometing" function, to take the value and
create a new node ( non static or global) and return the pointer to that node ?

ps 1: head is the top of the list
oh and sorry about my poor english
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Sure. Use malloc. One presumes this is C, not C++.
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
if I use malloc like this :

struct node * create(struct node *head,int val)
{
struct node *ptr;
ptr = (struct node *) malloc(sizeof(struct node));
ptr->data = val;
ptr->next = NULL;


return ptr;
}

ptr now is a dangling pointer isn't it ??
DaWei's Avatar, Join Date: Dec 2006
Team Leader
"ptr" goes away, yes. It isn't a dangling pointer, it's a NONEXISTENT pointer. However, you RETURNED it's value to the calling code, so that code now has a copy of it. That presumes you called it something like this:

pMyNode = create (head, val);

pMyNode now holds the value that was previously held by 'ptr', which is now gone bye-bye. You must save these values, so that you can free them later. Normally, that would be done in the 'next' pointers, so one would begin at the head and free all memory associated with the 'next' pointers until a NULL pointer was found.
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
isn't that the same case ??
http://www.go4expert.com/showthread.php?t=3893
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
Quote:
pMyNode now holds the value that was previously held by 'ptr', .
Isn't the values tha ptr holds automatically destroyed when we exit the function ?
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Pay attention. "ptr" was destroyed. Before it was destroyed, its contents were transerred out of the function, via the return mechanism, to another variable that now contains the value returned by malloc, which points to the dynamic memory that was allocated. If you transfer a candy bar from your right hand to your left hand, then I chop off your right hand, you still have the candy bar.
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
I' m paying attension but I'm not seem to get it ..
{ char *buffer;

char *fillBuffer () {
char line[1000];
gets (line);
return line;
}

buffer = fillBuffer();}

Isn't that suppose to be a characteristic example of a dangling pointer.
Before "line" is destroyed, aren't its contents tranfserred out of the function, via the return mechanism ?
Sorry again if I insist but I must understand it well
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You're code is incomplete. It means nothing. There is no such thing as a 'dangling' pointer, despite all the protestations of various gurus that you might feel compelled to kneel before, and kiss the hems of their garments. There are valid pointers and invalid pointers. It baffles me that you cannot understand that valid information can be preserved, despite the death of its originator.
wrecker's Avatar, Join Date: Mar 2007
Go4Expert Member
I didnt understand, if there exist a pointer called Dangling pointer, what is it? How to recognise whether a pointer is dangling pointer or not. please give a detailed explanation. Thanks