Thread: Need to allocate?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Need to allocate?

    If I have a pointer to a struct and I call a function which returns a pointer to a struct that already has memory allocated to it, do I need to allocate the memory for the original variable? consider the follwoing pseudocode...

    Code:
    main function {
      structure pointer s_ptr;
    
      s_ptr = some_function()
    }
    
    structure pointer some_function {
      structure pointer s_ptr_temp;
    
      s_ptr_temp = malloc(yadda yadda);
    
      return s_ptr_temp;
    }
    is this ok?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it is okay, but somewhere before dereferencing the pointer you should check that malloc() did not return a null pointer, and somewhere after using the pointer you should free().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you only need to allocate memory for an address once. then you can have as many pointers as you want pointing to the address.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    what about this?

    Code:
    int main() {
      struct mystruct *ptr = malloc(sizeof(mystruct));
      struct mystruct * ptr2;
    
      ptr2 = ptr1;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Bladactania View Post
    what about this?
    What about it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As Meldreth just noted, "you can have as many pointers as you want pointing to the address".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    ...but you may only free() the corresponding memory exactly once.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    ...but you may only free() the corresponding memory exactly once.

    Greets,
    Philip
    And, don't use any of the other pointers after the memory has been freed!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Why can't you allocate a 2D array like this?
    By rudyman in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2008, 01:47 PM
  3. Allocate memory question
    By h3ro in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2007, 01:55 PM
  4. When to Allocate or Not to Allocate
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2005, 11:32 PM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM