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 7 1696
"Anon Email" <an********@fas tmail.fm> wrote in message
news:83******** *************** **@posting.goog le.com... 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
"Anon Email" <an********@fas tmail.fm> wrote in message
news:83******** *************** **@posting.goog le.com... 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.
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
"Anon Email" <an********@fas tmail.fm> wrote in message
news:83******** *************** **@posting.goog le.com... 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 an********@fast mail.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
Thanks guys.
Interesting. I always find the concept of "randomness " or "undefined
behaviour" interesting when it comes to computers.
Cheers,
Deets
Thanks guys.
I love finding out what goes on behind the scenes.
Cheers,
Deets This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
peculiar. The 'offensive' code is included below.
The weird thing lies in the alert box preceded by // **.
* function textboxReplaceSelect *
|
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 onclick="javaScript:alert('document.forms(0)='+document.forms(0));
parent.myFunc(document.forms(0));" type="button" value="Open"
name="Button" ID="Button">
The strange...
|
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 coming from. Can you help?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
|
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 them all if a top one changed.
The strange thing is that the script runs without problems on IE but in
FireFox i get a "too much recursion" error and...
|
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.
----------------------------------------------
//example 1:
typedef int t_Array;
int main(int argc, char* argv)
| |
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 of 32 bit.
I made this stupid trial code:
---------------------------------------------
FILE *fout;
|
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 tested other values such as
(int)Math.Log(16,2),(int)Math.Log(4,2),(int)Math.Log(32,2)...et, they all
return the same value as their counterpart...
|
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 the same method (aka
delegate?). I use the Tag property within this method to determine what user
action is taking place. Very simple:
When...
|
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 things.
You can see the code below. The line in question is:
WTacctTotal = objRs("WTacctTotal")
|
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; }
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |