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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:00:34+00:00 2026-06-12T11:00:34+00:00

A coworker poked me with this question, after noticing a curious behavior with C++

  • 0

A coworker poked me with this question, after noticing a curious behavior with C++ structs.

Take this trivial code:

struct S {
  int i;
#ifdef TEST
  ~S() {}
#endif
};

void foo (S s) {
  (void)s;
}

int main () {
  foo(S());
  return 0;
}

I have generated the assembly code once without the explicit destructor:

g++-4.7.2 destructor.cc -S -O0 -o destructor_no.s

and later including it:

g++-4.7.2 destructor.cc -DTEST -S -O0 -o destructor_yes.s

This is the code[1] for main in destructor_no.s:

main:
    pushq   %rbp
    movq    %rsp, %rbp
    movl    $0, %eax
    movl    %eax, %edi
    call    _Z3foo1S   // call to foo()
    movl    $0, %eax
    popq    %rbp
    ret

While, instead, if the destructor is defined explicitly:

main:
    pushq   %rbp
    movq    %rsp, %rbp
    subq    $16, %rsp
    movl    $0, -16(%rbp)
    leaq    -16(%rbp), %rax
    movq    %rax, %rdi
    call    _Z3foo1S   // call to foo()
    leaq    -16(%rbp), %rax
    movq    %rax, %rdi
    call    _ZN1SD1Ev  // call to S::~S()
    movl    $0, %eax
    leave
    ret

Now, my assembly knowledge is a bit rusty, but it seems to me that:

  1. in the first case, the struct is passed “by value”. I.e., its memory content is copied into the %edi register, that, if I am not mistaken, is the first register used for argument passing in the x86-64 ABI.

  2. in the second case, instead, the struct is allocated on the stack, but the foo() function is called with a pointer in %rdi.

Why is there such a difference?


Notes:

  • The same behavior is confirmed if using gcc-4.6.3, or clang 3.1.

  • Of course, if optimizations are enabled, the call to function foo() is completely optimized away in any case.

  • An interesting pattern emerges when adding more variables to the struct, if no destructor is explicly provided.

Up to 4 ints (= 16 bytes) are passed through the argument registers:

pushq   %rbp
movq    %rsp, %rbp
subq    $16, %rsp
movl    $0, -16(%rbp)
movl    $0, -12(%rbp)
movl    $0, -8(%rbp)
movl    $0, -4(%rbp)
movq    -16(%rbp), %rdx
movq    -8(%rbp), %rax
movq    %rdx, %rdi
movq    %rax, %rsi
call    _Z3foo1S

but as soon as I add a fifth int to the struct, the argument to the function, still passed “by value”, is now on the stack:

pushq   %rbp
movq    %rsp, %rbp
subq    $56, %rsp
movl    $0, -32(%rbp)
movl    $0, -28(%rbp)
movl    $0, -24(%rbp)
movl    $0, -20(%rbp)
movl    $0, -16(%rbp)
movq    -32(%rbp), %rax
movq    %rax, (%rsp)
movq    -24(%rbp), %rax
movq    %rax, 8(%rsp)
movl    -16(%rbp), %eax
movl    %eax, 16(%rsp)
call    _Z3foo1S

[1] I have removed some lines that I think are unnecessary for the purpose of this question.

  • 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-06-12T11:00:36+00:00Added an answer on June 12, 2026 at 11:00 am

    In C++03-speak if you define a destructor your struct is not a POD-type anymore. An object of the variant without the destructor behaves like a C struct variable (thus it’s just passed around by value), while the one with the user-defined one behaves like a C++ object.

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

Sidebar

Related Questions

A coworker's typo when calling a subref raised this strange syntax question. If I
I was told this by a coworker, and I was curious if this was
A coworker of mine started with this code: var newList = new List<>(); foreach(var
I have this code from a coworker (probably got it from the web somewhere)
A coworker of mine has this problem, apparently after installing Re#, which seems totally
A co-worker posed this question to me, and I told them, No, you'll need
My coworker suggested making several of the Eclipse code-formatting and warning settings to be
A coworker recently showed me some code that he found online. It appears to
My coworker did this experiment: public class DoubleDemo { public static void main(String[] args)
A coworker showed me the following code and asked me why it worked. <span

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.