exception in string

This is a discussion on exception in string within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am reviewing the exception safety of some code and met with some issues about exception safety in ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    exception in string

    Hello everyone,


    I am reviewing the exception safety of some code and met with some issues about exception safety in string, which I can not find the answer at hand. I think the 3 code segments may all throw exceptions. Because the storage of string internal character data is on heap (using allocator for char?), so when there is low memory, there will be bad_alloc exception?

    1. Initialization

    Code:
    string str1 = "Hello"; // or string str ("Hello");
    2. Assignment

    Code:
    string str2 = str1; // str1 is another string object or reference.
    3. Empty construction

    Code:
    string str;
    I am not quite sure about (3).


    thanks in advance,
    George

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I think it depends on implementation, Some can decide to allocate storage even in case of default constructor for internal use. Most I thinkwill not bother...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    All of string's constructors may throw.
    The base rule is that all functions may throw unless the standard explicitly says otherwise. From 17.3.4.8/3:
    Any of the functions defined in the C++ Standard library that do not have an exception-specification may throw implementation-defined exceptions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    So your answer to my question is all the scenarios may throw?

    Quote Originally Posted by vart View Post
    I think it depends on implementation, Some can decide to allocate storage even in case of default constructor for internal use. Most I thinkwill not bother...

    Thanks CornedBee,


    So, you answer to my 3 scenarios is, all of them may throw?

    Quote Originally Posted by CornedBee View Post
    All of string's constructors may throw.
    The base rule is that all functions may throw unless the standard explicitly says otherwise. From 17.3.4.8/3:

    regards,
    George

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no guarantee that #3 example DOESN'T allocate memory (take for example an implementation of string that maintains a valid C string at all times - it would require an empty string to have a single byte of the value zero to indicate an empty string).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Provided it doesn't use small buffer optimization.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and CornedBee,


    Let me conclude, I think it is safe to assume all of the 3 code segments will (may) throw exception. Please comment if you do not agree. :-)

    Quote Originally Posted by CornedBee View Post
    Provided it doesn't use small buffer optimization.

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Provided it doesn't use small buffer optimization.
    Of course - my point was more that UNLESS YOU KNOW the implementation, you can't know if the string is allocated or not. Small buffer optimization is an implementation that doesn't allocate storage for an empty string. Another option for the empty string is to not have a string stored at all - simply by the fact that the length is zero.

    My example, by the way, is not necessarily how it is implemented in ANY version of std::string - but since there is no clear statement of how it should be implemented, we can't determine what it does in the empty constructor example, or in any other constructor for that matter, and whether that will lead to an exception or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    So, I think you agree with my post #7. :-)

    Quote Originally Posted by matsp View Post
    Of course - my point was more that UNLESS YOU KNOW the implementation, you can't know if the string is allocated or not. Small buffer optimization is an implementation that doesn't allocate storage for an empty string. Another option for the empty string is to not have a string stored at all - simply by the fact that the length is zero.

    My example, by the way, is not necessarily how it is implemented in ANY version of std::string - but since there is no clear statement of how it should be implemented, we can't determine what it does in the empty constructor example, or in any other constructor for that matter, and whether that will lead to an exception or not.

    --
    Mats

    regards,
    George

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I do - there is no implicit guarantee that ANY string constructor are "exception safe".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    My question is answered.

    Quote Originally Posted by matsp View Post
    Yes, I do - there is no implicit guarantee that ANY string constructor are "exception safe".

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. string operation and related exception
    By George2 in forum C++ Programming
    Replies: 63
    Last Post: 03-04-2008, 01:54 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21