exercise 3.9, C++ Primer 4/e

A

arnuld

hai all,

here is the code:

#include <iostream>
#include <string>

int main() {
std::string s;
std::cout << s[0] << std::endl;
}


now i know it is WRONG as "s" is an empty string (implicit
initialisation by the default constructor of string class from Std.
Lib.). what i want to know is, when i run this code onto my Debian box
"gcc 3.3.5" it outputs the following characters:

"^@"

what is that? some non-printable character from ASCII, i guess.

do we call this an undefined behaviour?

thanks

"arnuld"
www.arnuld.blogspot.com
 
R

red floyd

arnuld said:
hai all,

here is the code:

#include <iostream>
#include <string>

int main() {
std::string s;
std::cout << s[0] << std::endl;
}


now i know it is WRONG as "s" is an empty string (implicit
initialisation by the default constructor of string class from Std.
Lib.). what i want to know is, when i run this code onto my Debian box
"gcc 3.3.5" it outputs the following characters:

"^@"

what is that? some non-printable character from ASCII, i guess.

do we call this an undefined behaviour?

Yes. Therefore, anything can happen. In this case, "anything" includes
working "as expected" (whatever that is), crashing your app, crashing
your computer, dialing up the pentagon and starting WWIII, or causing
your monitor to explode.
 
A

arnuld

red said:
Yes. Therefore, anything can happen. In this case, "anything" includes
working "as expected" (whatever that is), crashing your app, crashing
your computer, ok

dialing up the pentagon and starting WWIII,
WHAT!

or causing your monitor to explode.

are you really seriuos?

"arnuld"
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top