is that possible ?

Discussion in 'C' started by chopin17, Apr 16, 2007.

  1. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Sure. Use malloc. One presumes this is C, not C++.
     
  3. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    "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.
     
  5. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
  6. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Isn't the values tha ptr holds automatically destroyed when we exit the function ?
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  8. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  10. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [comment]Just to add one offtopic comment add the code block when having codes in the post[/comment]
     
  12. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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 ??
     
  13. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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;
    }
    
     
  14. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
  15. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  16. chopin17

    chopin17 New Member

    Joined:
    Apr 16, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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 ............. :eek:
    I think I ban my self....

    Bye ! :)
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    We would prefer banning the forum also. :rolleyes:
     
  18. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    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.
     
  19. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice