What does Visual Studio do with a deleted pointer and why?

A C++ book I have been reading states that when a pointer is deleted using the delete operator the memory at the location it is pointing to is "freed" and it can be overwritten. It also states that the pointer will continue to point to the same location until it is reassigned or set to NULL . In Visual Studio 2012 however; this doesn't seem to be the case! Example: #include &

Visual Studio用删除的指针做什么,为什么?

我一直在阅读的一本C ++书籍指出,当使用delete操作符删除一个指针时,它所指向的位置上的内存被“释放”并且可以被覆盖。 它还指出指针将继续指向相同的位置,直到它被重新分配或设置为NULL 。 然而,在Visual Studio 2012中, 这似乎并非如此! 例: #include <iostream> using namespace std; int main() { int* ptr = new int; cout << "ptr = " << ptr << endl; delete ptr;

Which location address in a pointer refers to

What does the address in a pointer refer to, real address location in main memory or virtual address. Can it be configured. And if it refers to virtual address , does Memory manager needs to convert this address to real address everytime it is accessed This depends on your system and OS. For a typical windows/linux user space application, the address is a virtual memory address. User spac

指针中的哪个位置地址指的是

指针中的地址是指什么,主存储器中的实际地址位置还是虚拟地址。 可以配置吗? 如果它指向虚拟地址,内存管理器每次访问时都需要将此地址转换为实地址 这取决于您的系统和操作系统。 对于典型的windows / linux用户空间应用程序,地址是虚拟内存地址。 用户空间应用程序无法使用物理地址访问内存 - 这是操作系统为每个进程提供的抽象之一。 MMU(内存管理单元)为每个内存访问执行此翻译,并由操作系统为您的过程设置c

Why is address zero used for the null pointer?

In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't dangerous; when I call malloc it returns a pointer with the value zero if it can't get me memory; I use if (p != 0) all the time to make sure passed pointers are valid, etc. But since memory a

为什么地址零用于空指针?

在C(或C ++)中,如果指针的值为零,指针是特殊的:我建议在释放内存后将指针设置为零,因为这意味着再次释放指针并不危险。 当我调用malloc时,如果它无法让我记忆,它会返回一个零值的指针; 我一直使用if (p != 0)来确保传入的指针是有效的,等等。 但是由于存储器寻址从0开始,不是0就像其他地址一样是有效地址吗? 如果情况如此,如何使用0来处理空指针? 为什么不是负数而是空数字呢? 编辑: 一堆很好的答案。

Pointer to vector vs vector of pointers vs pointer to vector of pointers

Just wondering what you think is the best practice regarding vectors in C++. If I have a class containing a vector member variable. When should this vector be declared a: "Whole-object" vector member varaiable containing values, ie vector<MyClass> my_vector; Pointer to a vector, ie vector<MyClass>* my_vector; Vector of pointers, ie vector<MyClass*> my_vector;

指向矢量vs指针向量的指针vs指向矢量的指针

只是想知道你认为什么是关于C ++中向量的最佳实践。 如果我有一个包含矢量成员变量的类。 什么时候该向量被声明为: 包含值的“全对象”矢量成员变量,即vector<MyClass> my_vector; 指向矢量的指针,即vector<MyClass>* my_vector; 指针矢量,即vector<MyClass*> my_vector; 指向矢量指针的指针,即vector<MyClass*>* my_vector; 我在我的一个类中有一个特定的例子,当前我已经将一个向量声

Appending a vector to a vector

This question already has an answer here: Concatenating two std::vectors 16 answers a.insert(a.end(), b.begin(), b.end()); or a.insert(std::end(a), std::begin(b), std::end(b)); The second variant is a more generically applicable solution, as b could also be an array. However, it requires C++11 std::copy (b.begin(), b.end(), std::back_inserter(a)); This can be used in case the items in ve

将矢量附加到矢量

这个问题在这里已经有了答案: 连接两个std :: vectors 16个答案 a.insert(a.end(), b.begin(), b.end()); 要么 a.insert(std::end(a), std::begin(b), std::end(b)); 第二个变体是一个更通用的解决方案,因为b也可以是一个数组。 但是,它需要C ++ 11 std::copy (b.begin(), b.end(), std::back_inserter(a)); 这可以用于向量a中的项没有赋值运算符(例如const成员)的情况。 在所有其他情况下,与上述插入解决方案

What are the barriers to understanding pointers and what can be done to overcome them?

Why are pointers such a leading factor of confusion for many new, and even old, college level students in C or C++? Are there any tools or thought processes that helped you understand how pointers work at the variable, function, and beyond level? What are some good practice things that can be done to bring somebody to the level of, "Ah-hah, I got it," without getting them bogged down

理解指针有什么障碍,克服它们有什么可以做的?

为什么C或C ++中许多新的,甚至是老年的大学水平的学生都指出这种混乱的主要原因? 是否有任何工具或思维过程帮助您理解指针在变量,函数和更高级别上的工作方式? 有什么好的做法可以使人们达到“啊,我明白了”的水平,而不会让他们陷入整体概念? 基本上,钻类似的情况。 指针是一个概念,对于许多人来说,最初可能会引起混淆,特别是当涉及到复制指针值并仍然引用相同的内存块时。 我发现最好的比喻是将指针视为一张纸

Why scoped pointers in boost

What is the objective of scoped pointer? to my understanding, the scoped pointer manages the memory within a block of code. If i want to declare a variable within a block , i can just declare it on a stack and not worry about cleaning. Not if it's of dynamic size or type. In addition, scoped pointers can be swapped, and in C++11 unique_ptr can be moved, so they're not strictly scoped

为什么在boost中使用范围指针

范围指针的目标是什么? 据我了解,范围指针管理一块代码中的内存。 如果我想在一个块内声明一个变量,我可以在堆栈中声明它,而不用担心清理。 不是如果它是动态大小或类型。 另外,范围指针可以被交换,并且在C ++ 11中unique_ptr可以被移动,所以它们不被严格限定范围。 与基于堆栈的数据不同,scoped_ptr有一个reset()成员 - 换句话说,您可以构建/破坏您心中的内容。 有了这个,你可以使用一个空指针(技术上的op

Which is better: storing objects vs storing pointers?

This question already has an answer here: Why should I use a pointer rather than the object itself? 22 answers It depends on how you're using your store. Storing objects means you will copy them when calling Add , and also when copying the store (among other circumstances): that could have a cost, and can lead to unwanted behaviours. Pointers may be a good alternative, but you should

哪个更好:存储对象vs存储指针?

这个问题在这里已经有了答案: 为什么我应该使用指针而不是对象本身? 22个答案 这取决于你如何使用你的商店。 存储对象意味着在调用Add时以及复制存储时(以及其他情况),您将复制这些对象:这可能会带来成本,并且可能导致不需要的行为。 指针可能是一个很好的选择,但是你应该更喜欢托管指针,比如std::unique_ptr ,所以你不必处理删除操作。 版本C: auto e = std::unique_ptr<E>(new E()); store.Add(e);

What is the benefit of using a pointer C++

This question already has an answer here: Why should I use a pointer rather than the object itself? 22 answers There is no "simulation" happening at all. book1 has an address too, and the this pointer is set to the address of book1 when you do book1.setBooksId(123); . There is no difference.

使用指针C ++有什么好处

这个问题在这里已经有了答案: 为什么我应该使用指针而不是对象本身? 22个答案 根本没有“模拟”发生。 book1有一个地址也与this指针设置为地址book1 ,当你做book1.setBooksId(123); 。 没有区别。

Static and dynamic memory allocation in C++

This question already has an answer here: Why should I use a pointer rather than the object itself? 22 answers The left side is an int , the right side is creating an int * (pointer) You want: int * ptr = new int; Or if you want to do static allocation int num; // = 5; Will initialize num to 5 Your claim is correct. left part (ptr) is of static memory allocation. right part (new in

C ++中的静态和动态内存分配

这个问题在这里已经有了答案: 为什么我应该使用指针而不是对象本身? 22个答案 左边是一个int ,右边是创建一个int * (指针) 你要: int * ptr = new int; 或者如果你想做静态分配 int num; // = 5; Will initialize num to 5 你的声明是正确的。 左边部分(ptr)是静态内存分配。 右边部分(new int)是动态内存分配的。 换句话说,右边的部分返回一个int * ,一个(动态分配的) 指向int指针 。 您不能将