Is an incomplete initializer good form?

  • Thread starter Jake Montgomery
  • Start date
J

Jake Montgomery

It is my understanding the the c++ standard guarantees that an
incomplete initializer will initialize all the remaining elements to 0.
I have seen the following form, and was wondering is there was any good
reason to avoid it?


int main()
{
struct foo {
int sizeOfThisStruct;
int x;
int y;};

foo myfoo = {sizeof(foo)}; // sets the sizeOfThisStruct member and
//zeros the rest in one line.
}
 
V

VJ

Jake said:
It is my understanding the the c++ standard guarantees that an
incomplete initializer will initialize all the remaining elements to 0.
I have seen the following form, and was wondering is there was any good
reason to avoid it?


int main()
{
struct foo {
int sizeOfThisStruct;
int x;
int y;};

foo myfoo = {sizeof(foo)}; // sets the sizeOfThisStruct member and
//zeros the rest in one line.
}

I do not know what the standard says, but I would treat those as
undefined, until they are assigned values.

You can make a constructor inside your structure, like this:

struct foo
{
foo( )
: sizeOfThisStruct( 0 ),
x( 0 ),
y( 0 )
{}

int sizeOfThisStruct;
int x;
int y;
};

That way you are sure how your variables are initialized.
 
V

Victor Bazarov

To Jake:

That's correct.
I have seen the following form, and was wondering is there was

At least one reply indicates that poor education of your peers might
be a valid reason.
I do not know what the standard says, but I would treat those as
undefined, until they are assigned values.

You can make a constructor inside your structure, like this:

struct foo
{
foo( )
: sizeOfThisStruct( 0 ),
x( 0 ),
y( 0 )
{}

int sizeOfThisStruct;
int x;
int y;
};

That way you are sure how your variables are initialized.

Two reasons not to do that. First, a user-defined c-tor makes the
struct lose its Plain-Old-Data-ness. Second, if this struct comes
from a library, you have no way of editing it.

The Standard guarantees that the remaining elements are initialised
with 0. Only mistrusting your compiler can make people want to do
what you propose.

V
 
V

VJ

Victor said:
To Jake:



That's correct.

Regardless what c++ standard says, I do not trust compilers, therefore I
am going to continue initializing my variables before using them
At least one reply indicates that poor education of your peers might
be a valid reason.

Why do you think my education is poor?

Two reasons not to do that. First, a user-defined c-tor makes the
struct lose its Plain-Old-Data-ness. Second, if this struct comes
from a library, you have no way of editing it.

The Standard guarantees that the remaining elements are initialised
with 0. Only mistrusting your compiler can make people want to do
what you propose.

He did not say the structure is POD, but I must admit I did not think
about it.

And yes, you are correct - I do not trust compilers enough to let them
initialize structures on their own
 
V

Victor Bazarov

VJ said:
Regardless what c++ standard says, I do not trust compilers,
therefore I am going to continue initializing my variables before
using them

Whatever floats your boat.
Why do you think my education is poor?

I said nothing about *your* education. But the sheer fact that you
don't trust your tools may mean you don't know them enough.
[..]
And yes, you are correct - I do not trust compilers enough to let
them initialize structures on their own

Again, whatever.

V
 
G

Gianni Mariani

VJ wrote:
....
Regardless what c++ standard says, I do not trust compilers, therefore I
am going to continue initializing my variables before using them

Not that compilers are infallible, however, betting your compiler is
wrong is a loosing strategy. Besides, initialization of structs has
been defined this way since K&R C days, there would be far more problems
in code than yours if the compiler suddenly stopped initializing the
rest of the elements to zeros.
 
D

Default User

VJ wrote:

Regardless what c++ standard says, I do not trust compilers,
therefore I am going to continue initializing my variables before
using them


Why do you trust your compiler to do the initializations correctly? You
may be paranoid, but are you paranoid enough?


Seriously, a major deviation from the standard like that wouldn't be
around in most popular compiler sets. It would have been noticed and
complained about many times over.




Brian
 
F

Frederick Gotham

Jake Montgomery:
It is my understanding the the c++ standard guarantees that an
incomplete initializer will initialize all the remaining elements to 0.


Correct. Specifically:

When initialising an aggregate, all remaining elements become default-
initialised.

I have seen the following form, and was wondering is there was any good
reason to avoid it?


int main()
{
struct foo {
int sizeOfThisStruct;
int x;
int y;};

foo myfoo = {sizeof(foo)}; // sets the sizeOfThisStruct member and
//zeros the rest in one line.
}


For some reason an "int" is used to store a positive integer, but some
people just can't break this habit. Also, there's redundant parentheses
around "foo", but again, some people just can't break this habit. Other
than that though, it looks OK.
 
V

Victor Bazarov

Frederick said:
Jake Montgomery:


For some reason an "int" is used to store a positive integer, but some
people just can't break this habit. Also, there's redundant
parentheses around "foo", but again, some people just can't break
this habit. Other than that though, it looks OK.

Parentheses around 'foo' are NOT redundant - they are required since
'foo' is a type-id. They woudl be redundant if the initialisation
were

foo myfoo = { sizeof(myfoo) };

because they are not needed around an expression denoting an object.

V
 
F

Frederick Gotham

Victor Bazarov:
Parentheses around 'foo' are NOT redundant - they are required since
'foo' is a type-id. They woudl be redundant if the initialisation
were

foo myfoo = { sizeof(myfoo) };

because they are not needed around an expression denoting an object.


Sorry you're right, I'm too accustomed to using lowercase for object names
and uppercase for types, didn't think to check if it was a type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top