Returning pointer from function question

This is a discussion on Returning pointer from function question within the C++ Programming forums, part of the General Programming Boards category; I have this function: Code: Matrix* EffectParameter::GetMatrix() { Matrix temp; effect->GetEffect()->GetMatrix(handle, &temp); return &temp; } The question is, will the ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Returning pointer from function question

    I have this function:

    Code:
    Matrix* EffectParameter::GetMatrix()
    {
    	Matrix temp;
    	effect->GetEffect()->GetMatrix(handle, &temp);
    	return &temp;
    }

    The question is, will the pointer returned by the function point to the valid memory. Or will the temp variable fall out of the function's scope and risk getting over written and than making the pointer point to random memory?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    No, that code returns a dangling pointer -- a pointer to a variable on the stack which has gone out of scope, sort of like SlyMaelstrom. (I think it was him/her.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    So than I'm guessing it will point to valid memory for a short pariod. I tested it by using it right after the function returned but I guess it was an invalid pointer after all.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Sorry for the double post I forgot to ask my next question...

    Code:
    Matrix& EffectParameter::GetMatrix()
    {
    	Matrix temp;
    	effect->GetEffect()->GetMatrix(handle, &temp);
    	return temp;
    }
    That will work right? I think I was told to do it this way before.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Well, here's what happens.

    As the function returns, the stack pointer moves down, shifting past the local variable. The local variable goes out of scope. Yet, you can still access the information (at risk of segfaulting), since the data hasn't yet been overwritten. It's like this. This code actually prints 5, usually.
    Code:
    void one() {
        int x = 5;
    }
    
    void two() {
        int x;
        std::cout << x;
    }
    
    int main() {
        one();
        two();
    }
    The value of one::x is set to 5, and so when the stack pointer moves up again for two(), since one() and two() have the same signature, two::x is allocated the same memory as one::x.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ok ya that is a very good explanation. I think I understand what your talking about.

    I posted my next question just above your last post if you wouldn't mind helping me out there. Thanks.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Same problem as before. You are returning a reference to it instead of a pointer. Now if you create a proper copy constructor(assuming you have pointer datatypes in matrix). You can just return a Matrix.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    And if the Matrix is expensive to copy, then you can pass in a reference parameter and just use that instead of returning anything.

    >> So than I'm guessing it will point to valid memory for a short pariod.
    The object will be valid until the function ends. The pointer then will only be valid until the function ends, but since the function ends after the return statement that "short period" is really short, and of course means that you can never use that pointer.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ok ya thanks, that's all my questions. I'll just make the user pass in a Matrix pointer that I can fill up than.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You could have the user pass a reference instead of a pointer so that you don't have to worry about a null pointer. Using a reference helps indicate that the parameter is mandatory, and the syntax is cleaner as well.

    Some people prefer a pointer in this case (Stroustrup) to make it clear that the Matrix is being modified, although you could change the function name so that it is obvious (e.g. FillMatrix or something like that).

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Oh that's weird. I never thought of doing that nor do I see that very often. I find it odd that directx doesn't do that.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    DirectX doesn't do it because DirectX must maintain compatibility to non-C++ languages, and that's easier to do with pointers than with references.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ahh ok ya now that makes sence. Well I just read that references are usualy implanted with pointers by the compiler. But I'm going to use references to help avoid null pointers as someone said here.

    Thanks for all of your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

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