我分配/释放哪些对象
在 Objective-C 中,简单地说 - 当假设
- 所有指针变量不再使用时必须释放它们时,我是否正确?
- 每个指针变量(*)都是某种类型的类,或者不是?
- 因此,每个指针变量都需要使用“alloc”和“init”(或类似的)进行分配和初始化?
- 当使用对象方法声明变量时,我可能不需要“alloc”或“init”?
- 数字声明(BOOL、int、float 等)只要不声明为指针就不需要内存管理?
感谢您提供任何帮助解决我的困惑的建议 伊弗洛
in Objective-C and simply speaking - am I correct when assuming
- that all pointer variables must be released when they're not used any more?
- Every pointer variable (*) is a class of some kind, or not?
- Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?
- When declaring variales using Object methods I may not need "alloc" or "init"?
- Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared as a pointer?
Thanks for any advice helping to sort out my confusion
iFloh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于你是否“拥有”这个尖锐的东西。我建议您仔细阅读这些内存管理 学习如何释放指针的规则。
不是每个指针。指针变量可以保存指向对象或内存块的指针。它只是一个指向某些东西的指针。
不是每个指针。指针变量可以保存指向对象或内存块的指针。指向的东西可以是一个已经存在的对象或一个分配的内存块。
不是每次都会。您可能会获得指向现有对象的指针,而不知道是谁分配和初始化它的。同样,有一些所有权规则需要遵循。我建议您仔细阅读这些内存管理 规则。
是的。有所谓的原始类型,只要将它们作为值进行操作,就不必处理它们的内存管理。
It depends if you "own" the pointed thing. I recommend you to carefully read these Memory Management rules to learn how to release a pointer.
Not every pointers. A pointer variable may hold a pointer to an object or to a block of memory. It is just a pointer to something.
Not every pointers. A pointer variable may hold a pointer to an object or to a block of memory. The pointed thing can be an already existing object or an allocated chunck of memory.
Not everytime. You may get a pointer to an existent object without knowing who allocated and initialized it. Again, there are some ownership rules to follow. I recommend you to carefully read these Memory Management rules.
Yes. There are called primitive types, and as long as you manipulate them as value, you don't have to deal with their memory management.
仅当“指针变量”指向 Objective-C 类的实例并且仅当您导致该实例被保留时之前。 阅读此内容:内存管理指南。
如果指针变量被声明为指向类实例的指针,那么它将指向一堂课。否则就不会了。指针正是如此;指向内存块的指针(引用)。该内存块可能是一个类、类的实例、C 结构或原始缓冲区(或其他内容)。
仅当指针指向 Objective-C 类的实例时。如果它是 C 结构,您可以使用
malloc()
。即使对于 Objective-C 类,您也可能不会分配
任何内容:NSString *foo = [NSString stringWithFormat: @"Hello, %@", @"World!"];
NSString *bar = @"bar";
NSBundle *main = [NSBundle mainBundle];
(顺便说一句:以上都不需要
-release
。)这个问题表明您应该阅读并重新阅读(我大约每 18 个月阅读一次)我编写 Objective-C 代码的第一个十年,每次都学到一些东西) -- Objective-C 语言指南。
您或许还应该阅读一些有关 C 语言本身的内容。这可能会帮助您理解指针与其他类型。
是的,但它比特定类型简单得多。当您说
int foo;
时,您是在告诉编译器在本地范围(通常是在堆栈上)中开辟一些空间来存储 int 的数据。当你说int *foo;
时,你是在告诉编译器在本地作用域中开辟出一点空间来存储一个指针,该指针指向内存中的一个位置,该位置包含一个 int。因此,
NSArray *bar
只是一个指向实例的指针,是的,NSArray bar;
,如果没有明确禁止的话,会在堆栈来保存 NSArray 实例。当您有一个指向某些东西的指针时,必须以某种方式初始化某些东西,并且通常是通过分配来完成的(但并非总是如此)。
Only if the "pointer variable" points to an instance of an Objective-C class and only if you caused that instance to be retained previously. Read this: Memory Management Guide.
If the pointer variable is declared as being a pointer to an instance of a class, then it will point to an instance of a class. Otherwise, it won't. Pointers are exactly that; a pointer -- a reference -- to a chunk of memory. That chunk of memory might be a class, an instance of a class, a C structure, or a raw buffer (or something else).
Only if the pointer points to an instance of an Objective-C class. If it is a C structure, you might use
malloc()
. Even in the case of an Objective-C class, you might notalloc
anything:NSString *foo = [NSString stringWithFormat: @"Hello, %@", @"World!"];
NSString *bar = @"bar";
NSBundle *main = [NSBundle mainBundle];
(BTW: None of the above need a
-release
.)This question indicates that you should go read -- and re-read (I read it about once every 18 months for the first decade I wrote Objective-C code, learning something each time) -- the Objective-C language guide.
You should probably also read something about the C language itself. That would likely help you to understand pointers vs. other types.
Yes, but it is much simpler than being type specific. When you say
int foo;
you are telling the compiler to carve out a bit of space in the local scope -- on the stack, generally -- to store an int's worth of data. When you sayint *foo;
, you are telling the compiler to carve out a bit of space in the local scope to store a pointer to -- to store the address of -- a location in memory that contains an int.Thus,
NSArray *bar
is just a pointer to an instance and, yes,NSArray bar;
, if it were not explicitly disallowed, would carve out a chunk of space on the stack to hold an NSArray instance.When you have a pointer to something that something must be initialized somehow and that is often done through allocation (but not always).
我相信你是对的:
是的。
我猜。我不确定我是否理解这个问题。
有点:正如您所说,如果您使用某些类方法,则不一定是
alloc
+init
。因此,分配和初始化是隐式完成的。正确。 (见上文)
我相信它不仅仅是数字,而且 C 基元不需要内存管理。
我建议您阅读 Apple 的内存管理规则 以及 斯坦福 iPhone 编程课程。
I believe you are correct:
Yes.
I guess. I'm not sure I understand the question.
Kind of: as you said, it's not necessarily
alloc
+init
, if you use certain Class methods. So, the allocation and initialization are done implicitly.Correct. (see above)
I believe that it's more than numbers and that C primitives do not require memory management.
I'd recommend that you read the memory management rules from Apple as well as the Stanford class on iPhone Programming.