ios - 大家中午好,xcode 中如何查看一个对象是在堆区还是在栈区?
迷茫
迷茫 2017-04-18 09:47:36
0
3
907

大家中午好,xcode 中如何查看一个对象是在堆区还是在栈区?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
黄舟

It should be impossible to view.
Your initialization methods starting with new, alloc, retain, and copy are all in the heap area, including objects managed by reference counting.
Constants will be in the stack area.

Make it simple, remove NSString *aString = @"aaa"编译时会转换为常量

Other Objective-C objects, theoretically, are in the heap area.

In addition, block is also an exception. For specific knowledge points, you can learn about memory management.

巴扎黑

How to check whether an object is in the heap area or stack area
================================
The question you asked is rather vague. If it is during method execution Among them, the native type (or value type) defined locally. Then it must be on the stack. Destroyed directly when the function execution ends. Other reference types (or interfaces in oc) are created on the heap and ARC is responsible for cleaning them.

I guess what you want to ask is class Foo {

let a = 1

}
At this time, is the a field on the stack or the heap? If this is the problem, then tell you that a is on the heap, but it is a value type.

The value type has nothing to do with allocating memory on the stack or the heap, it is only related to copying by value (implicitly immutable)

Peter_Zhu

Under normal circumstances, you can print out the address to determine whether it is in the heap or stack memory. The stack is from the higher address downwards, and the heap is from the lower address upwards. Generally, if you see 0x7fff... this kind of address must be on the stack. , one thing to note is the TEXT segment. NSString is basically placed in the TEXT segment. This address is lower than the heap address and is easy to distinguish; you can take a look at how the memory is divided;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template