Connecting Tech Pros Worldwide Help | Site Map

accessing an empty string

arnuld
Guest
 
Posts: n/a
#1: Jul 18 '07
#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 ?


--
-- http://arnuld.blogspot.com

Robert Bauck Hamar
Guest
 
Posts: n/a
#2: Jul 18 '07

re: accessing an empty string


arnuld wrote:
Quote:
#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.
Quote:
is it a valid C++ programme ?
No: You haven't included <stringand <ostream(technically, you need
<ostream>, but it seems that most, if not all, implementations of
<iostreamincludes <ostream>), but if you do that, it's still undefined
behaviour.

--
rbh
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Jul 18 '07

re: accessing an empty string


On 2007-07-18 11:15, arnuld wrote:
Quote:
#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.

--
Erik Wikström
John Harrison
Guest
 
Posts: n/a
#4: Jul 18 '07

re: accessing an empty string


arnuld wrote:
Quote:
#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
Closed Thread