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

Similar Threads

C++ Primer exercise 3.13 17
C++ Primer 4/e exercise 1.19 14
C++ Primer ex 3.14 8
C++ Primer ex 5.18 5
C++ Primer ex 4.16 2
C++ Primer ex 7.5 18
C++ Primer ex 9.27 4
C++ Primer ex 4.30 10

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top