473,396 Members | 1,826 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.

Strange value

Hi people,

I'm playing around with Bartosz Milewski's code at the moment, and I
got the following strange results upon execution of the code included
further below. Please be aware that I deliberately tweaked the code to
produce these results:

Matter for 0 created
Hello from world 1.
Matter for 2009196833 created
Hello from world 2.
Good bye from world 2.
Matter in 2009196833 annihilated
Good bye from world 1.
Matter in 0 annihilated

The code is:

#include <iostream>

class Matter
{
public:
Matter (int i)
: _identifier (i)
{
std::cout << " Matter for " << _identifier << " created\n";
}

~Matter ()
{
std::cout << " Matter in " << _identifier << " annihilated\n";
}
private:
const int _identifier;
};

class World
{
public:
World (int i)
: _identifier (i), _matter (_identifier) // initializing
embeddings
{
std::cout << "Hello from world " << _identifier << ".\n";
}

~World ()
{
std::cout << "Good bye from world " << _identifier << ".\n";
}
private:
const Matter _matter; // Embedded object of type Matter
const int _identifier;
};

World TheUnivers (1);

int main ()
{
World myWorld (2);
}

Now, I know that "int" should be declared before "Matter" in class
"World". I deliberately changed the order to check out the results.
What on earth is 2009196833? Shouldn't this value be zero, as there is
no preceding initialization for the "Matter" object within the
"myWorld" object? i.e., the "Matter" for both "World" objects should
be equal to zero...?

Strange.

Deets
Jul 22 '05 #1
7 1688
"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
Hi people,

I'm playing around with Bartosz Milewski's code at the moment, and I
got the following strange results upon execution of the code included
further below. Please be aware that I deliberately tweaked the code to
produce these results: ....
class World
{
public:
World (int i)
: _identifier (i), _matter (_identifier) // initializing
.....
const Matter _matter; // Embedded object of type Matter
const int _identifier;
};

Note that the compiler always initializes data members in the
order of their declaration. For instance, _matter before _identifier.
As a consequence: _matter (_identifier)
leads to undefined behavior: it initializes _matter with
a variable that hasn't been initialized yet.

.... Now, I know that "int" should be declared before "Matter" in class
"World". I deliberately changed the order to check out the results.
What on earth is 2009196833? Shouldn't this value be zero, as there is
no preceding initialization for the "Matter" object within the
"myWorld" object? i.e., the "Matter" for both "World" objects should
be equal to zero...?


Welcome to "Undefined Behavior" (a.k.a. UB). To allow optimal
performance, the C++ language definition says things like
"if no value has been assigned to a variable, it can have any value,
and it is even illegal to try to read that value". If your code
triggers UB, anything may happen at run-time, there is no safety net.

In C++, you must know what you are doing.

This said, several compilers or code verification tools can flag
the error above, and emit a warning during compilation.
hth -Ivan
--
http://ivan.vecerina.com
Jul 22 '05 #2
"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
Now, I know that "int" should be declared before "Matter" in class
"World". I deliberately changed the order to check out the results.
What on earth is 2009196833? Shouldn't this value be zero, as there is
no preceding initialization for the "Matter" object within the
"myWorld" object? i.e., the "Matter" for both "World" objects should
be equal to zero...?


Uninitialized != zero. If you want an object's initial
value to be zero(*), you must specifically initialize it
with that value (except for statically defined objects).

-Mike
* for object types where 'zero' has meaning.
Jul 22 '05 #3
Thanks guys.

When I tweaked the code I was aware that I was passing Matter (int i)
uninitialized arguments. I did this on purpose to see what would
happen. I'm curious to know where the output values come from. The
first object that gets created receives an assumed value of zero, so I
would have thought that the second object would also get zero. (Is
there some sort of default initializing taking place?) Instead it gets
that massive value (2009196833), which looks like it might be the
value limit of an integer, or something. The third one is 2147348480,
the fourth is 8 and the fifth is 2009246515.

These values look pretty random. But I'm not convinced. Regardless of
when I compile them or how often I compile them, the values are the
same. There must be more to it...and this is what I hope to find out.

Cheers, and thanks again.

Deets
Jul 22 '05 #4

"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
Thanks guys.

When I tweaked the code I was aware that I was passing Matter (int i)
uninitialized arguments. I did this on purpose to see what would
happen. I'm curious to know where the output values come from.
Anywhere. Everywhere. Nowhere. Your nasal passages. Or somewhere
else. The language definition specifically denotes the result
of such situations as *undefined*, thus there is no predictable
behavior.
The
first object that gets created receives an assumed value of zero,
Assumed by you. But this is an incorrect assumption.
so I
would have thought that the second object would also get zero.
so any deductions following from your incorrect assumption are
suspect.
(Is
there some sort of default initializing taking place?)

No.
Instead it gets
that massive
make that 'random'.
value (2009196833), which looks like it might be the
value limit of an integer,
Or it could be your shoe size, the age of the universe, or
some other value. The behavior is *undefined*.
or something. The third one is 2147348480,
the fourth is 8 and the fifth is 2009246515.
Three more random values, which are the result of *undefined*
behavior.

These values look pretty random.
They are. The code which produced them is specifically identified
by the language to have *undefined* behavior.
But I'm not convinced.
Up to you. :-)
Regardless of
when I compile them or how often I compile them, the values are the
same.
If you're using a multitasking OS (e.g. Windows) try compiling/
running it again with additional tasks already running, try it
on a different machine with the same compiler, etc.
But your results, whatever they might be, still cannot be used
to identify a specific, predictable behavior, from the language
perspective.

There must be more to it...and this is what I hope to find out.


There indeed can be much more to it (undefined behavior). The
possible manifestations of UB are theoretically limitless.
So I suppose you'll keep busy for a while. :-)

-Mike
Jul 22 '05 #5
an********@fastmail.fm (Anon Email) wrote:
These values look pretty random. But I'm not convinced. Regardless of
when I compile them or how often I compile them, the values are the
same. There must be more to it...and this is what I hope to find out.


Leftover stuff from static initialization routines getting called
before main() starts, or from deallocated objects that had their
memory returned to the heap... stuff like that. It is not quite
"random", but it is undefined. If you compile with different flags, on
another platform, on a different version of your compiler, etc., you
may (or may not) get completely different values.

--
Dave O'Hearn
Jul 22 '05 #6
Thanks guys.

Interesting. I always find the concept of "randomness" or "undefined
behaviour" interesting when it comes to computers.

Cheers,

Deets
Jul 22 '05 #7
Thanks guys.

I love finding out what goes on behind the scenes.

Cheers,

Deets
Jul 22 '05 #8

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

Similar topics

11
by: F. Da Costa | last post by:
Hi, This is a strange issue I'v been staring at for half a day now. It concerns catching keys via the onkeydown handler. In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
6
by: WindAndWaves | last post by:
Hi Gurus The page below has a strange error. It seems to be working very well, just when you enter 8 or 9 for day, month or year then you get an error. I really have no idea where that is...
9
by: Christian Hubinger | last post by:
Hi! I've implemented some DropDown list in ASP.NET that use Ajax to fetch the data from the server. The javascript is written to call cascading to the bottom most dropdown in order to update...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is strange enough! it's not 3 but 2 ! I've...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
2
by: cmt | last post by:
Greetings I have a web page with an ASP script in it that is not displaying all the data from the SQL Server DB correctly in a form. I have one line in the code, that is doing some strange...
3
by: Tomasz J | last post by:
Hello Developers, I have a control derived from System.Web.UI.WebControls.WebControl. Control has this property: public string Value { set { _value = value; } get { return _value; }
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
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
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?
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
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
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.