return function - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: return function

  1. #16
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    @Lindley thanks for the code, but I do not understand what the function3 does?

    @VictorN now it prints random string, but again I don't know what's the difference with the code of laserlight.

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: return function

    It makes absolutely no difference what function3() does. None. It can do anything. The point is, the act of calling it overwrites whatever was on the stack below main(), which will wipe out the data function2() had put in its local "test" variable (if it isn't gone already) and demonstrates the problem.

  3. #18
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    wait. str is getting the returning value of function2. So now the value is stored. Why it is cleared when it is printeD?

  4. #19
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: return function

    Quote Originally Posted by StGuru
    str is getting the returning value of function2. So now the value is stored. Why it is cleared when it is printeD?
    Lindley is taking advantage of something implementation dependent, i.e., it is an attempt to trigger the use of the memory previously allocated to the destroyed array. This attempt happens to work.

    Really, just keep it simple: do not return a pointer to an object that will be destroyed when the function returns. It is as simple as that, even if by chance you find that it works.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #20
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,354

    Re: return function

    Quote Originally Posted by StGuru
    wait. str is getting the returning value of function2. So now the value is stored. Why it is cleared when it is printeD?
    This "value" is a pointer to some memory location where a character array was placed before function2 has returned. And at the time you are printing this memory was reallocated and reoccupied by some other data!
    Victor Nijegorodov

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center