检查是否有什么东西在malloced [英] Checking if something was malloced

查看:134
本文介绍了检查是否有什么东西在malloced的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一个指向某个变量..有一种方法来检查它是否是静态还是动态分配?

Given a pointer to some variable.. is there a way to check whether it was statically or dynamically allocated??

推荐答案

从您的评论引述:

即时作出,将基本摆脱结构的方法。它具有数据成员这是一个指向东西可能会或可能不会被malloced ..取决于哪一个,我想释放它

im making a method that will basically get rid of a struct. it has a data member which is a pointer to something that may or may not be malloced.. depending on which one, i would like to free it

正确的做法应该是的其他的成员添加到结构:一个指向一个释放函数

The correct way is to add another member to the struct: a pointer to a deallocation function.

这不只是静态与动态分配。还有的若干个的可能分配器,其中的malloc()的只是其中的一个。

It is not just static versus dynamic allocation. There are several possible allocators, of which malloc() is just one.

在类Unix系统中,它可能是:

On Unix-like systems, it could be:


  • 静态变量

  • 在堆栈

  • 在堆栈,但动态分配(即的alloca()

  • 在堆,用分配的malloc()

  • 在堆,用分配新的

  • 在堆,在分配的数组的中间新[]

  • 在堆,用分配)
  • 系统结构的malloc(内
  • 在堆,一个基类与分配的对象之内新的

  • MMAP
  • 分配
  • 使用自定义分配器分配

  • 更多的选择,包括上述
  • 的几种组合和变​​化
  • A static variable
  • On the stack
  • On the stack but dynamically allocated (i.e. alloca())
  • On the heap, allocated with malloc()
  • On the heap, allocated with new
  • On the heap, in the middle of an array allocated with new[]
  • On the heap, within a struct allocated with malloc()
  • On the heap, within a base class of an object allocated with new
  • Allocated with mmap
  • Allocated with a custom allocator
  • Many more options, including several combinations and variations of the above

在Windows上,也有一些运行时, LocalAlloc 的GlobalAlloc HeapAlloc (有几个堆,你可以轻松地创建),依此类推。

On Windows, you also have several runtimes, LocalAlloc, GlobalAlloc, HeapAlloc (with several heaps which you can create easily), and so on.

您必须始终为你所用分配器正确的释放函数释放内存。因此,负责分配内存的程序的任何部分也应释放内存,或者你必须通过正确的释放函数(或者它周围的包装器)到code,这将释放内存。

You must always release memory with the correct release function for the allocator you used. So, either the part of the program responsible for allocating the memory should also free the memory, or you must pass the correct release function (or a wrapper around it) to the code which will free the memory.

您也可以通过要求指针始终与特定的分配器或通过提供自己分配器(在一个函数的形式来分配存储器,并可能将其释放一个功能)分配避免整个问题。如果你自己提供的分配器,你甚至可以用技巧(如标记指针)允许一个也使用静态分配(但我不会进入这个方法在这里的详细说明)。

You can also avoid the whole issue by either requiring the pointer to always be allocated with a specific allocator or by providing the allocator yourself (in the form of a function to allocate the memory and possibly a function to release it). If you provide the allocator yourself, you can even use tricks (like tagged pointers) to allow one to also use static allocation (but I will not go into the details of this approach here).

雷蒙德陈有一个博客张贴关于它(Windows为中心的,但其概念是一样的无处不在):< A HREF =htt​​p://blogs.msdn.com/oldnewthing/archive/2006/09/15/755966.aspx>分配和释放跨模块的边界内存

Raymond Chen has a blog post about it (Windows-centric, but the concepts are the same everywhere): Allocating and freeing memory across module boundaries

这篇关于检查是否有什么东西在malloced的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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