2

Hi i'm new to learning C++ and i just have a quick question.

if i am creating an array containing 5 structs should i clear the array as shown below upon construction or leave it as when i declare it.

in other words is doing something like

person a[5]; for(int i = 0; i < 5; i++) { a[i].name= ""; a[i].value= 0; } 

the same as doing something like this, please do note that i will basically looping through these later and setting these values.

person[5]; 
2
  • person[5] is 6th position FYI Commented Mar 14, 2013 at 16:39
  • 1
    It really depends on the details of the struct itself. And what you want to achieve, of course. Commented Mar 14, 2013 at 16:41

7 Answers 7

3

No, you don't. What you need is a proper default constructor for person

Sign up to request clarification or add additional context in comments.

1 Comment

Of course, that has the consequence of rendering person a non-aggregate. That could not be an issue, but it is worth mentioning. Also, depending on types of the data members, a user defined default constructor might not be necessary.
3

Just write a default constructor for person, assuming it's a struct or class. It will be called to initialize each array element automatically.

In general, you should probably prefer to use std::vector<person> instead, but you haven't given enough context to say for sure.


The default constructor would look something like: this (note that std::string has its own default constructor, so we don't need to do anything for that)

struct person { std::string name; int value; person() : value(0) {} }; 

Now this:

person a[5]; 

is properly initialized with no further effort.


If you're planning to initialize the elements "properly" later anyway, then using a vector makes it easier to skip the wasted dummy initialization:

struct person { std::string name; int value; person() : value(0) {} person(std::string const &n, int v) : name(n), value(v) {} }; std::vector<person> people; people.reserve(5); // just create each person with the right values in the first place: for (int i = 0; i < 5; ++i) { people.push_back(person(get_name(i), get_value(i))); } 

Finally, if you don't mind having uninitialized memory around, your first version (with no constructor and no loop) is fine. However, note that losing track of which elements/members are valid and which are garbage, is a fertile source of bugs.

Comments

2

Only POD types that have static storage duration are being zero-initialized upon creation. But since using of uninitialized variables produces undefined behavior, it is considered a good practice to initialize variables explicitly, even when it might be redundant.

But instead of initializing it in a loop, it's better to define a default constructor, which will initialize the data members of the instance of person when it is constructed:

struct person { person() : value(0) { } std::string name; int value; } 

And also note that using of STL containers such as std::vector is much better idea than using
C-style arrays :)

3 Comments

"Your struct is a POD type" - no it's not, not with std::string inside ayway. And you don't need to initialize string with empty "", it has default constructor which does that
You can't say it is POD without knowing what the types of its members are.
@juanchopanza, aleguna: Edited.
2

As mentioned above person[5] is the 6th position

In C++ dont use array and instead use vectors

Comments

2

Is there any reasons why you can not use a vector? It is they C++ way and it will be much easier and less likely to have bugs that way. Here is an example:

 #include <vector> std::vector<person> a ; a.push_back( person(...) ) ; 

Also, as mentioned if you need to use arrays then having a default constructor for person assure that you do not need to initialize later on. For example:

class person { public: person() : value(0) {...} // default constructor }; 

2 Comments

Maybe they want a fixed size, aggregate structure.
@juanchopanza I amended to mention default ctor if they needed to use arrays
0

On the first line, where you declare your array, the default person constructor will be invoked 5 times (once per element of your array). If your person constructor initializes the name and value elements to sensible values, there is no need for your loop.

Comments

0

No, they are not the same.

In the first example:

person a[5]; 

You are declaring an array of length 5, of type person.

However, this:

person[5] 

This is not valid - there is no variable name, nothing to be assigned, nothing that you can loop through later.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.