本文介绍了在C ++中删除指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我试图把我的头围绕指针,我们只是在几个星期前在学校看到他们,而今天练习我遇到了一个愚蠢?问题,它可以是超级直接给你,但我有一点到没有编程经验。



我已经看到了很多关于删除指针的问题,但他们都似乎与删除一个类而不是一个简单的指针(或任何适当的术语可能)相关,这里是我试图运行的代码:

  #include< iostream> ;; 

using namespace std;

int main(){
int myVar,
* myPointer;

myVar = 8;
myPointer =& myVar;

cout<< 删除指针<< endl;
cout<< 存储器地址:< myPointer<< endl;

//看起来我不能*只是*删除它,因为它触发一个错误
delete myPointer;
cout<< myPointer:< myPointer<< endl;
//错误:a.out(14399)malloc:***错误对象0x7fff61e537f4:
//被释放的指针未分配
// ***在malloc_error_break中设置断点调试
//中止陷阱:6

//使用新的关键字befor删除它工作,但
//它真的释放了空间吗?
myPointer = new int;
delete myPointer;
cout<< myPointer:< myPointer<< endl;
// myPointer继续存储一个内存地址。

//在删除它之前使用NULL,似乎工作。
myPointer = NULL;
delete myPointer;
cout<< myPointer:< myPointer<< endl;
// myPointer返回0.

}

问题是:


  1. 为什么第一种情况不起作用?看来最直接的用法是使用和删除指针?

  2. 在第二个示例中,错误不是被触发,而是执行myPointer的值cout的返回内存地址?

  3. #3真的有效吗?似乎在为我工作,指针不再存储地址,这是正确的方式来删除指针吗?

很长的问题,想让这个尽可能清楚,也重申,我有很少的编程经验,所以如果有人可以用外行人的术语来回答这个问题,将非常感谢!

解决方案

1& 2

  myVar = 8; //不动态分配。无法调用删除它。 
myPointer = new int; //动态分配,可以调用删除就可以了。

第一个变量在堆栈上分配。你可以使用 new 运算符在动态分配的内存上调用delete。



3。

  myPointer = NULL; 
delete myPointer;

上面什么也没有。你没有释放任何东西,因为指针指向NULL。






不应执行以下操作:

  myPointer = new int; 
myPointer = NULL; // leaked memory,no pointer to above int
delete myPointer; //没有点

你指向NULL,留下泄露的内存分配)。
你应该释放你指向的内存。没有办法访问分配的 new int ,因此内存泄漏。






正确的方式:

  myPointer = new int; 
delete myPointer; //释放内存
myPointer = NULL; // pointer dangling ptr to NULL






更好的方法:



如果您使用的是C ++,不要使用原始指针。使用,它可以在很少的开销下为您处理这些事情。 C ++ 11随附。


Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience.

I've seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a 'simple' pointer (or whatever the proper term might be), here's the code I'm trying to run:

#include <iostream>;

using namespace std;

int main() {
  int myVar,
      *myPointer;

  myVar = 8;
  myPointer = &myVar;

  cout << "delete-ing pointers " << endl;
  cout << "Memory address: " << myPointer << endl;

  // Seems I can't *just* delete it, as it triggers an error
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4:
  // pointer being freed was not allocated
  // *** set a breakpoint in malloc_error_break to debug
  // Abort trap: 6

  // Using the new keyword befor deleting it works, but
  // does it really frees up the space?
  myPointer = new int;
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // myPointer continues to store a memory address.

  // Using NULL before deleting it, seems to work.
  myPointer = NULL;
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // myPointer returns 0.

}

So my questions are:

  1. Why won't the first case work? Seems the most straightforward use to use and delete a pointer? The error says the memory wasn't allocated but 'cout' returned an address.
  2. On the second example the error is not being triggered but doing a cout of the value of myPointer still returns a memory address?
  3. Does #3 really work? Seems to work to me, the pointer is no longer storing an address, is this the proper way to delete a pointer?

Sorry for the long question, wanted to make this as clear as possible, also to reiterate, I have little programming experience, so if someone could answer this using layman's terms, it would be greatly appreciated!

解决方案

1 & 2

myVar = 8; //not dynamically allocated. Can't call delete on it.
myPointer = new int; //dynamically allocated, can call delete on it.

The first variable was allocated on the stack. You can call delete only on memory you allocated dynamically (on the heap) using the new operator.

3.

  myPointer = NULL;
  delete myPointer;

The above did nothing at all. You didn't free anything, as the pointer pointed at NULL.


The following shouldn't be done:

myPointer = new int;
myPointer = NULL; //leaked memory, no pointer to above int
delete myPointer; //no point at all

You pointed it at NULL, leaving behind leaked memory (the new int you allocated).You should free the memory you were pointing at. There is no way to access that allocated new int anymore, hence memory leak.


The correct way:

myPointer = new int;
delete myPointer; //freed memory
myPointer = NULL; //pointed dangling ptr to NULL


The better way:

If you're using C++, do not use raw pointers. Use smart pointers instead which can handle these things for you with little overhead. C++11 comes with several.

这篇关于在C ++中删除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:06