带有指针向量的奇怪行为 [英] Strange behaviour with a vector of pointers

查看:58
本文介绍了带有指针向量的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行这段简单的代码时,我很难理解我得到的输出

I'm having a bit of trouble understanding the output that I get when I run this simple piece of code

#include <vector>
#include <iostream>
#include "LxUNIXsys.h"
using namespace std;

int main(int argc, char** argv) {

  //memory management class
  LxUNIXsys mem_manager;

  vector <double*> vec;

  mem_manager.DumpMemoryInfo();getchar();
  for(int i =0; i < 3; i++) vec.push_back(new double (3));
  for(int i =0; i < vec.size(); i++) cout << *vec[i] << endl;
  mem_manager.DumpMemoryInfo();getchar();
  for(int i =0; i < vec.size(); i++) delete vec[i];
  vec.clear();
  for(int i =0; i < 3; i++) cout << *vec[i] << endl;
  cout <<"size of vec " <<vec.size() << endl;
  mem_manager.DumpMemoryInfo();

  return 0;
}

LxUNIXsys是我用于内存转储的类,我从中调用的方法是:

The LxUNIXsys is a class that I'm using for the memory dump and the method I'm calling from it is:

void LxUNIXsys::DumpMemoryInfo() {

  pid_t id = getpid();
  //  cout << "id = " << id << endl;
  stringstream ss("");
  ss << "pmap -d " << id  << " | grep private ";
  //  cout << ss.str().c_str() << endl;
  cout << "Memory dump: [pid = " << id << " ] " << flush; system(ss.str().c_str());
}

我得到的输出是:

Memory dump: [pid = 17012 ] mapped: 51476K    writeable/private: 7452K    shared: 0K

3
3
3
Memory dump: [pid = 17012 ] mapped: 51480K    writeable/private: 7456K    shared: 0K

3
3
3
size of vec 0
Memory dump: [pid = 17012 ] mapped: 51480K    writeable/private: 7456K    shared: 0K

因此,基本上我删除了指针,不仅信息仍然存在,而且在clear()之后也可以访问它.

So, basically I deleted the pointer and not only the information is still there but I can access it after clear().

我也曾尝试使用delete[]删除vec,但结果是相同的.

I have also tried to delete vec using delete[] but the result is the same.

PS:我知道智能指针的存在,但是我想知道为什么这段代码没有达到预期的效果.

PS: I know about the existence of smart pointers, but I want to know why this code does not do what's expected.

PPS:我知道未定义的行为,清除后忘记了访问,我只想一种有效的方法立即释放内存,就像我删除常规指针一样.

PPS: I know about the undefined behavior, forget about the accessing after clear, I just want an effective way to release the memory immediately, like when I delete a regular pointer.

推荐答案

您具有未定义的行为.这是由于您访问std::vector中不存在的元素而引起的.未定义的行为意味着任何事情都可能发生.甚至看起来应该被销毁的物体仍然存在.

You have undefined behaviour. This is caused by you accessing elements of your std::vector that just don't exist. Undefined behaviour means that anything can happen. It can even appear that objects that should have been destroyed are still there.

C ++标准没有说明操作系统应如何管理内存的分配和释放.它只是给出有关程序可以依赖的规则.没有理由在操作系统实际上没有比您要求的要早的时候回收为这三个double分配的内存.您就是不能再依赖它了.您唯一可以依靠的就是您不应该在std::vector的范围之外访问.

The C++ standard says nothing about how the operating system should manage the allocation and deallocation of memory. It simply gives rules about what your program can rely on. There's no reason that the memory allocated for those three doubles couldn't actually be reclaimed by the OS a little later than you asked for it to be. You just can't rely on it any more. The only thing you can rely on is that you should not be accessing outside the bounds of your std::vector.

这正是标准将其指定为未定义行为的原因.它为操作系统或环境提供了执行其选择的内存管理的余地,尽管它选择了它,但仍为您的程序提供了规则,以依赖于明确定义的行为.

This is precisely why this is specified by the standard as undefined behaviour. It gives the operating system or environment the leeway to perform its memory management however it chooses, while still giving your program that rules to rely on for well-defined behaviour.

这篇关于带有指针向量的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆