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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:53:01+00:00 2026-06-18T05:53:01+00:00

This compiles, but I’ve never seen it in any other code. Is it safe?

  • 0

This compiles, but I’ve never seen it in any other code. Is it safe?

Testclass():sources(new int[32]){}

Instead of:

Testclass(){
    sources = new int[32];
}
  • 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-18T05:53:02+00:00Added an answer on June 18, 2026 at 5:53 am

    Use:

    Testclass():sources(new int[32]){}
    

    This is using member-initialization-list which is the preferred way to initialize members.

    By “safe” or “okay” you probably meant, whether it is exception-safe? What if new throws the bad_alloc exception?

    Well, in that case, the destructor will not be called, because the object is not fully-constructed, as constructor-body is not executed. There may be a resource leak if you’ve acquired any resource in the initialization list.

    Consider this,

    class X
    {
        int  *ints;  // Order of declaration matters!
        Huge *huges; // It ensures that huges will be initialized after ints
    
        X() : ints(new int[32]), huges(new Huge[10000]) {}
    };
    

    If new Huge[10000] throws an exception, the memory allocated toints will leak!

    In such cases, function-try-block can be useful. See these:

    • Function try blocks, but not in constructors
    • What is the purpose of a function try block?

    If you think about this problem of exception-safety, you will soon realize that if a class manages just one resource, then the life will be easier. If a single class manages more than one resource, then you wouldn’t be able to decide which one threw an exception in the member-initialization-list, and consequently, you wouldn’t be able to decide which one is to be deallocated in the catch block of function-try-block. A resource leak is destined.

    However, if a class needs more than one resource, then first you encapsulate each of the different type of resource in a class, and declare objects of these resource-managing class as a member of your class.

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

Sidebar

Related Questions

I noticed that this code compiles but I have no idea why: int main()
This code compiles fine with GCC and Clang but not with MSVC 2010: #include
This code compiles, and runs successfully locally, but not on another server. Both machines
I have non-template class with a templatized constructor. This code compiles for me. But
This Java code compiles fine, but when I try to run it I get:
The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this
This code compiles fine but give segmentation fault error while running? Can anyone tell
This compiles without error but when I run this code section some strange error
This code compiles but looks very strange. I have a typical and simple parent/child
This code compiles and runs but it does not copy the text from the

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.