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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:10:40+00:00 2026-05-14T04:10:40+00:00

Why is it that for user defined types when creating an array of objects

  • 0

Why is it that for user defined types when creating an array of objects every element of this array is initialized with the default constructor, but when I create an array of a built-in type that isn’t the case?

And second question: Is it possible to specify default value to be used while initializing elements in the array? Something like this (not valid):

char* p = new char[size]('\0');

And another question in this topic while I’m with arrays. I suppose that when creating an array of user defined type, every element of this array will be initialized with default value. Why is this?

If arrays for built in types do not initialize their elements with their defaults, why do they do it for User Defined Types?

Is there a way to avoid/circumvent this default construction somehow? It seems like bit of a waste if I for example have created an array with size 10000, which forces 10000 default constructor calls, initializing data which I will (later on) overwrite anyway.

I think that behaviour should be consistent, so either every type of array should be initialized or none. And I think that the behaviour for built-in arrays is more appropriate.

  • 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-14T04:10:40+00:00Added an answer on May 14, 2026 at 4:10 am

    Something like this (not valid):

    As far as I know that is perfectly valid.
    Well not completely, but you can get a zero intialized character array:

    #include <iostream>
    #include <cstdlib>
    
    int main(int argc, char* argv[])
    {
        //The extra parenthesis on the end call the "default constructor"
        //of char, which initailizes it with zero.
        char * myCharacters = new char[100]();
        for(size_t idx = 0; idx != 100; idx++) {
            if (!myCharacters[idx])
                continue;
            std::cout << "Error at " << idx << std::endl;
            std::system("pause");
        }
        delete [] myCharacters;
        return 0;
    }
    

    This program produces no output.

    And another question in this topic while I’m with arrays. I suppose that when creating an array of user defined type and knowing the fact that every elem. of this array will be initialized with default value firstly why?

    Because there’s no good syntactic way to specialize each element allocated with new. You can avoid this problem by using a vector instead, and calling reserve() in advance. The vector will allocate the memory but the constructors will not be called until you push_back into the vector. You should be using vectors instead of user managed arrays anyway because new’d memory handling is almost always not exception safe.

    I think that behaviour should be consistent, so either every type of array should be initialized or none. And I think that the behaviour for built-in arrays is more appropriate.

    Well if you can think of a good syntax for this you can write up a proposal for the standard — not sure how far you’ll get with that.

    Why is it that for user defined types when creating an array of objects every element of this array is initialized with the default constructor, but when I create an array of a built-in type that isn’t the case? and
    And another question in this topic while I’m with arrays. I suppose that when creating an array of user defined type, every element of this array will be initialized with default value. Why is this? and
    If arrays for built in types do not initialize their elements with their defaults, why do they do it for User Defined Types?
    Because a user defined type is never ever valid until its constructor is called. Built in types are always valid even if a constructor has not been called.

    And second question: Is it possible to specify default value to be used while initializing elements in the array? Something like this (not valid):
    Answered this above.

    Is there a way to avoid/circumvent this default construction somehow? It seems like bit of a waste if I for example have created an array with size 10000, which forces 10000 default constructor calls, initializing data which I will (later on) overwrite anyway.
    Yes, you can use a vector as I described above.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll need to create a custom view engine and use… May 14, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer Correct. It looks like your mono binary has its symbols… May 14, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer I would rather inject IAppSettings into every class that needs… May 14, 2026 at 5:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.