about pointer

D

David

1 #include <iostream>
2
3 class Name{
4 public:
5 char* s;
6 };
7
8 int main(){
9 Name *t= new Name;
10 Name t1;
11
12 cout<< t->s <<endl;
13 cout<<endl<<endl;
14 cout<< t1.s <<endl;
15 }

why line 12 and 14 generate different output?
12 generate (null)
14 generate irregular output
 
P

Phlip

David said:
1 #include <iostream>
2
3 class Name{
4 public:
5 char* s;
6 };
7
8 int main(){
9 Name *t= new Name;
10 Name t1;
11
12 cout<< t->s <<endl;
13 cout<<endl<<endl;
14 cout<< t1.s <<endl;
15 }

why line 12 and 14 generate different output?
12 generate (null)
14 generate irregular output

I will simplify your program. Its excesses are confusing you.
8 int main(){
9 char *s1;
10 char *s2;
11
12 cout<< s1 <<endl;
13 cout<<endl<<endl;
14 cout<< s2 <<endl;
15 }

If you run that, you will also get unpredictable results. Neither pointer
points anywhere specific. In your case, 'new' may have allocated Name out of
zero-packed storage, hence the zeros in 's' were interpreted as NULL. But
that's undefined behavior. Name has no non-trivial constructor, so it never
assigns anything to s's bits.

Read /Accelerated C++/, prefer std::string instead of character pointers,
and always assign something to your pointers.
 
V

Victor Bazarov

David said:
1 #include <iostream>
2
3 class Name{
4 public:
5 char* s;
6 };
7
8 int main(){
9 Name *t= new Name;
10 Name t1;
11
12 cout<< t->s <<endl;
13 cout<<endl<<endl;
14 cout<< t1.s <<endl;
15 }

why line 12 and 14 generate different output?

Your program is not supposed to generate any output, it is not
supposed to compile. Neither 'cout' nor 'endl' are defined. You
needed to say

using namespace std;

before using 'cout' or 'endl'.
12 generate (null)
14 generate irregular output

Considering that the 'namespace std' issue has been resolved, the
answer to your question is "your program causes undefined behaviour
because it tries to output something by dereferencing _uninitialised_
pointers".

Neither of the pointers is initialised. It is quite possible that
your compiler generates such code that causes any memory allocated
using 'new' to be set to zero. The Standard does not require that,
so relying on that in your work is not a good idea.

IOW, if you really want to know why you get different output, ask
in a newsgroup dedicated to your compiler. As far as Standard C++
is concerned, you may get _anything_.

Victor
 
J

John Harrison

David said:
1 #include <iostream>
2
3 class Name{
4 public:
5 char* s;
6 };
7
8 int main(){
9 Name *t= new Name;
10 Name t1;
11
12 cout<< t->s <<endl;
13 cout<<endl<<endl;
14 cout<< t1.s <<endl;
15 }

why line 12 and 14 generate different output?
12 generate (null)
14 generate irregular output

Because the two pointers are uninitialised and therefore the compiler is
allowed to do anything. This is called Undefined Behaviour in the C++
standard and your program has got it twice over.

john
 
D

David

John Harrison said:
Because the two pointers are uninitialised and therefore the compiler is
allowed to do anything. This is called Undefined Behaviour in the C++
standard and your program has got it twice over.

john

Actually the line 12 always generates (null), my question is actullay
does that mean it's always initialized to (null)? when when use "new
Name"?
 
V

Victor Bazarov

David said:
Actually the line 12 always generates (null), my question is actullay
does that mean it's always initialized to (null)? when when use "new
Name"?

Read what you wrote and tell me if I interpreted this right: "Actually
A is true. My question is, does that mean that A is true?"

And surprisingly, the answer is "no". Wow...

For POD expression 'new T' does NOT value-initialise the object. Only
the expression 'new T()' does. For POD, that is.

Now, read carefully: whatever result you get when you run your program
is not an indicator of anything. Your program has _undefined_ behaviour.
Outputting (null) is just one possible behaviour out of infinite number
of behaviours. Running a computer program with undefined behaviour does
NOT mean getting _different_ result every time you run it. If that were
true, the behaviour would be somehow defined (random results or whatever).
The behaviour is *undefined*. There is no sense to take that behaviour
as an example of anything _except_ of itself, undefined behaviour.

Victor
 
J

John Harrison

David said:
"John Harrison" <[email protected]> wrote in message

Actually the line 12 always generates (null), my question is actullay
does that mean it's always initialized to (null)? when when use "new
Name"?

As a practical matter (see Victor's reply for the factual answer) if you
want s to be initialised to NULL then you should write a constructor, that
is what they are there for.

class Name
{
public:
Name() : s(NULL) {}
char* s;
};

Now s will always be initialised to NULL.

john
 

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,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top