可以 C++类判断它是在栈上还是在堆上?
我有
class Foo {
....
}
Is there a way for Foo to be able to split out:
function blah() {
Foo foo; // on the stack
}
并且
function blah() {
Foo foo* = new Foo(); // on the heap
}
我希望 Foo 能够根据它是在堆栈还是堆上分配来执行不同的操作。
编辑:
很多人问我“为什么这样做?”
答案:
我现在使用的是引用计数 GC。但是,我希望有能力运行 mark & 。扫也。为此,我需要标记一组“根”指针——这些是堆栈上的指针。因此,对于每个类,我想知道它们是在堆栈中还是在堆中。
I have
class Foo {
....
}
Is there a way for Foo to be able to separate out:
function blah() {
Foo foo; // on the stack
}
and
function blah() {
Foo foo* = new Foo(); // on the heap
}
I want Foo to be able to do different things depending on whether it's allocated on the Stack or the Heap.
Edit:
Alof of people have asked me "why do this?"
The answer:
I'm using a ref-counted GC right now. However, I want to have ability to run mark & sweep too. For this, I need to tag a set of "root" pointers -- these are the pointers on the stack. Thus, for each class, I'd like to know whether they're in the stack or in the heap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
一种巧妙的方法是:
如果对象是在堆栈上创建的,那么它必须位于外部函数堆栈变量方向的某个位置。堆通常从另一侧增长,因此堆栈和堆会在中间的某个地方相遇。
(肯定有一些系统是行不通的)
A hacky way to do it:
If the object was created on the stack it must live somewhere in the direction of the outer functions stack variables. The heap usually grows from the other side, so that stack and heap would meet somewhere in the middle.
(There are for sure systems where this wouldn't work)
您需要实际问我们真正的问题(a):-)您可能很清楚为什么您认为这是必要的,但它几乎可以肯定不是。事实上,这几乎总是一个坏主意。换句话说,您认为您为什么需要这样做?
我通常发现这是因为开发人员希望根据对象的分配位置来删除或不删除对象,但这通常应该留给代码的客户端而不是代码本身。
更新:
既然您已经澄清了问题的原因,我很抱歉,您可能已经发现了您所问的问题有意义的少数几个领域之一(运行您自己的垃圾收集进程) )。理想情况下,您应该重写所有内存分配和取消分配运算符,以跟踪堆中创建和删除的内容。
但是,我不确定拦截类的 new/delete 是否是一个简单的问题,因为可能存在未调用
delete
的情况,并且由于 mark/sweep 依赖于引用计数,因此您需要能够拦截指针分配才能正常工作。你有没有想过你要如何处理这件事?
经典示例:
不会导致删除调用。
另外,您将如何检测到指向某个实例的指针位于堆栈上这一事实? new 和 delete 的拦截可以让你存储对象本身是基于堆栈还是基于堆,但我不知道如何知道指针将被分配到哪里,特别是使用如下代码:
也许你可以想研究一下 C++ 的智能指针,它对于淘汰手动内存管理大有帮助。共享指针本身仍然可能会遇到循环依赖等问题,但明智地使用弱指针可以轻松解决这个问题。
您的场景中可能不再需要手动垃圾收集。
(a) 这称为
X/Y 问题
。很多时候,人们会提出一个预先假设一类解决方案的问题,而更好的方法只是描述问题,而不对最佳解决方案是什么有先入之见。You need to actually ask us the real question(a) :-) It may be apparent to you why you think this is necessary but it almost certainly isn't. In fact, it's almost always a bad idea. In other words, why do you think you need to do this?
I usually find it's because developers want to delete or not delete the object based on where it was allocated but that's something that should usually be left to the client of your code rather than your code itself.
Update:
Now that you've clarified your reasons in the question, I apologise, you've probably found one of the few areas in which what you're asking makes sense (running your own garbage collection processes). Ideally, you'd override all the memory allocation and de-allocation operators to keep track of what is created and removed from the heap.
However, I'm not sure it's a simple matter of intercepting the new/delete for the class since there could be situations where
delete
is not called and, since mark/sweep relies on a reference count, you need to be able to intercept pointer assignments for it to work correctly.Have you thought about how you're going to handle that?
The classic example:
will not result in a delete call.
Also, how will you detect the fact that the pointer to one of your instances is on the stack? The interception of new and delete can let you store whether the object itself is stack or heap-based but I'm at a loss as to how you tell where the pointer is going to be assigned to, especially with code like:
Perhaps you may want to look into C++'s smart pointers, which go a long way toward making manual memory management obsolete. Shared pointers on their own can still suffer from problems like circular dependencies but the judicious use of weak pointers can readily solve that.
It may be that manual garbage collection is no longer required in your scenario.
(a) This is known as the
X/Y problem
. Many times, people will ask a question that pre-supposes a class of solution whereas a better approach would be just to describe the problem with no preconceptions of what the best solution will be.答案是否定的,没有标准/便携式方法可以做到这一点。涉及重载新运算符的黑客行为往往存在漏洞。依赖于检查指针地址的黑客行为是特定于操作系统和堆实现的,并且可能会随着操作系统的未来版本而改变。您可能对此感到满意,但我不会围绕这种行为构建任何类型的系统。
我将开始寻找不同的方法来实现你的目标 - 也许你可以有一个完全不同的类型作为你的方案中的“根”,或者要求用户(正确地)使用特殊的构造函数来注释堆栈分配的类型。
The answer is no, there is no standard/portable way to do this. Hacks involving overloading the new operator tend to have holes. Hacks that depend on checking pointer addresses are OS specific and heap implementation specific, and may change with future versions of the OS. You may be comfortable with that, but I wouldn't build any sort of system around this behavior.
I would start looking at different ways to accomplish your goal - perhaps you can have a totally different type to serve as the "root" in your scheme, or require the users to (properly) annotate the stack allocated types as such with a special constructor.
如果将“this”的值与堆栈指针的当前值进行比较,这是可能的。如果这< sp 那么你已经在栈中分配了。
试试这个(在 x86-64 中使用 gcc):
它应该输出:
It is possible if you compare the value of 'this' with the current value of the stack pointer. If this < sp then you have been allocated in the stack.
Try this out (using gcc in x86-64):
It should output:
一种更直接且侵入性较小的方法是在内存区域映射(例如
/proc//maps
)中查找指针。每个线程都有一个分配给其堆栈的区域。静态和全局变量将位于 .bss 部分,常量位于 rodata 或 const 段中,等等。A more direct, and less intrusive method would be to look up the pointer in the memory region maps (such as
/proc/<pid>/maps
). Each thread has a region allocated to its stack. Static and global variables will live in the .bss section, constants in a rodata or const segment, and so on.我不确定你在问什么,但覆盖
new
运算符可能就是你想要做的。由于在 C++ 中在堆上创建对象的唯一安全方法是使用 new 运算符,因此您可以区分堆上存在的对象与其他形式的内存。谷歌“在c++中重载新的”以获取更多信息。但是,您应该考虑是否真的有必要在类内部区分这两种类型的内存。如果你不小心的话,让一个对象根据其存储位置而表现不同,听起来就像是灾难的根源!
I am not positive what you are asking, but overriding the
new
operator may be what you are trying to do. As the only safe way to create an object on the heap in C++ is to use thenew
operator, you can differentiate between objects that exist on the heap versus other forms of memory. Google "overloading new in c++" for more information.You should, however, consider if differentiating between the two types of memory is really necessary from inside the class. Having an object behave differently depending upon where it is stored sounds like a recipe for disaster if you are not careful!
如上所述,您需要通过重载的 new 运算符来控制对象的分配方式。但是要注意两件事,首先是“placement new”运算符,它在用户预先分配的内存缓冲区内初始化对象;其次,没有什么可以阻止用户简单地将任意内存缓冲区转换为对象类型:
另一种方法是,大多数运行时使用的内存比进行堆分配时要求的内存多一些。他们通常在那里放置一些服务结构,以通过指针识别正确的释放。您可以检查这些模式的运行时实现,尽管这会使您的代码确实难以移植、危险且无法支持的过度杀伤力。
同样,如上所述,当您应该询问您设计此解决方案的初始问题(“为什么”)时,您实际上是在询问解决方案的详细信息(“如何”)。
As mentioned above, you need to control how your object is allocated through overloaded new operator. Watch out for two things however, first the 'placement new' operator that initializes your object inside the memory buffer preallocated by user; second, nothing stops the user from simply casting arbitrary memory buffer into your object type:
Another way is the fact that most runtimes use a bit more memory than asked when doing heap allocations. They usually place some service structure there to identify proper deallocations by pointer. You could inspect your runtime implementation for these patterns, although it will make your code really unportable, dangerous and unsupportable overkill.
Again, as mentioned above, you really are asking for solution details ("how") when you should ask about the initial problem you devised this solution for ("why").
pax 提出的元问题是“你为什么要这样做”,你可能会得到更丰富的答案。
现在假设您出于“充分的理由”(也许只是好奇)这样做,可以通过重写运算符 new 和 delete 来获得此行为,但不要忘记重写所有 12 个变体,包括:
new、删除、新建不抛出、删除不抛出、新数组、删除数组、新数组不抛出、删除数组不抛出、放置新、放置删除、放置新数组、放置删除数组。
您可以做的一件事是将其放入基类中并从中派生。
这有点痛苦,那么您想要什么不同的行为呢?
The meta question as asked by pax is asked "why would you want to do that" you'll likely get a more informative answer.
Now assuming you're doing this for "a good reason" (perhaps just curiousity) can get this behaviour by overriding operators new and delete, but don't forget to override all 12 variants including:
new, delete, new no throw, delete no throw, new array, delete array, new array no throw, delete array no throw, placement new, placement delete, placement new array, placement delete array.
One thing you can do is put this in a base class and derive from it.
This is kind of a pain, so what different behavior did you want?
不,它无法可靠或明智地完成。
您可以通过重载
new
来检测何时为对象分配了new
。但是,如果该对象被构造为类成员,并且所属类在堆上分配怎么办?
这是添加到您已有的两个代码示例中的第三个代码示例:
静态/全局对象怎么样?您如何区分它们与堆栈/堆?
您可以查看对象的地址,并使用它来确定它是否在定义堆栈的范围内。但堆栈的大小可能会在运行时调整。
所以,实际上,最好的答案是“C++ 不使用标记和清除 GC 是有原因的”。
如果您想要一个合适的垃圾收集器,请使用另一种支持它的语言。
另一方面,大多数经验丰富的 C++ 程序员发现,当您学习了资源管理的必要技术时,对垃圾收集器的需求几乎消失了(RAII)。
Nope, it can't be done reliably or sensibly.
You may be able to detect when an object is allocated with
new
by overloadingnew
.But then what if the object is constructed as a class member, and the owning class is allocated on the heap?
Here's a third code example to add to the two you've got:
What about static/global objects? How would you tell them apart from stack/heap ones?
You could look at the address of the object, and use that to determine if it is within the range that defines the stack. But the stack may be resized at runtime.
So really, the best answer is that "there's a reason why mark & sweep GC's aren't used with C++".
If you want a proper garbage collector, use a different language, one which supports it.
On the other hand, most experienced C++ programmers find that the need for a garbage collector pretty much vanishes when you learn the necessary techniques for resource management (RAII).
MFC类的一种方式:
.H
.CPP
A way for MFC classes:
.H
.CPP
为你的类重载 new() 。这样您就可以区分堆和堆栈分配,但不能区分堆栈和静态/全局分配。
Overload new() for your class. This way you'll be able to tell between heap and stack allocation, but not between stack and static/global.
我建议改用智能指针。根据设计,类应该具有有关类的数据和信息。簿记任务应委托给课堂外。
重载new和delete可能会导致比你想象的更多的漏洞。
I would recommend using smart pointers instead. By design, the class should have data and information about class. Book-keeping tasks should be delegated outside the class.
overloading new and delete can lead to more holes than you can imagine.
要回答您的问题,有一种可靠的方法(假设您的应用程序没有使用多个线程),假设您的智能指针未包含的所有内容都不在堆上:
->重载 new,以便您可以存储分配的所有块的列表以及每个块的大小。
->当你的智能指针的构造函数时,在你的 this 指针所属的块中搜索。如果它不在任何块中,您可以说它“在堆栈上”(实际上,这意味着它不由您管理)。否则,您知道指针被分配的位置和时间(如果您不想寻找孤立指针和懒惰的释放内存,或类似的东西..)
它不依赖于架构。
To answer your question, a reliable way (assuming your aplication isn't using more thant one thread), assuming that everithing wich is not contained by your smart pointer isn't on the heap :
-> Overloading new, so that you ca store a list of all blocs allocated, with the size of each block.
-> When the constructor of your smart pointer, search in wich block your this pointer belong. If it isn't in any block, you can say it's "on the stack" (actualy, it means it's not managed by you). Otherwise, you know where and when your pointer was allocated (if you wan't to look for orphan pointers and lasily free memory, or things like that..)
It do not depend from the architechture.
有一个解决方案,但它强制继承。请参阅 Meyers,“更有效的 C++”,第 27 条。
编辑:
Ron van der Wal 撰写的总结了 Meyers 的建议 ,Meyers 本人在他的博客中链接到该链接(在这篇文章中):
原始文章还有更多内容需要阅读:Ron van der Wal 对此建议进行了评论,然后演示了其他替代堆跟踪方法。
There is a solution, but it forces inheritance. See Meyers, "More Effective C++", Item 27.
EDIT:
Meyers' suggestion is summarized in an article written by Ron van der Wal, which Meyers himself linked to in his blog (in this post):
There is more to read on the original article: Ron van der Wal comments on this suggestion, and then demonstrates other alternative heap tracking methods.
看看这里的程序:http://alumni.cs.ucr。 edu/~saha/stuff/memaddr.html。通过一些转换,它输出:
Take a look at the program here: http://alumni.cs.ucr.edu/~saha/stuff/memaddr.html. With a few casts, it ouputs:
有特定于平台的解决方案:
Windows:有查询堆栈范围的系统函数,请参阅问题
https://stackoverflow.com/q/3918375/9437799。利用这些堆栈变量可以被唯一地标识。
Posix: 同样,pthread_attr_getstack() 应该提供堆栈范围。
根据您的问题,不需要考虑数据段中的变量,否则您可能会迭代分配给进程的模块并查询它们的内存范围。
There are platform specific solutions:
Windows: There are system functions to query stack ranges, see question
https://stackoverflow.com/q/3918375/9437799 . With these stack variables may be uniquely identified.
Posix: Similiarily pthread_attr_getstack() should deliver stack ranges.
From your question variables in data segments do not need to be considered, else you might iterate over the modules allocated to the process and query their memory ranges.