Can someone please explain ?

This is a discussion on Can someone please explain ? within the C Programming forums, part of the General Programming Boards category; First forgive me about my english ... suppose we have the following code snippet Code: #include struct node { int ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Can someone please explain ?

    First forgive me about my english ...

    suppose we have the following code snippet
    Code:
    #include 
    
    struct node 
    {
        int data;
        struct node *next;
    }
    struct node *create(int val)
    {
        struct node *nd;
        nd = (struct node *) malloc(sizeof(struct node));
        nd->data = val;
        nd->next = NULL;
        return nd;
    }
    
    int main()
    {
        struct node *x;
        x = create(10);
        return 0;
    }
    Alright my question 1 now :
    "nd" points to a data area created in the "create" function
    when "create" function exists this "data area" is destroyed Or no ???
    If no why ??
    because of malloc OR because is coppied to "x" in "main" ???

    And question 2 :
    In this code snippet how we could modify the code, so the
    "nd" to be a dangling pointer ???

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The address of the allocated variable is returned to main and stuffed in a pointer. Now you can use (dereference) that pointer to get to what it points to. Variables allocated with malloc don't lose scope when it returns, because otherwise you'd never be able to allocate memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    If you want a really technical answer, then it's one of those questions that look simple but really aren't.

    > because of malloc OR because is coppied to "x" in "main" ???
    People could explain in so many words that both of these are wrong. Consider what malloc does: it returns a pointer to at least as much memory you asked for. But does the pointer it returned last forever? No. A pointer respects scope like any other object. For your code, it means that yes nd is destroyed. nd doesn't exist outside of create and isn't accessable, but the memory (from malloc) it pointed at should be.

    When you cannot access memory that is still being used, that is a memory leak. If you forgot to store the address to memory that create would return in x, that would be a memory leak too. Nothing is really copied, and malloc doesn't do anything special to stop memory from leaking, but when people talk about such things, they usually just want you to do the Right Thing. Plus a technical answer gets very complicated if you bring up return value optimization.

    So the Right Thing is to always have your pointers holding a value, preferably a value that matches the pointer's type. That's all there is to it really.

    > how we could modify the code, so the
    "nd" to be a dangling pointer ???
    Code:
    struct node *create( int val ) {
       struct node notapointer = { val, NULL };
       return &notapointer;
    }
    If you point to the return value of this function, that pointer will dangle.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    ..

    So the x pointer in main is valid because of malloc.

    so if we modify the code like this
    Code:
    struct node *create(int val)
    {
         struct node *ndPtr;
         struct node nd;
         nd.data = val;
         nd.next = NULL;
         ndPtr = &nd;
         return ndPtr;
    }
    in this case would "x" (in function main ) be valid ????

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    .

    sorry citizen i didn't see your post before my second reply.
    So if I get it right you say that
    - malloc allocates memory and "holds" that area until free is called ?
    - And if I hadn't use 'x' in main to point to that arrea then I had a memory leak ??

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    > - malloc allocates memory and "holds" that area until free is called ?
    Yes. The best way to "hold" memory that malloc allocates is to store it in a pointer. And malloc returns NULL if it fails. You will have to watch for that and deal with it. If you add further abstractions, like you did with create, those abstractions will inherit those issues.

    - And if I hadn't use 'x' in main to point to that arrea then I had a memory leak ??
    Yes.

    And regarding your other code:
    No it's not okay. Returning a pointer to automatic memory is bad. Returning a pointer to dynamic memory from malloc is okay.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    .

    Thanks Citizen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 11:36 AM
  2. Replies: 4
    Last Post: 11-19-2002, 08:18 PM
  3. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 08:53 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21