C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unwanted Default Initialisation
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Tomás
Guest





PostPosted: Wed May 17, 2006 9:21 pm    Post subject: Unwanted Default Initialisation Reply with quote



The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get default
initialised?

We're given the choice with local variables, e.g.:

char s[65];

Versus:

char s[65] = {};


It'd be nice to have the choice with globals and statics in functions.
Just a thought.


I suppose you could circumvent the system:

template<class T>
class DoNotInit {

T object;

public:

DoNotInit() {}

/* Rather than:

DoNotInit() : object() {}
*/


T &operator()() { return object; }
};


void FuncInit()
{
static char s[65];

s[17] = '5';
};

void FuncDoNotInit()
{
static DoNotInit<char[65]> dni;

static char (&s)[65] = dni();

s[17] = '5';
}


With all of C's claims of efficiency, it looks like they dropped the ball
on this one.

(One thing I'll say about C++ though: If you have a problem with the
language, you can get around it most of the time.)

-Tomás

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Greg Herlihy
Guest





PostPosted: Thu May 18, 2006 6:21 am    Post subject: Re: Unwanted Default Initialisation Reply with quote



Tomás wrote:
Quote:
The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get default
initialised?

We're given the choice with local variables, e.g.:

char s[65];

Versus:

char s[65] = {};


It'd be nice to have the choice with globals and statics in functions.
Just a thought.


I suppose you could circumvent the system:

template<class T
class DoNotInit {

T object;

public:

DoNotInit() {}

/* Rather than:

DoNotInit() : object() {}
*/


T &operator()() { return object; }
};


void FuncInit()
{
static char s[65];

s[17] = '5';
};

void FuncDoNotInit()
{
static DoNotInit<char[65]> dni;

static char (&s)[65] = dni();

s[17] = '5';
}


With all of C's claims of efficiency, it looks like they dropped the ball
on this one.

(One thing I'll say about C++ though: If you have a problem with the
language, you can get around it most of the time.)

-Tomás

Because space for global variables must be allocated in a program's
image - every global variable will be initialized with a specific value
at program startup. So in the interests of standardization, it makes
sense for all programs to initialize globals with the same, specific
value (0) instead of leaving each program to choose a specific value of
its own. There is certainly no incurred cost in terms of a program's
efficiency in zero-initializing global variables. Local variables, on
the other hand, re-use memory - so the initial value of a local
variable cannot be guaranteed without explicitly assigning it a value
upon allocation; so in the case of local variables, zero-initialization
would incur a cost.

In fact it is not possible to avoid zero-initialization for global
variables. The 65 character array in the static dni variable in the
sample program has in fact been zero-initialized - no differently than
had it been declared a global array on its own. In fact, in order to
initialize a global variable with random values, the program would have
to execute an initialization routine to do so explicitly. In other
words the default zero-initialization behavior is in fact the more
efficient behavior than the behavior which the DoNotInit class template
attempts to - but fails to - attain.

Greg


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Jack Klein
Guest





PostPosted: Thu May 18, 2006 6:21 am    Post subject: Re: Unwanted Default Initialisation Reply with quote



On Wed, 17 May 2006 15:59:14 CST, "Tomás" <No.Email (AT) Address (DOT) ucar.edu>
wrote in comp.std.c++:

Quote:

The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get default
initialised?

[snip]

Quote:
With all of C's claims of efficiency, it looks like they dropped the ball
on this one.

What makes you think this is all that inefficient? Considering that C
and C++ were developed in (and primarily for) hosted environments,
where some OS mechanism copies the executable image from secondary
storage into processor memory, and generally has to do some fixing up
anyway, just how expensive do you think it is to zero out a block of
memory?

Especially when you consider that at least some operating systems zero
out all the memory accessible to a program before it begins execution,
for security reasons. All the executable format and OS loader have to
do is not to overwrite those zeros.

On platforms where all-bits-zero is not a valid representation for a
null pointer and/or a floating point 0.0, there is more work involved,
but those are in a very small minority.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Tomás
Guest





PostPosted: Thu May 18, 2006 3:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

===================================== MODERATOR'S COMMENT:

This thread is now drifting into areas unrelated to C++ standardization.
Let's try not to drift too far into basic OS technology. -sdc


---
Steve Clamage, stephen.clamage (AT) sun (DOT) com


===================================== END OF MODERATOR'S COMMENT
Greg Herlihy posted:

Quote:
Because space for global variables must be allocated in a program's
image - every global variable will be initialized with a specific
value at program startup.


I hadn't considered this. I can program in C++, but my knowledge of what
goes on under the hood is limited. I know a bit about assembly language,
but concepts like the "stack" escape me... I've tried reading up on it a
couple of times, but I find books on the topic to be quite unaccessible.
(Which is quite strange considering I can leisurely read through books
like TC++PL by Bjarne).

I know how assembly code works; it's basically just machine code written
in English. Assembly like the following:

mov ax, 34
inc qw

gets turned into hexadecimal codes:

0A EC F3 A3


Are you saying that the global and static objects get stored in the same
place as this machine code? And therefore that their value is hardcoded
in the executable, something like:

0A EC F3 A3 00 00 00 00 00 00 00

?

-Tomás

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
kanze
Guest





PostPosted: Thu May 18, 2006 5:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Tomás wrote:
Quote:
Why do global objects and also, static objects in functions,
get default initialised?

Because everything in C++ gets default initialized. The only
difference with regards to static objects is that they are zero
initialized before hand. This was doubtlessly done because it
was free on the early Unix systems -- everything was zero
initialized, whether the C language specification required it or
not, so they decided to make it a requirement.

[...]
Quote:
With all of C's claims of efficiency, it looks like they
dropped the ball on this one.

In what sense? Logically, they probably should have required
the initialization everywhere, but on older machines, it can be
expensive for larger objects, so they just required it where it
was free. (Depending on how malloc/free is implemented, larger
objects are in fact zero initialized on a lot of implementations
anyway.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
John Nagle
Guest





PostPosted: Thu May 18, 2006 6:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Jack Klein wrote:
Quote:
On Wed, 17 May 2006 15:59:14 CST, "Tomás" <No.Email (AT) Address (DOT) ucar.edu
wrote in comp.std.c++:


The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get default
initialised?

The embedded computer world has some issues with C++ in this area. See

http://www.embedded.com/98/9812/9812pp.htm

These have mainly to do with whether const C++ objects can be initialized
at compile time and placed in ROM. In a typical deeply embedded environment,
at power-on, all the code and constants are in ROM, and all the data is random.
Anything beyond that needs to be handled in startup code. A memory clear of the
data areas has to explicitly be done before the constructors run to satisfy
the C++ rules.

John Nagle

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Ron Natalie
Guest





PostPosted: Sun May 21, 2006 9:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Tomás wrote:
Quote:
The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get default
initialised?

The bigger question, is why in an intelligent well thought out language
is default initialization skipped for only certain object in certain
arcane circumstances.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Tomás
Guest





PostPosted: Mon May 22, 2006 4:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Ron Natalie posted:

Quote:
Tomás wrote:
The die was cast decades ago... but nonetheless I'd like to discuss:

Why do global objects and also, static objects in functions, get defaul
t
initialised?

The bigger question, is why in an intelligent well thought out language
is default initialization skipped for only certain object in certain
arcane circumstances.


For efficiency. Use as little resources as possible, and make it as fast as
possible. Also... it's better to be explicit about what value you want,
e.g.:

static int *p = 0;

rather than:

static int *p;


-Tomás

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Guest






PostPosted: Mon May 22, 2006 5:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

"Tomás" wrote:
Quote:
Ron Natalie posted:
...
The bigger question, is why in an intelligent well thought out language
is default initialization skipped for only certain object in certain
arcane circumstances.


For efficiency. Use as little resources as possible, and make it as fast as
possible. Also... it's better to be explicit about what value you want,
e.g.:

static int *p = 0;

rather than:

static int *p;

That doesn't really address the question. Efficiency is an argument for
providing the option of not initializing a variable. The rest of your
answer could be taken as an argument for making explicit initialization
mandatory. However, your answer did not give a reason why, when an
explicit initializer is not provided, the variable should be default
initialized in some cases, and not initialized at all in others.


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Tomás
Guest





PostPosted: Mon May 22, 2006 7:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Quote:
However, your answer did not give a reason why, when an explicit
initializer is not provided, the variable should be default
initialized in some cases, and not initialized at all in others.


Answer: Because C++ is based on C, and this feature originated in C.

I've plenty of questions in my head to ask about the C++ programming
language, but there isn't much point asking them here because they were
around since the time of C. For example:

(Q) Why does "int" default to "signed int" rather than "unsigned int"?
I've always thought it would be much more natural to have "int" unsigned.

How far do you think C++ has progressed insomuch as abandoning its
backward compatibility with C?

I have an idea in my head: When the language is changed so that it breaks
existing code, would it be feesible to write a little program which
processes source code files to adjust the code to the changes? For
instance, if we changed the syntax of the initialiser list (just a
hypothetical of course), the little program could change:

int Blah::Blah(int &s) : p(5), r(22), t(s)
{

}

into:

int Blah::Blah(int &s) = { p(5); r(22); t(s) }
{

}



-Tomás

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
James Kanze
Guest





PostPosted: Thu May 25, 2006 3:21 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

No.Email (AT) Address (DOT) ucar.edu wrote:
Quote:
However, your answer did not give a reason why, when an
explicit initializer is not provided, the variable should be
default initialized in some cases, and not initialized at all
in others.

Answer: Because C++ is based on C, and this feature
originated in C.

That is the explination of this particular case. Arguably,
today, initialization should be required in every case. With
today's technology, the cost would usually be very close to zero
anyway.

Quote:
I've plenty of questions in my head to ask about the C++
programming language, but there isn't much point asking them
here because they were around since the time of C. For
example:

(Q) Why does "int" default to "signed int" rather than
"unsigned int"? I've always thought it would be much more
natural to have "int" unsigned.

Because the name is an abbreviation for "integer", and the set
of integers includes negative numbers. Because int was present
long before unsigned int, and changing it would have broken too
much existing code.

Quote:
How far do you think C++ has progressed insomuch as abandoning
its backward compatibility with C?

I think that many people today would say: not enough.

Quote:
I have an idea in my head: When the language is changed so
that it breaks existing code, would it be feesible to write a
little program which processes source code files to adjust the
code to the changes?

Usually yes. There are probably some subtle cases, however --
the change in the scope of a variable declared in a for, or the
introduction of two phased lookup, come to mind. (99% of the
time, these two changes result in an error. But in both cases,
it's possible to construct code which subtly changes semantics
with the change.)

--
James Kanze kanze.james (AT) neuf (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
David R Tribble
Guest





PostPosted: Sun Jun 11, 2006 6:33 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

Quote:
How far do you think C++ has progressed insomuch as abandoning
its backward compatibility with C?


James Kanze wrote:
Quote:
I think that many people today would say: not enough.

And probably as many people, including Bjarne and most of the
members of both the ISO C and ISO C++ committees, would
say that attempting to maintain as much compatibility between
the two languages is a desirable thing to do. (Bjarne has said
so in his "Sibling Rivalry" articles.) Or to say it the other way
around, allowing the two languages to drift too far apart is a
bad thing.
http://www.research.att.com/~bs/bs_faq.html#difference

Bear in mind that both languages continue to evolve, though
not as much as they did in their first one or two ISO revisions.
Many people (again, including Bjarne) are of the opinion that
the languages should pretty much stop evolving (except for
some minor semantic clean-ups) and settle down for the long
haul, and that most of the improvements be made (only) to the
standard libraries.

C and C++ are not that far apart today, and many conforming C
programs compile just fine as C++ programs (possibly with
just a few minor tweaks). Many C programmers (I would think)
became better programmers because of the influence of C++.
http://david.tribble.com/text/cdiffs.htm

And C++ programmers should not assume that C++ is superior
in all areas than C. (Take the issue of NULL, for example,
for which C has a cleaner solution.)

And finally, _both_ languages have features missing from them
that some people feel a "complete" language should provide
(e.g., automatic garbage collection, built-in support for threads,
etc.).

-drt

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Francis Glassborow
Guest





PostPosted: Wed Jun 14, 2006 1:24 am    Post subject: Re: Unwanted Default Initialisation Reply with quote

In article <1150042996.895868.31010 (AT) m38g2000cwc (DOT) googlegroups.com>, David
R Tribble <david (AT) tribble (DOT) com> writes
Quote:
Bear in mind that both languages continue to evolve, though
not as much as they did in their first one or two ISO revisions.
Many people (again, including Bjarne) are of the opinion that
the languages should pretty much stop evolving (except for
some minor semantic clean-ups) and settle down for the long
haul, and that most of the improvements be made (only) to the
standard libraries.
FOFL. Have you had a good look at the work on the next version of C++

including the contributions from Bjarne himself for things such as
concepts, new initialisation syntax etc.

Yes the rate of change is greatly reduced but it is much more than just
adding new libraries.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
kanze
Guest





PostPosted: Wed Jun 14, 2006 3:35 pm    Post subject: Re: Unwanted Default Initialisation Reply with quote

David R Tribble wrote:
Quote:
How far do you think C++ has progressed insomuch as
abandoning its backward compatibility with C?

James Kanze wrote:
I think that many people today would say: not enough.

And probably as many people, including Bjarne and most of the
members of both the ISO C and ISO C++ committees, would say
that attempting to maintain as much compatibility between the
two languages is a desirable thing to do.

I should have made it clear that I'm not one of those many:-).
I am fully in favor of maintaining a large degree of
compatibility with C. My impression, however, is that I am in a
minority -- Stroustrup agrees, but sometimes I get the
impression that he is very alone on this.

[...]
Quote:
And C++ programmers should not assume that C++ is superior
in all areas than C. (Take the issue of NULL, for example,
for which C has a cleaner solution.)

It does? Your the first person I've heard express that view.
(Using the word "clean" for the current situation in either
language is an abuse, but the C++ solution seems slightly less
dirty.)

The way it looks, however, C++ will have a much cleaner solution
after the next revision.

Quote:
And finally, _both_ languages have features missing from them
that some people feel a "complete" language should provide
(e.g., automatic garbage collection, built-in support for
threads, etc.).

Interesting point: in the parts I cut, you mentionned that the
language shouldn't evolve, but only the libraries. Where as the
two points you mention cannot be handled strictly in the
library; they require language modifications. (Ditto the
improvements in null pointer constants.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
David R Tribble
Guest





PostPosted: Sun Jun 18, 2006 4:26 am    Post subject: Re: Unwanted Default Initialisation Reply with quote

Quote:
How far do you think C++ has progressed insomuch as
abandoning its backward compatibility with C?


James Kanze wrote:
David R Tribble wrote:
Quote:
I think that many people today would say: not enough.


David R Tribble wrote:
Quote:
And probably as many people, including Bjarne and most of the
members of both the ISO C and ISO C++ committees, would say
that attempting to maintain as much compatibility between the
two languages is a desirable thing to do.


James Kanze wrote:
Quote:
I should have made it clear that I'm not one of those many:-).
I am fully in favor of maintaining a large degree of
compatibility with C. My impression, however, is that I am in a
minority -- Stroustrup agrees, but sometimes I get the
impression that he is very alone on this.

I'm agnostic about it myself. They are two different langages,
with two different audiences. Problems only really arise when
trying to use C libraries with C++ code. This is probably an
argument for C to be as upward-compatible with C++ as possible,
but not necessarily the other way around.


David R Tribble wrote:
Quote:
And C++ programmers should not assume that C++ is superior
in all areas than C. (Take the issue of NULL, for example,
for which C has a cleaner solution.)


James Kanze wrote:
Quote:
It does? Your the first person I've heard express that view.
(Using the word "clean" for the current situation in either
language is an abuse, but the C++ solution seems slightly less
dirty.)

Uh, yes. C++ uses '0', an integer constant, as its null pointer
constant (which leads to ambiguous expressions). C allows
'(void*)0', an expression with a pointer type, as its null pointer
constant (which is never ambiguous).

Many C++ authors, Bjarne included, have the extremely annoying
habit of saying "zero" instead of "null". Assigning zero to a pointer
is nonsense (since when is a pointer ever zero?); assigning null
to a pointer is semantically correct.

But neither language has a 'null' keyword (mostly for historical
reasons), which is probably the "cleanest" solution.


James Kanze wrote:
Quote:
The way it looks, however, C++ will have a much cleaner solution
after the next revision.

Praise be. Something resembling '(void*)0', which programmers
have been asking for for years?


David R Tribble wrote:
Quote:
And finally, _both_ languages have features missing from them
that some people feel a "complete" language should provide
(e.g., automatic garbage collection, built-in support for
threads, etc.).


James Kanze wrote:
Quote:
Interesting point: in the parts I cut, you mentionned that the
language shouldn't evolve, but only the libraries. Where as the
two points you mention cannot be handled strictly in the
library; they require language modifications. (Ditto the
improvements in null pointer constants.)

I didn't say _I_ believed that, but that many people do.
I don't think either C or C++ are as complete as they should be,
language-wise or library-wise.

-drt

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.