Question

I read that in the following case:

int i = 12;
int &a = i;

a will not occupy a space on the stack as it is an alias of i;

My question is suppose its a parameter as such

void funct(foo& a , int b)
{

}

when the function is created will a occupy a space on the stack ?

Was it helpful?

Solution

A reference is more or less like a pointer at this level and the following

#include <stdio.h>
#include <stdlib.h>

struct foo{
     int val;
};

int funct(foo& a, int b)
{
     return a.val;
}

int main(void) {
     foo obj;
     obj.val = 92;
     funct(obj, 22); // 22 is passed by value, obj is passed by reference

     return EXIT_SUCCESS;
}

gets translated to:

                .Ltext0:
                    .globl  _Z5functR3fooi  // funct()
                _Z5functR3fooi:
                .LFB2:
                    .cfi_startproc
0000 55             pushq   %rbp  // some stack bookkeeping
                    .cfi_def_cfa_offset 16
                    .cfi_offset 6, -16
0001 4889E5         movq    %rsp, %rbp
                    .cfi_def_cfa_register 6
0004 48897DF8       movq    %rdi, -8(%rbp)   <-- move the address on the stack frame
0008 8975F4         movl    %esi, -12(%rbp)  <-- move the value on the stack frame
000b 488B45F8       movq    -8(%rbp), %rax   <-- get the address from the stack frame
000f 8B00           movl    (%rax), %eax     <-- use it
0011 5D             popq    %rbp
                    .cfi_def_cfa 7, 8
0012 C3             ret
                    .cfi_endproc
                .LFE2:
                    .globl  main
                main:
                .LFB3:
                    .cfi_startproc // Main
0013 55             pushq   %rbp // Stack bookkeeping
                    .cfi_def_cfa_offset 16
                    .cfi_offset 6, -16
0014 4889E5         movq    %rsp, %rbp
                    .cfi_def_cfa_register 6
0017 4883EC10       subq    $16, %rsp
                .LBB2:
001b C745F05C       movl    $92, -16(%rbp)     <-- save 92 (the entire POD struct) on the stack frame
     000000
0022 488D45F0       leaq    -16(%rbp), %rax    <-- get the pointer to the stack frame where the obj is
0026 BE160000       movl    $22, %esi          <-- save the value in a register
     00
002b 4889C7         movq    %rax, %rdi         <-- address of the stack frame to the object
002e E8000000       call    _Z5functR3fooi     // funct() call
     00
0033 B8000000       movl    $0, %eax
     00
                .LBE2:
0038 C9             leave
                    .cfi_def_cfa 7, 8
0039 C3             ret
                    .cfi_endproc
                .LFE3:
                .Letext0:

Of course keep in mind that this is just an implementation (gcc's one to be precise) without any optimization. It depends on the compiler how this really works.

(asm generated by http://assembly.ynh.io/)

OTHER TIPS

According to the standard, whether or not a reference requires storage is unspecified. So it depends on the implementation.

A reference is often implemented as an address in assembly. However, a reference is neither a pointer to an object nor a copy of the object. A reference is the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top