what will atoi(m_mapVideoConfig["root.Image.NbrOfConfigs"].c_str()) do if key does not exist?

What the following C++ function, which will be used with 64 bit Microsoft Visual Studio 2008 and Ubuntu Linux, return?

     uint32_t Camera::GetChannelCount()
    {
         return  atoi(m_mapVideoConfig["root.Image.NbrOfConfigs"].c_str());

    }
   
   where m_mapVideoConfig is std:map<std::string,std::string>

   * if the key "root.Image.NbrOfConfigs" does not exist

  will it throw an exception or crash?

   Thank you.
cameron423Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
will it throw an exception or crash?
no. atoi will stop conversion from left to right when the first non-digit was found (or sign is not at first position). so it returns 0 when the string is empty or is not a number at all.

Sara
0
sarabandeCommented:
that even would apply if the string contains a number that is beyond 31-bit maximum or 32-bit minimum for a signed 32-bit integer, since all compilers i know would not throw an exception at overflow but cut the bits instead.

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
>> if the key "root.Image.NbrOfConfigs" does not exist will it throw an exception or crash?
The key will be created and the value string will be empty. If this isn't want you want you should use the find method rather than the index operator.
0
sarabandeCommented:
fyi: the better alternative to atoi is strtol or std::istringstring::operator>>.

strtol has 3 arguments:
- const char * str // zero-terminated string
- char ** sentinel // that returns a pointer to str to indicate where the conversion stops
- int base              // integer base for example 10 if the number is decimal

std::string s = "12345abc";
char * ps = NULL;
int number = strtol(s.c_str(), &ps, 10);
if (ps == NULL || (*ps) != '\0')
{
       // error: the conversion stopped somewhere in the middle of the string

Open in new window


the c++ way though is to use stringstream:

#include <sstream>

std::string s = "12345abc";
std::istringstream is(s);  // feed input stream with string number
if (!(is>>number)) 
{
       // error: the conversion stopped somewhere in the middle of the string
       ...

Open in new window


Sara
0
evilrixSenior Software Engineer (Avast)Commented:
Wouldn't it be better to just use the correct map?

std:map<std::string, long>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.