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

using nonconstant expressions in an initializer list

Hi all,

Recently i was reading a c book which contained a section called C
pitfalls.

it had a paragraph on the following lines:-

-------------------------------------------------------------------
using nonconstant expressions in an initializer list
-------------------------------------------------------------------
struct s {int i;};
int foo(void);

struct s bar(void)
{
struct S s = {foo()}; /*problem here*/
return s;
}

Explanation:-

unlike other automatic variable initializers,the expressions in an
initializer list for a struct,union,or array must be constant
expressions. constant expressions are not values of constant qualified
objects.they are expressions that trivially(without the use of global
or flow analysis) can be evaluated by the compiler at compile time

constant expressions in initializers can include the following

1) arithmetic constant expressions comprised of arithmetic constants of
various types and sizeof expressions ex:- (sizeof(struct S) +7)/8 * 8

2) Null pointer constants

3) addresses of static duration objects,such as file scope variables or
of function designators
Now my questions are as follows:-

1) To what extent this explanation is true ??
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!
3) what is this global or flow analysis ??

Aug 6 '06 #1
2 1988
Aarklon posted:
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!

Make sure you're not using a C++ compiler (C++ allows what you're trying to
do).

--

Frederick Gotham
Aug 6 '06 #2
aa*****@gmail.com writes:
Hi all,

Recently i was reading a c book which contained a section called C
pitfalls.

it had a paragraph on the following lines:-

-------------------------------------------------------------------
using nonconstant expressions in an initializer list
-------------------------------------------------------------------
struct s {int i;};
int foo(void);

struct s bar(void)
{
struct S s = {foo()}; /*problem here*/
return s;
}

Explanation:-

unlike other automatic variable initializers,the expressions in an
initializer list for a struct,union,or array must be constant
expressions. constant expressions are not values of constant qualified
objects.they are expressions that trivially(without the use of global
or flow analysis) can be evaluated by the compiler at compile time
I hope it said "const qualified", not "constant qualified".
constant expressions in initializers can include the following

1) arithmetic constant expressions comprised of arithmetic constants of
various types and sizeof expressions ex:- (sizeof(struct S) +7)/8 * 8

2) Null pointer constants

3) addresses of static duration objects,such as file scope variables or
of function designators
Now my questions are as follows:-

1) To what extent this explanation is true ??
It's true for C90. I think this restriction was relaxed in C99.
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!
Try compiling in strict C89C90 mode ("-ansi -pedantic").
3) what is this global or flow analysis ??
Some expressions can potentially be evaluated at compilation time by
analyzing data flow, i.e., where particular values came from. For
example:

int x = 10;
int y = 20;
int z = x + y;
printf("z = %d\n", z);

An optimizing compiler can determine that z's initial value is 30, and
might even replace the printf() call with
puts("z = 30");

To do so, it has to prove that the values of x and y cannot have
changed before the expression "x + y" is evaluated, and that the value
of z cannot have changed before the printf call.

C defines certain expressions as being constant, and requires them to
be evaluated at compilation time. The language's definition of a
constant expression is designed so that it doesn't depend on this kind
of analysis.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 6 '06 #3

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

Similar topics

72
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
3
by: spipyeah | last post by:
My compiler is known not to be very compliant. So I can't base myself on the fact that the following doesn't work to determine whether or not it is legal C++. In an initializer list, can you...
6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
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: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
7
by: Najib Abi Fadel | last post by:
Is postgres going to support in a future release the use of SUBSELECT in a CHECK expression ?? Thx
2
by: cody | last post by:
class Test { IList list = new ArrayList(); MyCollection list2 = new MyCollection (list); } Leads to this error. I know I could initialize them in the ctor but I'm asking myself where this...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.