accessing an empty string

Discussion in 'C++' started by arnuld, Jul 18, 2007.

  1. arnuld

    arnuld 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).

    is it a valid C++ programme ?


    --
    -- http://arnuld.blogspot.com
    arnuld, Jul 18, 2007
    #1
    1. Advertising

  2. 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
    Robert Bauck Hamar, Jul 18, 2007
    #2
    1. Advertising

  3. 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
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Jul 18, 2007
    #3
  4. 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
    John Harrison, Jul 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,024
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    803
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    573
    Dave Thompson
    Feb 14, 2004
  4. Tzury Bar Yochay
    Replies:
    1
    Views:
    404
    Gabriel Genellina
    Mar 24, 2008
  5. Savvoulidis Iordanis

    Empty gridview cells and checking for empty string

    Savvoulidis Iordanis, Sep 5, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    518
    Savvoulidis Iordanis
    Sep 5, 2008
Loading...

Share This Page