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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:00:05+00:00 2026-05-26T17:00:05+00:00

What happens in the following code? I guess it doesn’t work since I get

  • 0

What happens in the following code?

I guess it doesn’t work since I get a segmentation fault if I want to add something to the b array, but what exactly did I do here?

Is there no way of specifying the size of an array inside the constructor?

class A {
  public:
   A() {
      b[3];
   }
  private:
    B b[];
};
  • 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-26T17:00:06+00:00Added an answer on May 26, 2026 at 5:00 pm

    B b[] here is a “flexible array member“, a non-standard extension in your compiler (taken from C99) that allows you to declare an unbounded array as the last member in a type. It’s only of use when allocating your object the old-fashioned C way (when you pad your argument to malloc to make space for the array), and ought to be avoided. In this case, you haven’t allocated any extra memory for the array, so in your constructor body when you’re trying to access something that occurs 3 ‘elements’ past this nothingness, you’re invoking UB. I’ll ignore the extension for the rest of my answer, as it really has no place in C++ code.

    Is there no way of specifying the size of an array inside the constructor?

    No, there isn’t.

    Array bounds must be known at compile-time, so there is no case where you know more in your ctor body than you do in the class definition; you are required to write the dimension in the member’s declaration itself:

    class A {
        B b[3];
    };
    

    If the dimension is a run-time quantity in your program, you’ll need to instead store a pointer and, in your constructor, point it at a dynamic block of memory:

    class A {
      public:
       A() : b(new B[3]) {}
      ~A() { delete[] b; }
      private:
        B* b;   // same as `B b[]`! but far clearer
    };
    

    Instead, though, I suggest a std::vector:

    class A {
      public:
       A() : b(3) {}
      private:
        std::vector<B> b;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes users mistakenly redirected to ?Process=ViewImages&PAGEID=. When this happens, they get the following error.
Can anyone shed some insight onto why the following happens? irb(main):001:0> r = '1'..'30'
The following problem happens on both Safari and Chrome, so probably a WebKit issue.
What happens in the memory when a class instantiates the following object? public class
I have the following issue and I would like to know what exactly happens.
first I want to say that I hope this doesn't look like I am
Using reflector I found the following code in the System.Web.UI.WebControls.Parameter class: internal void UpdateValue(HttpContext
Consider the following short code snippet. namespace B { public class Foo { public
I thought this would be pretty simple, but guess not. I have the following
I have the following code to test the menu button : <script type=text/javascript cahrset=utf-8>

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.