Connecting Tech Pros Worldwide Help | Site Map

accessing an empty string

  #1  
Old July 18th, 2007, 10:15 AM
arnuld
Guest
 
Posts: n/a
#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

  #2  
Old July 18th, 2007, 10:35 AM
Robert Bauck Hamar
Guest
 
Posts: n/a

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
  #3  
Old July 18th, 2007, 12:35 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a

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
  #4  
Old July 18th, 2007, 07:55 PM
John Harrison
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Empty pointer to std::string mr_sorcerer answers 10 April 5th, 2007 11:05 PM
Empty String when accessing DataGrid TextBox Tetet B via .NET 247 answers 0 November 22nd, 2005 10:54 AM
Empty String when accessing DataGrid TextBox Tetet B via .NET 247 answers 0 November 22nd, 2005 10:54 AM
Empty String when accessing DataGrid TextBox Tetet B via .NET 247 answers 0 July 21st, 2005 03:09 PM