在 C++ 中删除指针

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

问题描述

限时送ChatGPT账号..

语境:我正在努力思考指针,几周前我们刚刚在学校看到它们,今天练习时我遇到了一个傻瓜?问题,这对您来说可能非常简单,但我几乎没有编程经验.

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.

我在 SO 中看到了很多关于删除指针的问题,但它们似乎都与删除类有关,而不是简单"指针(或任何适当的术语),这是我的代码我想跑:

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.

}

所以我的问题是:

  1. 为什么第一个案例不起作用?使用和删除指针似乎是最直接的用法?该错误表示内存未分配,但cout"返回了一个地址.
  2. 在第二个示例中,错误不是被触发,而是对 myPointer 的值进行计算 仍然 返回内存地址?
  3. #3 真的有效吗?似乎对我有用,指针不再存储地址,这是删除指针的正确方法吗?

抱歉问了这么长的问题,想尽可能清楚地说明这一点,还要重申一下,我的编程经验很少,所以如果有人能用外行的术语回答这个问题,将不胜感激!

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.

第一个变量被分配到栈上.您只能使用 new 运算符对动态分配的内存(在堆上)调用 delete.

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;

以上内容什么都没做.你没有释放任何东西,因为指针指向 NULL.

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

不应该做以下事情:

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

您将其指向 NULL,留下泄漏的内存(您分配的新 int).你应该释放你指向的内存.无法再访问分配的 new int,因此内存泄漏.

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.

正确的做法:

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

<小时>

更好的方法:

如果您使用 C++,不要使用原始指针.改用智能指针,它可以以很少的开销为您处理这些事情.C++11 带有几个.

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++ 中删除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:C++ 是否可以确定指针是否指向有效对象? 下一篇:指向引用的指针和指向指针的引用之间的区别

相关文章

最新文章