Connecting Tech Pros Worldwide Help | Site Map

accessing an empty string

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 09:15 AM
arnuld
Guest
 
Posts: n/a
Default accessing an empty string

#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, 09:35 AM
Robert Bauck Hamar
Guest
 
Posts: n/a
Default 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, 11:35 AM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default 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, 06:55 PM
John Harrison
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.