473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is an incomplete initializer good form?

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.
}

Nov 6 '06 #1
9 2774
VJ
Jake Montgomery wrote:
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.
Nov 6 '06 #2
To Jake:

VJ wrote:
Jake Montgomery wrote:
>It is my understanding the the c++ standard guarantees that an
incomplete initializer will initialize all the remaining elements to
0.
That's correct.
I have seen the following form, and was wondering is there was
>any good reason to avoid it?
At least one reply indicates that poor education of your peers might
be a valid reason.
>>

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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #3
VJ
Victor Bazarov wrote:
To Jake:

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


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
>
>>I have seen the following form, and was wondering is there was
>>>any good reason to avoid it?


At least one reply indicates that poor education of your peers might
be a valid reason.

Why do you think my education is poor?

>>>
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.


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
Nov 6 '06 #4
VJ wrote:
Victor Bazarov wrote:
>To Jake:

VJ wrote:
>>Jake Montgomery wrote:

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


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
Whatever floats your boat.
>>I have seen the following form, and was wondering is there was

any good reason to avoid it?


At least one reply indicates that poor education of your peers might
be a valid reason.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #5
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.
Nov 6 '06 #6
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
Nov 6 '06 #7
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.

--

Frederick Gotham
Nov 6 '06 #8
Frederick Gotham wrote:
Jake Montgomery:
>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #9
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.

--

Frederick Gotham
Nov 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
5
by: Lou Pecora | last post by:
g++ compiler error question. I have a container C whose constructor takes a class B that is inherited from an abstract class A. So I have the line of code: B binstance; C ...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
2
by: Jan | last post by:
In a 2-page order form, not all applicants will complete it due to field validations. If I want to see data from incomplete orders, can this be done using some sort of session control so that each...
5
by: friend.05 | last post by:
1) #include <stdio.h> #include <stdlib.h> #include "graph.h" #define MAX_VTX 4 struct _graph_vertex {
2
by: dave_dp | last post by:
Hi folks, I'm interested as to what extent using incomplete types doesn't result in a undefined behavior, more generally it touches the usage of incomplete types.. for example, it is stated that...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.