我应该删除来自其他函数或类方法的指针吗? [英] Should I delete pointers that come from other functions or class methods?

查看:115
本文介绍了我应该删除来自其他函数或类方法的指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java的背景,但是我仍然不完全了解指针和范围的概念,如果这个问题看起来很傻,那么请抱歉.

I have a background in Java and I'm still not fully used to the concept of pointers and scope, so sorry if the question seems silly.

我读到某处应该删除在堆上分配的指针.我了解这一点,但我也应该删除这样给我的指针:

I read somewhere that I should delete pointers that I have allocated on the Heap. I understand that but should I also delete a pointer that is given to me like this:

#include<dirent.h>

DIR* dir;
struct dirent* entries;

dir= opendir("D:/DIR")
entries= readdir(entries)

// Should I delete the pointers after I'm done with them?
delete entries;
delete dir;

我应该删除从其他位置分配的指针还是只是超出范围会自动删除它们?

Should I delete the pointers which are assigned from somewhere else or just going out of scope deletes them automatically?

或者因为我没有使用new分配它们而删除它们是否正确?但是,如果那是错误的,我该如何确保在我使用完其他方法后会删除从其他方法分配的内存?

Or is it even right to delete them since I haven't assigned them using new? But then if it's wrong how can I make sure that the memory assigned from the other methods would be deleted after I'm finished using them?

推荐答案

C与C ++不同,特别是在这方面.

C is not the same as C++, notably for this aspect.

在使用某些外部库提供的某些外部C(或C ++)功能时,您应阅读其文档,并遵循所有权"规则和 惯例 .例如,如果您使用 getline ,则您了解您需要free,例如此处.如果您使用 opendir ,则应使用 closedir .如果您使用 sqlite3_prepare_v2 ,则需要此处)和类似析构函数的函数.

When using some external C (or C++) function provided by some external library, you should read its documentation and follow the "ownership" rules and conventions. For example if you use getline you understand that you need to free like here. If you use opendir you should use closedir. If you use sqlite3_prepare_v2 you'll need to sqlite3_finalize it, etc... Sometimes you'll think in terms of some abstract data type (like here) with a destructor-like function.

在C语言中开发您自己的(公共或私有")函数时,如果它返回堆分配的内存以及谁(以及如何),则需要 document 负责free(或释放).

When you develop your own (public or "private") function in C, you need to document if it returns heap allocated memory and who (and how) is responsible of free (or releasing) it.

使用C ++,您还具有智能指针不是灵丹妙药.

With C++, you also have smart pointers and RAII; so you generally can avoid manual new and delete. Smart pointers are helpful, but not a silver bullet.

因此,您应明确文档约定(并遵循外部库和API的约定)有关所有权的规定.理解和明智地定义此类约定是一项重要任务.

So you should explicit and document conventions (and follow the conventions of external libraries and APIs) about ownership. Understanding and defining wisely such conventions is an important task.

圆形引用难以处理(请考虑 GC手​​册 ),至少能够恰当地命名您的方法,并了解引用计数的局限性和强大功能.在某些情况下,您甚至可以显式使用垃圾收集器库或编写自己的分配器.

Circular references are difficult to handle (consider weak pointers when applicable). I recommend reading about garbage collection concepts and techniques (e.g. the GC handbook), to at least be able to name appropriately your approaches and understand the limitations and power of reference counting. In some cases, you might even explicitly use a garbage collector library or code your own allocator.

手动内存管理是整个程序的属性,这就是为什么很难做到这一点.甚至在某些情况下(长寿命的过程需要进行大量分配),您甚至需要担心碎片化.

Manual memory management is a whole program property, and that is why it is hard. There are even cases (long-living processes doing a lot of allocation) where you need to be afraid of fragmentation.

valgrind 之类的工具和 GCC 仪器选项很有帮助.内存泄漏和其他一些内存错误.另请注意 ASLR .请务必了解您虚拟地址空间 .wikipedia.org/wiki/Process_(计算)"rel =" nofollow noreferrer>过程.在Linux上,阅读 proc(5),然后尝试cat /proc/self/maps在某些终端中提供有用的见解.

Tools like valgrind and the address sanitizer (with GCC's instrumentation options or Clang's ones) are practically very helpful to hunt memory leaks and some other memory bugs. Be also aware of ASLR. Take care to understand the virtual address space of your process. On Linux, read proc(5) then try cat /proc/$$/maps and cat /proc/self/maps in some terminal for a useful insight.

这篇关于我应该删除来自其他函数或类方法的指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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