简体   繁体   English

C ++ STL容器中的NULL指针

[英]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. 最近,我注意到操作符上出现了Visual Studio致命错误-unordered_set,这是通过简单地插入指向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). 在检查了本地人之后,我注意到set仅包含2个元素,最后一个为NULL(所以我想这就是它崩溃的原因)。 Now to the question: How (theoretically) can an unordered_set (or any other STL container) get a NULL pointer as one of its elements. 现在开始思考一个问题:unordered_set(或任何其他STL容器)如何(理论上)如何获得NULL指针作为其元素之一。 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) 为感兴趣的人员提供调用堆栈和部分源代码: http ://privatepaste.com/c8e7f35a4e(PushToProcessed是从Object本身调用的,它传递对自身的引用,因此不能为NULL)

Sets can contain pointers which can be NULL. 集可以包含可以为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. 容器可以轻松包含NULL值。 Are you talking about the internals of a container getting messed up? 您是在谈论容器内部混乱吗?

a NULL is a perfectly valid pointer value. NULL是一个完全有效的指针值。 If a NULL is passed in, it will be treated just like any memory address. 如果传入NULL,则将其与任何内存地址一样对待。 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. 由于您仅看到2个条目,因此我猜测您真正看到的是将许多NULL指针“添加”到您的集合中,但是由于它是一个集合,因此仅保留一个副本。

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. 为了检验我的假设,即许多项目都添加了所有NULL值,为什么不对cout进行简单调用,说明“指针添加了新项目,其中address =”。 Bet you'll be surprised. 打赌,您会感到惊讶。

I believe a container pointer value can be set via a dereferenced non-const iterator as in (*iter) = NULL. 我相信可以通过取消引用的非常量迭代器来设置容器指针值,如(* iter)= NULL。 This of course would be bad . 这当然是不好的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM