Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   is that possible ? (http://www.go4expert.com/forums/is-that-possible-t3898/)

chopin17 16Apr2007 19:56

is that possible ?
 
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 16Apr2007 21:13

Re: is that possible ?
 
Sure. Use malloc. One presumes this is C, not C++.

chopin17 16Apr2007 21:35

Re: is that possible ?
 
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 ?? :confused:

DaWei 16Apr2007 22:54

Re: is that possible ?
 
"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 16Apr2007 23:08

Re: is that possible ?
 
isn't that the same case ??
http://www.go4expert.com/showthread.php?t=3893

chopin17 16Apr2007 23:13

Re: is that possible ?
 
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 17Apr2007 00:28

Re: is that possible ?
 
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 17Apr2007 01:29

Re: is that possible ?
 
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 17Apr2007 04:35

Re: is that possible ?
 
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 17Apr2007 10:50

Re: is that possible ?
 
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


All times are GMT +5.5. The time now is 14:27.