Click here to Skip to main content
15,886,026 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
peterchen24-Jul-09 8:37
peterchen24-Jul-09 8:37 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
transoft24-Jul-09 5:04
transoft24-Jul-09 5:04 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
peterchen24-Jul-09 5:09
peterchen24-Jul-09 5:09 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
transoft24-Jul-09 5:27
transoft24-Jul-09 5:27 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
peterchen24-Jul-09 8:31
peterchen24-Jul-09 8:31 
AnswerRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
«_Superman_»24-Jul-09 3:54
professional«_Superman_»24-Jul-09 3:54 
AnswerRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
paolosh24-Jul-09 4:22
paolosh24-Jul-09 4:22 
GeneralRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
Emilio Garavaglia24-Jul-09 7:50
Emilio Garavaglia24-Jul-09 7:50 
No, you're not o the right path.
You code leaks memory. Let me expaly why:
void test()
{ foo(); }

literally means this:

  • a space wide enough to store a MyClass is leaved on the stack (That's what Myclass foo() means to the compiler)
  • the actual code address is pushed on stack, than
  • a jump to the beginning of foo is executed.
  • at thet point
  • a space wide enough to store a pointer is leaved on stack. (That's what MyClass* p requires)

  • new MyClass() is executed, that means:

    • a bunch of bytes enouh to store MyClass are taken from the heap
    • The MyClass::Myclass() constructor is called
    • The address of the allocated memory is copied to p

  • now, we are at return *p that means:
  • a copy of the value pointed by p (that's what *p means) is copied into the space previously left on the stack by calling MyClass(const MyClass&). That's what return If you did not declare it that's not important, the compiler generates it implicitly.

Now there are two distinct copy of MyClass: one taken from the heap whose address is stored in p and one that is stored temporarily on the stack containing a copy of it.
After the return,

  • the p pointer is destroyed by unrolling the stack. The value it store (the address of the heap containing the first Myclass) is lost forever, but the object is still there (hence the leak)
  • a jump is executed to the address stored in the stack, thus causing the execution to return after the call.
  • Now we are back to test().
  • No actions are required on the return value, hence it is destroyed and the stack unrolled

  • Now you have destroyed the temporary copy. The original object is still there.

    In general every time you call new, you've to guess where and when there is a corresponding delete that will be called.

    if you want to prperly debug all this, yu have to explicitly declare all what the compiler will elsewhere do implicitly: that is

    class MyClass
    {
    public:
      MyClass() {} ///< default constructor
      MyClass(const MyClass&) {} ///< copy contructor
      MyClass& operator=(const MyClass&) { return *this; } ///< assignment
      ~MyClass() {} ///< destructor
    };

    Now set a breakpoint at all of those functions, and try to debug, and you'll see funny things.


    2 bugs found.
    > recompile ...
    65534 bugs found.
    D'Oh! | :doh:


AnswerRe: Returning large objects from a function without unnecessarily invoking the constructor ! Pin
Nemanja Trifunovic24-Jul-09 8:58
Nemanja Trifunovic24-Jul-09 8:58 
Questionflickering issues with GDI+ Pin
Adassus24-Jul-09 3:26
Adassus24-Jul-09 3:26 
AnswerRe: flickering issues with GDI+ Pin
Iain Clarke, Warrior Programmer24-Jul-09 3:35
Iain Clarke, Warrior Programmer24-Jul-09 3:35 
AnswerRe: flickering issues with GDI+ Pin
Randor 24-Jul-09 9:12
professional Randor 24-Jul-09 9:12 
QuestionRemote Desktop Access by using client/server architecture Pin
Madhu_Rani24-Jul-09 3:20
Madhu_Rani24-Jul-09 3:20 
QuestionRe: Remote Desktop Access by using client/server architecture Pin
Moak25-Jul-09 0:04
Moak25-Jul-09 0:04 
AnswerRe: Remote Desktop Access by using client/server architecture Pin
Bacon Ultimate Cheeseburger25-Jul-09 16:20
Bacon Ultimate Cheeseburger25-Jul-09 16:20 
GeneralRe: Remote Desktop Access by using client/server architecture Pin
Madhu_Rani26-Jul-09 20:10
Madhu_Rani26-Jul-09 20:10 
GeneralRe: Remote Desktop Access by using client/server architecture Pin
Bacon Ultimate Cheeseburger29-Jul-09 18:35
Bacon Ultimate Cheeseburger29-Jul-09 18:35 
GeneralRe: Remote Desktop Access by using client/server architecture Pin
Madhu_Rani29-Jul-09 19:46
Madhu_Rani29-Jul-09 19:46 
GeneralRe: Remote Desktop Access by using client/server architecture Pin
Bacon Ultimate Cheeseburger29-Jul-09 20:40
Bacon Ultimate Cheeseburger29-Jul-09 20:40 
GeneralRe: Remote Desktop Access by using client/server architecture Pin
Madhu_Rani30-Jul-09 3:55
Madhu_Rani30-Jul-09 3:55 
QuestionMenu deletion Pin
transoft24-Jul-09 3:13
transoft24-Jul-09 3:13 
AnswerRe: Menu deletion Pin
«_Superman_»24-Jul-09 4:04
professional«_Superman_»24-Jul-09 4:04 
GeneralRe: Menu deletion Pin
transoft24-Jul-09 4:21
transoft24-Jul-09 4:21 
Questiongetting table names from MS Access (.mdb file) in MFC Pin
suthakar5624-Jul-09 2:54
suthakar5624-Jul-09 2:54 
QuestionRe: getting table names from MS Access (.mdb file) in MFC Pin
Rajesh R Subramanian24-Jul-09 2:57
professionalRajesh R Subramanian24-Jul-09 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.