473,323 Members | 1,547 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,323 software developers and data experts.

Is initialization order guaranteed?

#include <iostream>

using namespace std;

struct foo
{
int a;
int b;
};

int main()
{
int x = 5;

foo bar = {x = 1, x + 1};
cout << bar.a;
cout << bar.b << endl;

// Is this guaranteed to output "12"?
// Is it possible that this could output "16"?
}
Regards,
Dave
Jul 23 '05 #1
7 1341
Why should it give 12 or 16. Your statement (x=1,x+1) will assign the
value 1 to bar.a and 2 to bar.b.
So first cout will give 1 and next cout will give 2.
If you change your statement of initializing bar as
foo bar = {1,x+1}
then output will be 1 and 6.

Jul 23 '05 #2

"vindhya" <ma*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Why should it give 12 or 16. Your statement (x=1,x+1) will assign the
value 1 to bar.a and 2 to bar.b.
So first cout will give 1 and next cout will give 2.
If you change your statement of initializing bar as
foo bar = {1,x+1}
then output will be 1 and 6.


Pasting the code back for reference...

#include <iostream>

using namespace std;

struct foo
{
int a;
int b;
};

int main()
{
int x = 5;

foo bar = {x = 1, x + 1};
cout << bar.a;
cout << bar.b << endl;

// Is this guaranteed to output "12"?
// Is it possible that this could output "16"?
}

What I'm asking is whether the initializers are guaranteed to be evaluated
in order. If the implementation is free to evaluate the initializers in any
order, some compiler might choose to evaluate x+1 before evaluating x = 1.
If this happened, the output would be 16.
Jul 23 '05 #3

"vindhya" <ma*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Why should it give 12 or 16. Your statement (x=1,x+1) will assign the
value 1 to bar.a and 2 to bar.b.
So first cout will give 1 and next cout will give 2.


Right. And he is not outputting whitespace characters between integers, so
he should see "12" i.e. one integer outputted directly after the other.

Stephen Howe

Jul 23 '05 #4

"vindhya" <ma*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Why should it give 12 or 16. Your statement (x=1,x+1) will assign the
value 1 to bar.a and 2 to bar.b.
So first cout will give 1 and next cout will give 2.
so that would make "12" ("1" followed by "2")
If you change your statement of initializing bar as
foo bar = {1,x+1}
then output will be 1 and 6.

Jul 23 '05 #5
Dave wrote:
#include <iostream>

using namespace std;

struct foo
{
int a;
int b;
};

int main()
{
int x = 5;

foo bar = {x = 1, x + 1};
cout << bar.a;
cout << bar.b << endl;

// Is this guaranteed to output "12"?
// Is it possible that this could output "16"?
}


I believe the initialisation order is the order of declaration.

I.e., a then b, and the output will be "12" (newline, etc.)

Even if you had:

#include <iostream>

struct foo
{
int a;
int b;
foo(int val) : b(val + 1), a(val = 1) {}
};

int main(int argc, char* argv[])
{
//And called:
int x = 5;
foo bar = foo(x);
std::cout << bar.a << bar.b << x << std::endl;

return 0;
}

Then the order of initialisation is still the order of declaration (a and then b), not the order of the initialiser list.

Output is "125".

Ben
--
A7N8X FAQ: www.ben.pope.name/a7n8x_faq.html
Questions by email will likely be ignored, please use the newsgroups.
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #6
Ben Pope schreef:
Dave wrote:
#include <iostream>

using namespace std;

struct foo
{
int a;
int b;
};

int main()
{
int x = 5;

foo bar = {x = 1, x + 1};
cout << bar.a;
cout << bar.b << endl;

// Is this guaranteed to output "12"?
// Is it possible that this could output "16"?
}


I believe the initialisation order is the order of declaration.


True, but are you initializing b with 2 or with 6? If the compiler
uses some kind of stack, it could legally push x+1=6 and then x=1,
then pop 1 to initialize a and then pop 6 to initialize b. That is
still initialization in declaration order. The evaluation of
arguments is another question, and that's not ordered.

The actual output can also be "You're fired". The reason is the
code writes to and reads from x without an intervening sequence
point. That is undefined behavior, and that means all bets are off.

Regards,
Michiel Salters

Jul 23 '05 #7
msalters wrote:
Ben Pope schreef:

I believe the initialisation order is the order of declaration.

True, but are you initializing b with 2 or with 6? If the compiler
uses some kind of stack, it could legally push x+1=6 and then x=1,
then pop 1 to initialize a and then pop 6 to initialize b. That is
still initialization in declaration order. The evaluation of
arguments is another question, and that's not ordered.

The actual output can also be "You're fired". The reason is the
code writes to and reads from x without an intervening sequence
point. That is undefined behavior, and that means all bets are off.


Which is probably why it looks ugly and feels wrong, anyway! Thanks for the clarification.

If you really can calculate the value of two members from one argument, perhaps you have redundant data, which is usually asking for trouble.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #8

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

Similar topics

6
by: Michael Klatt | last post by:
I am working on a library which uses the GKS graphics package. I have a Gks object which opens the GKS subsystem in its constructor: // Gks.hpp class Gks { public : Gks(); };
4
by: Cheng Mo | last post by:
I know global varaibles should always be avoided. I asked this question just for deep insight about C++. If global variables are distributed among different source code files, what's the...
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.