我分配/释放哪些对象

发布于 2024-08-23 14:51:08 字数 293 浏览 9 评论 0原文

在 Objective-C 中,简单地说 - 当假设

  1. 所有指针变量不再使用时必须释放它们时,我是否正确?
  2. 每个指针变量(*)都是某种类型的类,或者不是?
  3. 因此,每个指针变量都需要使用“alloc”和“init”(或类似的)进行分配和初始化?
  4. 当使用对象方法声明变量时,我可能不需要“alloc”或“init”?
  5. 数字声明(BOOL、int、float 等)只要不声明为指针就不需要内存管理?

感谢您提供任何帮助解决我的困惑的建议 伊弗洛

in Objective-C and simply speaking - am I correct when assuming

  1. that all pointer variables must be released when they're not used any more?
  2. Every pointer variable (*) is a class of some kind, or not?
  3. Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?
  4. When declaring variales using Object methods I may not need "alloc" or "init"?
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲娇萝莉攻 2024-08-30 14:51:08

1) 所有指针变量必须是
当它们没有被使用时被释放
更多?

这取决于你是否“拥有”这个尖锐的东西。我建议您仔细阅读这些内存管理 学习如何释放指针的规则。

2) 每个指针变量 (*) 都是一个类
某种类型的,或者不是?

不是每个指针。指针变量可以保存指向对象或内存块的指针。它只是一个指向某些东西的指针。

3) 因此每个指针变量都需要
使用分配和初始化
“alloc”和“init”(或类似的)?

不是每个指针。指针变量可以保存指向对象或内存块的指针。指向的东西可以是一个已经存在的对象或一个分配的内存块。

4) 使用 Object 声明变量时
方法我可能不需要“分配”或
“初始化”?

不是每次都会。您可能会获得指向现有对象的指针,而不知道是谁分配和初始化它的。同样,有一些所有权规则需要遵循。我建议您仔细阅读这些内存管理 规则。

5) 数字声明(BOOL、int、float、
等)不需要内存管理
只要它们没有被声明为
指针?

是的。有所谓的原始类型,只要将它们作为值进行操作,就不必处理它们的内存管理。

1) that all pointer variables must be
released when they're not used any
more?

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.

2) Every pointer variable (*) is a class
of some kind, or not?

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.

3) Every pointer variable therefore needs
to be allocated and initialised using
"alloc" and "init" (or similar)?

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.

4) When declaring variales using Object
methods I may not need "alloc" or
"init"?

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.

5) Number declarations (BOOL, int, float,
etc) do not require memory management
as long as they're not declared as a
pointer?

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.

感情洁癖 2024-08-30 14:51:08

所有指针变量不再使用时都必须释放?

仅当“指针变量”指向 Objective-C 类的实例并且仅当您导致该实例被保留时之前。 阅读此内容:内存管理指南

每个指针变量( **)是否是某种类型的类?*

如果指针变量被声明为指向类实例的指针,那么它将指向一堂课。否则就不会了。指针正是如此;指向内存块的指针(引用)。该内存块可能是一个类、类的实例、C 结构或原始缓冲区(或其他内容)。

因此需要使用“alloc”和“init”(或类似的)来分配和初始化每个指针变量?

仅当指针指向 Objective-C 类的实例时。如果它是 C 结构,您可以使用 malloc()。即使对于 Objective-C 类,您也可能不会分配任何内容:

NSString *foo = [NSString stringWithFormat: @"Hello, %@", @"World!"];
NSString *bar = @"bar";
NSBundle *main = [NSBundle mainBundle];

(顺便说一句:以上都不需要 -release。)

使用对象方法声明变量时,我可能不需要“alloc”或“init”?

这个问题表明您应该阅读并重新阅读(我大约每 18 个月阅读一次)我编写 Objective-C 代码的第一个十年,每次都学到一些东西) -- Objective-C 语言指南

您或许还应该阅读一些有关 C 语言本身的内容。这可能会帮助您理解指针与其他类型。

数字声明(BOOL、int、float 等)只要不声明为指针就不需要内存管理?

是的,但它比特定类型简单得多。当您说int foo;时,您是在告诉编译器在本地范围(通常是在堆栈上)中开辟一些空间来存储 int 的数据。当你说int *foo;时,你是在告诉编译器在本地作用域中开辟出一点空间来存储一个指针,该指针指向内存中的一个位置,该位置包含一个 int。

因此,NSArray *bar 只是一个指向实例的指针,是的,NSArray bar;,如果没有明确禁止的话,会在堆栈来保存 NSArray 实例。

当您有一个指向某些东西的指针时,必须以某种方式初始化某些东西,并且通常是通过分配来完成的(但并非总是如此)。

that all pointer variables must be released when they're not used any more?

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.

Every pointer variable ( **) is a class of some kind, or not?*

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).

Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?

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 not alloc anything:

NSString *foo = [NSString stringWithFormat: @"Hello, %@", @"World!"];
NSString *bar = @"bar";
NSBundle *main = [NSBundle mainBundle];

(BTW: None of the above need a -release.)

When declaring variales using Object methods I may not need "alloc" or "init"?

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.

Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared as a pointer?

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 say int *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).

十级心震 2024-08-30 14:51:08

我相信你是对的:

  1. 是的。

  2. 我猜。我不确定我是否理解这个问题。

  3. 有点:正如您所说,如果您使用某些类方法,则不一定是alloc+init。因此,分配和初始化是隐式完成的。

  4. 正确。 (见上文)

  5. 我相信它不仅仅是数字,而且 C 基元不需要内存管理。

我建议您阅读 Apple 的内存管理规则 以及 斯坦福 iPhone 编程课程

I believe you are correct:

  1. Yes.

  2. I guess. I'm not sure I understand the question.

  3. 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.

  4. Correct. (see above)

  5. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文