Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:49:35+00:00 2026-06-04T05:49:35+00:00

My code did the following: Retrieve a value from a map with operator[] .

  • 0

My code did the following:

  1. Retrieve a value from a map with operator[].
  2. Check the return value and if NULL use insert to insert a new element in the map.

Magically, an element with value 0 appeared in the map.

After several hours of debugging I discovered the following: map‘s operator[] inserts a new element if the key is not found while insert does not change the value if the key exists.

Even if a default constructor for the map value type does not exist the code compiles and operator[] inserts 0.

Is there a way (e.g. some coding convention I could follow from now on) I could have prevented this from hurting me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:49:38+00:00Added an answer on June 4, 2026 at 5:49 am

    Is there a way (e.g. some coding convention I could follow from now on) I could have prevented this from hurting me?

    This may sound snarky, but: by reading the documentation.

    Since what you did is somewhat expected behaviour of the map, there’s not much you can do to guard against it.

    One thing you can heed in the future is the following. In your second step, you did something wrong:

    Check the return value and if NULL use insert to insert a new element in the map.

    This does never work with C++ standard library functions (other than C compatibility functions and new): the standard library doesn’t deal in pointers, least of all null pointers, so checking against NULL (or 0 or nullptr) rarely makes sense. (Apart from that, it wouldn’t make sense for a map’s operator [] to return a pointer in the first place. It obvoiusly returns the element type (or rather, a reference to it)).

    In fact, the standard library predominantly uses iterators so if at all, check for iterator validity by comparing against the end() of a container.

    Unfortunately, your code (checking against NULL) compiled since NULL is actually a macro that’s equal to 0 in C++ so you can compare it against an integer.

    C++11 gets safer by introducing the nullptr keyword which has a distinct type, so comparing it with an integer wouldn’t compile. So this is a useful coding convention: never use NULL, and instead compile with C++11 support enabled and use nullptr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code (for simplicity, I did not follow any C# coding rules).
I tried the following code. Although I don't get any errors, it did not
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
I'm not sure how to map the following tables below in EF 4.1 code
The following code fails throws a Zend_Controller_Exception (Invalid value passed to setPost(); must be
The UTF-8 of 龅 is E9BE85 and the unicode is U+9F85. Following code did
The following code will retrieve the body content of a url retrieved using CURL
I read from a book saying that the following c++ code should not compile:
I came across some matlab code that did the following: thing.x=linspace(... I know that
I did a behavioral simulation of my code, and it works perfectly. The results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.