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

Stroustrup 5.9, exercise 9

i am unable to think of any solution to this problem :-( :

"Find an example where it would make sense to use a name in its own
initializer."

???

Apr 2 '07 #1
6 1221
* arnuld:
i am unable to think of any solution to this problem :-( :

"Find an example where it would make sense to use a name in its own
initializer."

???
This question has been discussed before, so presumable googling would help.

As I recall my own example was the only practical usage:

Whatever o = {sizeof(o)};
STATIC_ASSERT( offsetof( Whatever, byteSize ) == 0 );

which sets the first member of o to the struct's size, and
zero-initializes the rest.

Later on, without discussing that question, someone in this group came
up with a "trick" that involved

Whatever o(o);

but I don't recall exactly what that was meant to accomplish.

Oh, wait, it's coming back to me ... I think it was about avoiding
compiler silly-warnings that a declared object was not "used". For the
cases where all the object declaration is meant to accomplish is to do
something in the constructor, and then clean up (undo) in destructor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #2
arnuld wrote:
i am unable to think of any solution to this problem :-( :

"Find an example where it would make sense to use a name in its own
initializer."

???
Not sure what is meant here, but maybe this

SomeType object(&object);

What would SomeType do with its own address? It could store it
to indicate the end of the chain of objects (instead of NULL, for
example).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #3
Alf P. Steinbach wrote:
* arnuld:
i am unable to think of any solution to this problem :-( :

"Find an example where it would make sense to use a name in its own
initializer."

???

This question has been discussed before, so presumable googling would help.

As I recall my own example was the only practical usage:

Whatever o = {sizeof(o)};
STATIC_ASSERT( offsetof( Whatever, byteSize ) == 0 );

which sets the first member of o to the struct's size, and
zero-initializes the rest.

Later on, without discussing that question, someone in this group came
up with a "trick" that involved

Whatever o(o);

but I don't recall exactly what that was meant to accomplish.

Oh, wait, it's coming back to me ... I think it was about avoiding
compiler silly-warnings that a declared object was not "used". For the
cases where all the object declaration is meant to accomplish is to do
something in the constructor, and then clean up (undo) in destructor.
well, to my *surprise* this works:

-----------------------------------------
#include<iostream>

int x = x;

int main()
{
return 0;
}
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O test.cpp
[arch@voodo tc++pl]$ ./a.out

----------------------------------------
BUT this did not and i started this post because earlier i tried this:

----------------------------------------
#include<iostream>

int main()
{
int x = x;

return 0;
}

[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O test2.cpp
test2.cpp: In function 'int main()':
test2.cpp:6: warning: 'x' is used uninitialized in this function
[arch@voodo tc++pl]$
-----------------------------------

i want to know the WHY

????

Apr 2 '07 #4
* arnuld:
>Alf P. Steinbach wrote:
* arnuld:
>>i am unable to think of any solution to this problem :-( :

"Find an example where it would make sense to use a name in its own
initializer."

???
This question has been discussed before, so presumable googling would help.

As I recall my own example was the only practical usage:

Whatever o = {sizeof(o)};
STATIC_ASSERT( offsetof( Whatever, byteSize ) == 0 );

which sets the first member of o to the struct's size, and
zero-initializes the rest.

Later on, without discussing that question, someone in this group came
up with a "trick" that involved

Whatever o(o);

but I don't recall exactly what that was meant to accomplish.

Oh, wait, it's coming back to me ... I think it was about avoiding
compiler silly-warnings that a declared object was not "used". For the
cases where all the object declaration is meant to accomplish is to do
something in the constructor, and then clean up (undo) in destructor.

well, to my *surprise* this works:

-----------------------------------------
#include<iostream>

int x = x;

int main()
{
return 0;
}
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O test.cpp
[arch@voodo tc++pl]$ ./a.out

----------------------------------------
BUT this did not and i started this post because earlier i tried this:

----------------------------------------
#include<iostream>

int main()
{
int x = x;

return 0;
}

[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O test2.cpp
test2.cpp: In function 'int main()':
test2.cpp:6: warning: 'x' is used uninitialized in this function
[arch@voodo tc++pl]$
-----------------------------------

i want to know the WHY

????
That's because namespace scope variables (such as the first one) are
zero-initialized first of all, and only then is the stated
initialization, if any, performed. Which means the first program's x is
guaranteed to be zero, just as if you hadn't specified an
initialization. The specified initialization initializes x with itself,
which is at this point 0 (earlier zero-initialization), so, it's 0.

There is no automatic zero-initialization of automatic variables, like
the one in your second program.

Thus, the second program's initialization initializes x with itself,
which at this point has an /indeterminate/ value, or in other words,
arbitrary bits, garbage. So the compiler helpfully offers a warning.
You're allowed to do that, or just about anything, but since it's
/usually/ a mistake, the compiler warns about it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #5
Alf P. Steinbach wrote:
* arnuld:
>well, to my *surprise* this works:
>#include<iostream>

int x = x;
>BUT this did not and i started this post because earlier i tried this:
[...]
>int main()
{
int x = x;
>i want to know the WHY
That's because namespace scope variables (such as the first one) are
zero-initialized first of all, and only then is the stated
initialization, if any, performed.
I thought, that zero initialization takes place *by default*, i.e. only
if there is no explicit initialization.
I'd expect undefined behavior, because the right hand side is not
initialized yet.
There is no automatic zero-initialization of automatic variables, like
the one in your second program.
Agreed.
Thus, the second program's initialization initializes x with itself,
which at this point has an /indeterminate/ value, or in other words,
arbitrary bits, garbage. So the compiler helpfully offers a warning.
Agreed.
You're allowed to do that, or just about anything, but since it's
/usually/ a mistake, the compiler warns about it.
I'd expect, that reading garbage leads to undefined behavior.

I have no standard to look it up, but replacing `int' with
`std::string', my compiler (g++-3.3 -ansi -pedantic -Wall) emits no
diagnosis at all, but the first (static) program causes a "Segmentation
fault", and the second (auto) prints "Aborted". Here's my test code:

#include<iostream>
#include<string>

//std::string stat_s = stat_s; // "Segmentation Fault"

int main()
{
// std::cout << "stat: " << stat_s << std::endl;
std::string auto_s = auto_s;
std::cout << "auto: " << auto_s << std::endl; // "Aborted"
}

My advice to arnuld:
1. Don't do never ever use the _value of x_ in the initializer of x.
2. If you do, don't expect any specific behavior from your compiler. It
may emit a diagnosis, produce illegal instructions, or delete your emails.

But the original question was about using the _name of x_ in its own
initializer. As the value is still undefined, I can imagine only two
formal cases :
1. sizeof( x ) -- but you can use sizeof with the type of x as well here.
2. &x, (or &x.member), which is well defined (the ampersand may be
hidden, if the ctor expects a reference). But remember: you may store
the address, but you must not yet dereference it.
Apr 2 '07 #6
* Ralph D. Ungermann:
Alf P. Steinbach wrote:
>* arnuld:
>>well, to my *surprise* this works:
>>#include<iostream>

int x = x;
>>BUT this did not and i started this post because earlier i tried this:
[...]
>>int main()
{
int x = x;
>>i want to know the WHY

>That's because namespace scope variables (such as the first one) are
zero-initialized first of all, and only then is the stated
initialization, if any, performed.

I thought, that zero initialization takes place *by default*, i.e. only
if there is no explicit initialization.
No, it takes place before any dynamic initialization.

However, under certain circumstances the compiler is allowed to replace
the dynamic initialization with static initialization (an optimization).

I'd expect undefined behavior, because the right hand side is not
initialized yet.
It is.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #7

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

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
7
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
by: arnuld | last post by:
/* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice: 1.) using ar array of char...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.