Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > accessing an empty string

Reply
Thread Tools

accessing an empty string

 
 
arnuld
Guest
Posts: n/a
 
      07-18-2007
#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

 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-18-2007
arnuld wrote:

> #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.

--
rbh
 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      07-18-2007
On 2007-07-18 11:15, arnuld wrote:
> #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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-18-2007
arnuld wrote:
> #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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Empty gridview cells and checking for empty string Savvoulidis Iordanis ASP .Net 1 09-05-2008 06:15 AM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57