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
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 ??
"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.
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.
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
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.
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
First thing first : If it baffles you to answer something then simply don't answer it ! And about my code, is not incomplete, is just a Code Snippet focused on what I'm interesting for.... Is just a simple question :::::: Code: #include <stdio.h> char *createstring() { char line[1000]; gets (line); return line; } int main() { char *buffer; buffer = createstring(); return 0; } You like that better ??? line points to 0xSomeAddressHavingData createstring() exits line points to 0xNothing buffer points to line points to nothing how you call this ??
First of all, if you can't hack a little criticism, you're getting into the wrong business. If you refuse to think, you're definitely getting into the wrong business. I call what you posted incorrect code. It also doesn't match your snippit, which makes your snippet no good for explaining the problem. The code that you show is full of woes. You cannot return the address of the array and do any good because THE ARRAY goes away. You have the address of where it was, which, because it is an autovariable, is useless information. When you use malloc, the array exists on the heap. The pointer to it is an autovariable, so you will lose THAT, but the array remains. If you return the value of the pointer to main, main now has the heap address stored in ITS autovariable. It doesn't matter that you lost the one in 'createstring'. Further, do not use 'gets'. It puts no constraint on how many characters you are willing to accept. If the user enters 1001, which is merely half of a full screen, your program is toast. Use 'fgets'. Your statement, 'line points to 0xNothing' is not correct. line points to precisely the memory that it pointed to before, but that memory NO LONGER CONTAINS LINE, or, at least, won't in the very near future. Get that data area from the heap, and it'll be there until you free it. I recommended that you study autovariables and scope. Clearly, you still need to do that. You also need to read this. There is a library function that performs your createstring function. It is called 'strdup'. Code: #include <stdio.h> char *createstring() { char *line = (char *) malloc (1000); if (line == NULL) return NULL; // ERROR! YOUR REQUEST WAS DENIED. return fgets (line, 1000, stdin); } int main() { char *buffer; buffer = createstring(); if (buffer == NULL) return 1;// EITHER MALLOC OR FGETS FAILED // Use the buffer ... ... free (buffer); return 0; }
I don't know If i ask clever questions but sure this one ->http://cboard.cprogramming.com/showthread.php?t=88784 is called a clever answer..
Yes, that's a good answer. You got the same answer here. It seems likely that you are going to pay no attention to either of them. You might want to get your hand fitted for a mop, because janitorial duties might well be your future. If you ever dropped a competing forum's link on MY forum, I would warn you once, then ban you.
Be sure that my future will be better than yours... At least I am not so rude like you. Oh... .and about that ban thing ............. I think I ban my self.... Bye !
A dangling pointer does not exist, pointers are valid or not. If a pointer is invalid, it means it does not point to a valid block of memory. Which in turn means you forgot to allocate memory for it or did not point it to a particular block of memory.
Oops I see there was a second page which I didn't read. I tried to reply to the last post of the previous page.