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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:50:55+00:00 2026-05-20T05:50:55+00:00

I have a general question, that may be a little compiler-specific. I’m interested in

  • 0

I have a general question, that may be a little compiler-specific.
I’m interested in the conditions under which a constructor will be called. Specifically, in release mode/builds optimised for speed, will a compiler-generated or empty constructor always be called when you instantiate an object?

class NoConstructor  
{  
    int member;  
};  

class EmptyConstructor  
{  
    int member;  
};

class InitConstructor  
{  
    InitConstructor()  
        : member(3)   
    {}  
    int member;  
};

int main(int argc, _TCHAR* argv[])  
{  
    NoConstructor* nc = new NoConstructor(); //will this call the generated constructor?  
    EmptyConstructor* ec = new EmptyConstructor(); //will this call the empty constructor?  
    InitConstructor* ic = new InitConstructor(); //this will call the defined constructor  

    EmptyConstructor* ecArray = new EmptyConstructor[100]; //is this any different?
}

I’ve done a lot of searching, and spent some time looking through the generated assembly code in Visual Studio. It can be difficult to follow in release builds though.

In summary:
Is the constructor always called? If so, why?

I understand this will very much depend on the compiler, but surely there’s a common stance. Any examples/sources you can cite would be really appreciated.

  • 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-20T05:50:56+00:00Added an answer on May 20, 2026 at 5:50 am

    When in optimizing mode, if your class or structure is POD (has only POD types) and constructor is not specified, any production quality C++ compiler will not only skip the call to a constructor but not even generate it.

    If your class has non-POD members who’s constructor(s) have to be called, compiler will generate default constructor that calls member’s constructors. But even then – it will not initialize POD types. I.e. if you don’t initialize member explicitly, you may end up with garbage there.

    The whole thing can get even fancies if your compiler/linker has LTO.

    Hope it helps! And make your program work first, then use a profiler to detect slow places, then optimize it. Premature optimization may not only make your code unreadable and waste tons of your time, but could also not help at all. You have to know what to optimize first.

    Here is a disassembly for code in your example (x86_64, gcc 4.4.5):

    main:
        subq    $8, %rsp
        movl    $4, %edi
        call    _Znwm
        movl    $4, %edi
        movl    $0, (%rax)
        call    _Znwm
        movl    $4, %edi
        movl    $0, (%rax)
        call    _Znwm
        movl    $400, %edi
        movl    $3, (%rax)
        call    _Znam
        xorl    %eax, %eax
        addq    $8, %rsp
        ret
    

    As you can see, there are no constructors called at all. There are no classes at all, every object is just a 4 bytes integer.

    With MS compiler, YMMV. So you have to check disassembly yourself. But result should be similar.

    Good luck!

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

Sidebar

Related Questions

I have a question that doesn't need any specific answer, just a general direction
This is more of a design question that others may have had similar experience
A general architecture question in Sitecore 6... Let’s say we have a situation where
Ok another WPF question, well I guess this is just general .NET. I have
I have to give a general note to some huge Java project for which
Just a very general question, that not only applies to this example. Let's say
When building Boost binary libraries with bjam , one may specify which compiler to
I have a general question about WPF performance. We have a relatively simple forms
I have a general question regarding Android's separation of layouts into layout.xml files: My
All, I have a very general question regarding a future project. I need to

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.