473,406 Members | 2,713 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,406 software developers and data experts.

Unintialized Pointer

Dear Experts,

In the following Program

class A
{
public:
void print()
{
cout<<"Hello World"<<endl;
}
};

int main()
{
A *a;
(*a).print();
}

If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?

Regards,
Siddharth

May 16 '07 #1
14 1473
siddhu wrote:
Dear Experts,

In the following Program

class A
{
public:
void print()
{
cout<<"Hello World"<<endl;
}
};

int main()
{
A *a;
(*a).print();
}

If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?
The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

Note that working "as expected" is one of the options, since it is part
of "anything".

May 16 '07 #2
red floyd wrote:
The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.
I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.

Sure, the letter of the expression "undefined behaviour" might "allow"
the compiler to do *anything*. However, I think that the spirit of that
expression simply means "the effects can be random because the values
cannot be guaranteed". In the specific context of trying to read an
uninitialized variable it means that the variable could have any value,
and no specific value is specified by the standard. It certainly does
not mean that trying to read an uninitialized variable will "call
NORAD and start WW3".

Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?

May 16 '07 #3
siddhu wrote:

If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?
There is no defined behavior for Undefined Behavior.


Brian
May 16 '07 #4
siddhu wrote:
If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?
It can actually be proven mathematically that it's impossible for
a program to determine, in the general case, if a certain piece of
code is called or not. Assume this:

int i;
if(someFunction()) i = 5;
use(i);

It might be that someFunction() always returns true and thus there's
no problem in the above code, but in the general case there exists no
algorithm to determine that. There can't exist such an algorithm. This
problem is related to http://en.wikipedia.org/wiki/Halting_problem

The only thing the compiler can do is to warn that the variable
*might* be used uninitialized, and if in your case it doesn't, then
you should turn a higher level of warnings on (or use a better
compiler).
May 16 '07 #5
Juha Nieminen wrote:

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and
completely wacky thing here>.

Sure, the letter of the expression "undefined behaviour" might
"allow" the compiler to do anything. However, I think that the spirit
of that expression simply means "the effects can be random because
the values cannot be guaranteed".
Nonsense. The effects need not be random at all. They may be pretty
consistent, which is how many worms and viruses work.
In the specific context of trying
to read an uninitialized variable it means that the variable could
have any value, and no specific value is specified by the standard.
Which means, as it's pointer, where it accessing is not specified.
It certainly does not mean that trying to read an uninitialized
variable will "call NORAD and start WW3".
In fact, Very Bad Things have happened through undefined behavior.

Brian

May 16 '07 #6
Juha Nieminen wrote:
red floyd wrote:
>The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.
Are you implying that HAL 9000's actions in the film 2001 aren't a
result of UB? Seems to me that's exactly what happened.... :-)
May 16 '07 #7
Juha Nieminen wrote:
red floyd wrote:
>The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.
[redacted]
Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?
Note that I left off some of the more improbable ones such as "causing
monkeys to fly out of my butt".
May 16 '07 #8

"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
siddhu wrote:
>If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?

It can actually be proven mathematically that it's impossible for
a program to determine, in the general case, if a certain piece of
code is called or not. Assume this:

int i;
if(someFunction()) i = 5;
use(i);

It might be that someFunction() always returns true and thus there's
no problem in the above code, but in the general case there exists no
algorithm to determine that. There can't exist such an algorithm. This
problem is related to http://en.wikipedia.org/wiki/Halting_problem

The only thing the compiler can do is to warn that the variable
*might* be used uninitialized, and if in your case it doesn't, then
you should turn a higher level of warnings on (or use a better
compiler).
The question was if the Standard allowed the code to work properly when
using an uninitialized pointer to a class. (And when he said it "works
fine", I assume he meant it runs without crashing or other detectable bad
behavior.)

According to the Standard, dereferencing an uninitialized pointer is
undefined behavior. It might work, it might not. The Standard doesn't
specify how it should behave.

As to whether the compiler could warn you that the specific code shown was a
problem, it's certainly possible. It may not be possible to always know (at
compile time) whether a pointer will be initialized or not when it is
eventually used, but the specific code shown certainly "could* be detected
as a problem by a compiler. (But that wasn't the OP's question.)

-Howard
May 16 '07 #9

"red floyd" <no*****@here.dudewrote in message
news:Iy**************@newssvr19.news.prodigy.net.. .
Juha Nieminen wrote:
>red floyd wrote:
>>The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.
[redacted]
> Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?

Note that I left off some of the more improbable ones such as "causing
monkeys to fly out of my butt".
If I recall correctly - and I don't - the Standards Committee discussed
restricting "undefined behavior" to explicitly exclude that particularly
nasty behavior. The proposal was shot down when it was pointed out that
"someone might write code to *intentionally* cause monkeys to fly out of red
floyd's butt... and then what if THAT code exhibited undefined behavior?!?"

(Plus it wouldn't really be "undefined behavior" if said behavior were
excluded, eh?)

-Howard
May 16 '07 #10
Howard wrote:
>
The proposal was shot down when it was pointed out that
"someone might write code to *intentionally* cause monkeys to fly out of red
floyd's butt... and then what if THAT code exhibited undefined behavior?!?"
So they discussed *my* butt in the Standards Committee? I'm not sure
whether to be flattered, scared, or disturbed! :-)

Thanks for the laugh, Howard!

--
red floyd
May 16 '07 #11

"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
red floyd wrote:
>The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.

Sure, the letter of the expression "undefined behaviour" might "allow"
the compiler to do *anything*. However, I think that the spirit of that
expression simply means "the effects can be random because the values
cannot be guaranteed". In the specific context of trying to read an
uninitialized variable it means that the variable could have any value,
and no specific value is specified by the standard. It certainly does
not mean that trying to read an uninitialized variable will "call
NORAD and start WW3".

Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?
Right, unless you're working on the whitehouse phone system programming that
the president uses. And some undefined behavior calls the wrong number...
May 17 '07 #12
Juha Nieminen wrote:
:: red floyd wrote:
::: The standard says that such behavior is undefined. Undefined
::: behavior can do anything, including but not limited to:
:::
::: * working "as expected"
::: * core dumping
::: * reformatting your hard drive
::: * calling NORAD and starting World War III
::: * Murdering the crew and locking the pod bay doors.
::
:: I have never understood why it's such a common "joke" to say that
:: "undefined behavior" means <insert physically impossible and
:: completely wacky thing here>.
::
:: Sure, the letter of the expression "undefined behaviour" might
:: "allow" the compiler to do *anything*. However, I think that the
:: spirit of that expression simply means "the effects can be random
:: because the values cannot be guaranteed".

That's the case for "unspecified behaviour". A certain number of
outcomes are possible, we just cannot generally tell which one.

:: In the specific context
:: of trying to read an uninitialized variable it means that the
:: variable could have any value, and no specific value is specified
:: by the standard. It certainly does not mean that trying to read an
:: uninitialized variable will "call NORAD and start WW3".

It is not at all physically impossible!

Let's assume that the OP programs a radar computer in an early warning
system. The computer contains a modem with memory mapped ports. To
make sure that the operators will not use the modem to download
"pr0n", the number to call is hardwired to NORAD red-alert.

Now, accessing one specific port of the modem triggers the actual
call. It doesn't matter if it is a read or a write access, it is the
actual address that is the trigger.

Let's try this out with a random uninitialized pointer...

:: Why can't people just tell what may happen *in practice* instead
:: of coming up with all the tired overly-repeated jokes?

In practice it just works anyway, sometimes.
Bo Persson
May 17 '07 #13
Juha Nieminen <no****@thanks.invalidwrote in news:464b7ae5$0$31527
$3*******@news.song.fi:
red floyd wrote:
>The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.
It's a fun way to illustrate that you should not invoke Undefined
Behaviour. I prefer to use the example of "Invoking UB may format your
hard drive.". But same idea.
Sure, the letter of the expression "undefined behaviour" might
"allow"
the compiler to do *anything*. However, I think that the spirit of that
expression simply means "the effects can be random because the values
cannot be guaranteed". In the specific context of trying to read an
uninitialized variable it means that the variable could have any value,
and no specific value is specified by the standard. It certainly does
not mean that trying to read an uninitialized variable will "call
NORAD and start WW3".

Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?
Because if someone were told what may happen in practice, they'd the just
turn around and do the Undefined Behaviour anyway, becasue "in practice"
nothing goes wrong. Until you port the code somewhere where it does go
wrong. Then either that programmer, or whomever is now porting the code,
complains about why doesn't such-and-such work. It works on Windows (or
Linux, or wherever they originally wrote the code), why not here?

Undefined Behaviour is Undefined Behaviour. There is no "what may happen
in practice". Anything can happen. Anything ranging from "working
properly" to crashing to "format your HD" (crashing with style?) to
"avian simians emerging from somewhere" (if the compiler implementor is
sufficiently demented....) to causing your CRT to explode (not that
unreasonable... older video cards and such didn't have controls on their
frequencies, so they could overdrive the electron gun in the CRT causing
it to overheat and explode.... so doing something like setFrequency
(newFreq) where newFreq is an uninitialized variable (reading from an
uninitialized variable is Undefined Behaviour), could set the frequency
too high and break your CRT).

May 17 '07 #14
On May 16, 11:43 pm, Juha Nieminen <nos...@thanks.invalidwrote:
red floyd wrote:
The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:
* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.
I have never understood why it's such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.
Because it highlights the fact that no list of possible
behaviors can be exhaustive.
Sure, the letter of the expression "undefined behaviour" might "allow"
the compiler to do *anything*. However, I think that the spirit of that
expression simply means "the effects can be random because the values
cannot be guaranteed". In the specific context of trying to read an
uninitialized variable it means that the variable could have any value,
and no specific value is specified by the standard.
Or that it could cause a hardware trap. As long as you're just
reading, those are probably the only possible behaviors (but I'm
not 100% sure about it).
It certainly does
not mean that trying to read an uninitialized variable will "call
NORAD and start WW3".
Why can't people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?
Well, I've seen dereferencing an uninitialized pointer hang the
machine, so that you had to power it off and then on again to do
anything with the machine. And I've seen writing through an
uninitialized pointer actually cause the next call to open to
render the disk unreadable, so that it required reformatting.
And I've worked on a system where an incorrect memory write
could cause the system to telephone your boss.

One of the reasons why C and C++ stress "undefined behavior" so
much is because they are used on such a wide variety of systems.
*IF* you're running application level software on a typical
workstation or (modern) PC, then about the worse thing that can
happen is a core dump (or whatever they call it under Windows),
but C/C++ are designed for a lot of other environments as well.
And a core dump can have serious secondary effects as well.
I've worked on systems where we had contractual penalties for
down time, and the time between a core dump and the time the
program was back up and running would cost us over 10000 Euros.
(But that's a secondary effect---a classical secondary effect of
buffer overflow, for example, is to allow unknown persons to
execute arbitrary code in priviledged mode on your machine.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
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

May 17 '07 #15

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
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. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
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: 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...
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
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...
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,...
0
isladogs
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...

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.