Why does this compile?

  • Thread starter Alexander Pandit
  • Start date
A

Alexander Pandit

here's the code :

using namespace std;

class A {
public:
A(int j): i(j) {
cout << "constructing an A object :" << i << endl;
}

~A() {
cout << "destructing an A object " << i << endl;
}

void show() {
cout << i << endl;
}

private:
int i;
};

int main() {

A *a;
a->show(); // <--- shouldn't this give a compile error , or does this
result in undefined behaviour?
return 0;
}
 
M

Mike Wahler

Alexander Pandit said:
here's the code :

#include <iostream>
#include said:
using namespace std;

class A {
public:
A(int j): i(j) {
cout << "constructing an A object :" << i << endl;
}

~A() {
cout << "destructing an A object " << i << endl;
}

void show() {

void show() const {
cout << i << endl;
}

private:
int i;
};

int main() {

A *a;
a->show(); // <--- shouldn't this give a compile error , or does this
result in undefined behaviour?

Undefined behavior. No diagnostic is required (but neither is
one prohibited).
return 0;
}

I try to prevent such errors by *always* initializing any objects
created.

-Mike
 
A

Alexander Pandit

Do you mean i should initialize it like :

A *a = NULL;
a->show(); // <-- Then I get a runtime-error

-Alex
 
A

angelo

Alexander said:
here's the code :

using namespace std;

class A {
public:
A(int j): i(j) {
cout << "constructing an A object :" << i << endl;
}

~A() {
cout << "destructing an A object " << i << endl;
}

void show() {
cout << i << endl;
}

private:
int i;
};

int main() {

A *a;
a->show(); // <--- shouldn't this give a compile error , or does this
result in undefined behaviour?
return 0;
}

If you added a line:
a = new A(1);
the following statements would seem absolutely natural and correct,
right? But the point is the C/C++ doesn't check if a pointer is assigned
a valid value or not, so as far as syntax is concerned, it has no
error at all.
Moreover, as you have said, the result is undefined, so the compiler's
not generating an error message is just natural.
 
M

Mike Wahler

Alexander Pandit said:
Do you mean i should initialize it like :

A *a = NULL;

No, initialize it with the address of a type 'A'
object. If the object doesn't exist yet, don't
define the pointer until it does. In any case,
in the context you showed, I see no reason
to use a pointer at all anyway. Just define
the object directly:

A a(42);
a.show();
a->show(); // <-- Then I get a runtime-error

You may or may not get a 'run-time error'.
The behavior is still undefined.

BTW please don't top-post. Thanks.

-Mike
 
A

Alexander Pandit

thank you.

-alex

Mike Wahler said:
No, initialize it with the address of a type 'A'
object. If the object doesn't exist yet, don't
define the pointer until it does. In any case,
in the context you showed, I see no reason
to use a pointer at all anyway. Just define
the object directly:

A a(42);
a.show();


You may or may not get a 'run-time error'.
The behavior is still undefined.

BTW please don't top-post. Thanks.

-Mike
 
R

Ron Natalie

Alexander said:
Do you mean i should initialize it like :

A *a = NULL;
a->show(); // <-- Then I get a runtime-error

-Alex
Not necessarily. Even the above code is undefined behavior
(might get a runtime error, might just hose up some other
memory in your program).

A* a = NULL;

if(a) a->show(); //

or intialize a with the address of an actual object.
 
K

Karthik Kumar

Alexander said:
here's the code :

using namespace std;

class A {
public:
A(int j): i(j) {
cout << "constructing an A object :" << i << endl;
}

~A() {
cout << "destructing an A object " << i << endl;
}

void show() {
cout << i << endl;
}

private:
int i;
};

int main() {

A *a;
a->show(); // <--- shouldn't this give a compile error , or does this
result in undefined behaviour?
return 0;
}
This is *undefined behaviour*. The compiler would not flag an error
since this is perfectly valid as per the C++ language syntax.
But it can definitely flag a warning since we are dealing with in an
uinitialized local variable here. Set your compiler warning level
at its highest level and it *might* catch this one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top