Stranger
Stranger

Reputation: 1

C++ a NULL pointer in the STL container

I am, unfortunately, not working on a program developed by myself fully. I recently I noticed a Visual Studio Fatal Error on operator-- of unordered_set, which was called from simple insertion of a pointer to unordered_set. After reviewing the locals, I've noticed that set only has 2 elements last of which is NULL (so I suppose that's what it crashed on). Now to the question: How (theoretically) can an unordered_set (or any other STL container) get a NULL pointer as one of its elements. Program is multi-threaded, but according to my reviews, that part of code should only be accessed from one thread. Thanks.

Call stack and parts of source code for those who are interested: http://privatepaste.com/c8e7f35a4e (PushToProcessed is called from Object itself, it passes reference to itself, so cannot be NULL)

Upvotes: 0

Views: 3714

Answers (3)

ergosys
ergosys

Reputation: 49039

I believe a container pointer value can be set via a dereferenced non-const iterator as in (*iter) = NULL. This of course would be bad.

Upvotes: 1

wheaties
wheaties

Reputation: 35980

a NULL is a perfectly valid pointer value. If a NULL is passed in, it will be treated just like any memory address. Since you're seeing only 2 entries I would wager a guess that what you really are seeing is many NULL pointers being "added" to your set but since it is a set only one copy retained.

Edit:

To test my hypothesis of many items being added all of NULL value why not put in a simple call to cout stating "New item added with address = " the value of the pointer. Bet you'll be surprised.

Upvotes: 4

ezpz
ezpz

Reputation: 12047

Sets can contain pointers which can be NULL.

#include <iostream>
#include <vector>

int main () {
    std::vector<int *> foo;
    foo.push_back (NULL);
    std::cout << foo.size () << std::endl;
    return 0;
}

A container can easily contain a NULL value. Are you talking about the internals of a container getting messed up?

Upvotes: 6

Related Questions