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 7592973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:02:46+00:00 2026-05-30T21:02:46+00:00

I want to know how the compiler save this temporary int if I call

  • 0

I want to know how the compiler save this temporary int if I call the function f(3);

int f (int x) { return x; } 

and how this will be excuted by the compiler :

int a=f(3);

is it just like doing int a=x; (I know x will be already destroyed ) or it really create a temporary variable called f(3), like this int f(3)=x;

int& a=f(3);

and why this wont work ?

  • 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-30T21:02:48+00:00Added an answer on May 30, 2026 at 9:02 pm

    Function calls

    The compiler will do one of the following things:

    1. Push the argument on stack and call the function
      • For a more complex function than yours, this will typically happen.
    2. Load the argument into a register and call the function
      • This may happen when optimizing, and there are enough registers to hold variables that need to be passed around.
    3. Optimize the function away completely (inlining)
      • For a trivial function like the one in your case, a sane compiler will do this with the most basic optimization level, so that you get the same assembly as if you did int a = 3.

    Reference variables in C++

    A reference variable, one declared as int &a in your code is “a different name for an existing memory location”. So declaring int &a does not allocate space for an int anywhere. It just declares a to refer to an already allocated memory location.

    This location may be an existing variable int b, so that you say:

    int b;
    int &a = b;
    

    Here, a will refer to the same contents that b refers to. “A new name for an existing object” is a good idiom to go with.

    You could get fancy and say int &a = array[5], so that a refers to 6th element of an int array array, or int &a = *(int*)0x12345678 to refer to a specific memory location, but I’m digressing.

    Your code

    int &a = 3;
    

    cannot work, because 3 is a temporary object, which will be forgotten after the statement is executed. To understand the problem more fundamentally think of this: If a refers to an already allocated memory location, what will it refer to, after the statement int &a = 3 is executed, and there is no longer a temporary object 3?

    This is also a common problem with reference variables in functions: returning a reference to a function-local object is undefined behavior… but I’m digressing again. You always have to have a “living, allocated object” for a to refer to, end of story.

    A bit more in-depth on reference variables

    What typically happens for a statement like

    int a = 3;
    

    is that the compiler generates code to (simplified):

    1. load the constant 3 to a register
    2. load the register to the memory location allocated for a

    The point is: in either case, there is no long-lived memory location allocated for the object 3, so an int &a really cannot be made to refer to this object because of that.

    “long-lived memory location” means a location that will live past the assignment operation. The register where 3 is stored will be overwritten and reused probably immediately after the assignment operation, so it doesn’t qualify even theoretically for the target of int &a (in practice, int &a can only be made to refer to a memory location, not a register, anyway).

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

Sidebar

Related Questions

Just out of curiosity: I know I can tell the compiler if I want
I want to know this info to reduce my code size so I will
want to know why String behaves like value type while using ==. String s1
I want to know how to use variables for objects and function names in
Hi I know this sounds quite not practical but still I would like to
For my assignment , I want to know about common compiler list? I really
Basically i want to know if the visual studio IDE and/or compiler in 2010
If i want to get a js code like this which compiles from coffeescript:
I have been asked this questions before but this time i want to know
I know that the compiler will sometimes initialize memory with certain patterns such as

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.