Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:21+00:00 2026-05-11T18:55:21+00:00

when ever a function has a object passed by value it uses either copy

  • 0

when ever a function has a object passed by value it uses either copy constructor or bit wise copy to create a temporary to place on stack to use inside the function,How about some object returned from function ?

//just a sample code to support the qn
rnObj somefunction()
{
return rnObj();
}

and also explain how the return value is taken to the called function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:55:21+00:00Added an answer on May 11, 2026 at 6:55 pm

    As can be judged by the other answers – the compiler can optimize this.

    A concrete example generated using MSVC, to explain how this is possible (as asked in one of the comments) –

    Take a class –

    class AClass
    {
    public:
       AClass( int Data1, int Data2, int Data3 );
    
       int GetData1();
    
    private:
       int Data1;
       int Data2;
       int Data3;
    };
    

    With the following trivial implementation –

    AClass::AClass( int Data1, int Data2, int Data3 )
    {
        this->Data1 = Data1;
        this->Data2 = Data2;
        this->Data3 = Data3;
    }
    
    int AClass::GetData1()
    {
        return Data1;
    }
    

    And the following calling code –

    AClass Func( int Data1, int Data2, int Data3 )
    {
        return AClass( Data1, Data2, Data3 );
    }
    
    int main()
    {
        AClass TheClass = Func( 10, 20, 30 );
        printf( "%d", TheClass.GetData1() );
    }
    

    (the printf() added just to make sure the compiler doesn’t optimize everything away…).
    In a non-optimized code, we would expect Func() to create a local AClass on its stack, construct it there and copy it as its return variable.

    However, the generated assembly actually looks like (removing unneeded lines) –

    _TEXT SEGMENT
    ___$ReturnUdt$ = 8   ; size = 4
    _Data1$ = 12         ; size = 4
    _Data2$ = 16         ; size = 4
    _Data3$ = 20         ; size = 4
    
    mov     eax, DWORD PTR _Data3$[esp-4]
    mov     ecx, DWORD PTR _Data2$[esp-4]
    mov     edx, DWORD PTR _Data1$[esp-4]
    push    esi
    mov     esi, DWORD PTR ___$ReturnUdt$[esp]
    push    eax
    push    ecx
    push    edx
    mov     ecx, esi
    call    ??0AClass@@QAE@HHH@Z    ; AClass::AClass
    mov     eax, esi
    pop     esi
    ret     0
    

    The 3 function variables are extracted from the stack and placed into eax, ecx and edx.
    Another forth value is placed into esi (and passed on to ecx).
    The constructor is called with the 3 parameters on the stack, and ecx still containing the forth value.

    Let’s take a look in the constructor –

    _TEXT SEGMENT
    _Data1$ = 8    ; size = 4
    _Data2$ = 12   ; size = 4
    _Data3$ = 16   ; size = 4
    
    mov    edx, DWORD PTR _Data2$[esp-4]
    mov    eax, ecx
    mov    ecx, DWORD PTR _Data1$[esp-4]
    mov    DWORD PTR [eax], ecx
    mov    ecx, DWORD PTR _Data3$[esp-4]
    mov    DWORD PTR [eax+4], edx
    mov    DWORD PTR [eax+8], ecx
    
    ret    12   ; 0000000cH
    

    The 3 constructor parameters are read into offsets of eax – eax being a copy of ecx, the forth parameter from the call above.
    So, the constructor builts the object where it is told to – the forth parameter of Func().

    And, you guessed it, the forth parameter of Func() is actually the real single place in the entire program where a constructed AClass exists. Let’s look at the relevant part of main() –

    _TEXT SEGMENT
    _TheClass$ = -12    ; size = 12
    _main PROC
    
    sub    esp, 12
    
    push   30
    push   20
    lea    eax, DWORD PTR _TheClass$[esp+20]
    push   10
    push   eax
    call   ?Func@@YA?AVAClass@@HHH@Z    ; Func
    

    12 bytes are reserved for an AClass and the three arguments to Func() are passed, along with the forth one – pointing to those 12 bytes.

    This is a specific example with a specific compiler. Other compilers do this differently. But that’s the spirit of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think that by already having experience with RoR and… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer Jakob Nielsen's "useit" site is a great resource for web… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer http://ant.apache.org/ Here is how to do C++ projects with Ant:… May 12, 2026 at 12:41 am

Related Questions

I'm back with another question concerning threads and synchronization. Imagine a server application that
For the code below, I am having some issues in IE. The second parameter
I am trying to currently embed a media player into certain web content and
Recently I was talking to a co-worker about C++ and lamented that there was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.