accessing an empty string

A

arnuld

#include <iostream>
#include <limits>


int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}


[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%


this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

is it a valid C++ programme ?
 
R

Robert Bauck Hamar

arnuld said:
#include <iostream>
#include <limits>


int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}


[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%


this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

Because it's undefined behaviour. If you don't _know_ that your index is
inside the bounds, use at:

s1.at(10);

And: You are trying to access the eleventh character.
is it a valid C++ programme ?

No: You haven't included <string> and <ostream> (technically, you need
<ostream>, but it seems that most, if not all, implementations of
<iostream> includes <ostream>), but if you do that, it's still undefined
behaviour.
 
G

Guest

#include <iostream>
#include <limits>


int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}


[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%


this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

Fist of it's the 11th character you are trying to access. The []
operator on standard containers (string can be considered as such) is
not checked*, the at() method provides the same service except it will
throw an exception if the index is out of range so you might want to use
that instead.

* Notice that it behaves a bit different on std::map, where a new
element will be created instead.
 
J

John Harrison

arnuld said:
#include <iostream>
#include <limits>


int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}


[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%


this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

is it a valid C++ programme ?

No it's not a valid C++ program. Sometimes invalid C++ programs do not
produce errors.

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

Similar Threads

why "++a" is undefined behaviour ? 15
C++ Primer ex 4.16 2
C++ Primer section 1.6 example 1
C++ Primer ex 3.14 8
C++ Primer ex 7.5 18
C++ Primer ex 3.24 (bitset class) 5
C++ Primer exercise 3.13 17
playing with vectors 5

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top