Objective-C 中星号的使用:相关问题

发布于 2024-08-17 15:30:43 字数 379 浏览 7 评论 0原文

我有几个与此相关的问题:Asterisk在Objective-C中的使用

NSArray 数组;在本地范围内将是堆栈上“分配”的对象。 NSArray *数组;表示由一大块内存支持的对象,通常从堆中分配。

您如何知道何时在堆栈和堆上分配了某些内容?所有局部变量都在栈上,所有指针都在堆上吗?

因为您没有取消引用指向对象的指针,并且指向对象的指针在方法实现本身中至关重要。当你说...

呵呵

I had a couple questions related to this: Asterisk usage in Objective-C

NSArray array; in a local scope would be an object "allocated" on the stack. NSArray *array; indicates an object backed by a hunk of memory, typically allocated from the heap.

How do you know when something is allocated on the stack and on the heap? Are all local variables on the stack and are all pointers on the heap?

Because you aren't dereferencing the pointer to the object and that pointer to the object is critical within the method implementation itself. When you say...

Huh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

岁月打碎记忆 2024-08-24 15:30:43

如何知道何时在堆栈和堆上分配了某些内容?所有局部变量都在堆栈上吗...

没关系。栈和堆是实现细节; C 和 Objective-C 语言不了解它们,并且您通常没有任何理由关心某些东西是在堆栈上还是在堆上。

在 Mac OS X 上,局部变量位于堆栈上。但是,对于几乎所有目的来说,这都没有什么后果。别担心。

...所有指针都在堆上吗?

不,指针是内存地址;就这样。

指针变量可以位于任何其他变量可以位于的任何位置,也就是说,可以位于任何位置(受实现定义的限制的限制,您无需关心,如上所述)。

有关详细信息,请参阅我的指针教程

<块引用>

因为您没有取消引用指向对象的指针,并且指向对象的指针在方法实现本身中至关重要。当你说...

呵呵

指针是一个内存地址。因此,它指的是该地址处的内存。取消引用指针就是访问该内存。

你永远不会直接访问 Cocoa 对象占用的内存。您只需向它发送消息,询问它问题或告诉它做某事。因此,您永远不会取消引用该指针。

“……指向对象的指针在方法实现本身中至关重要。”意味着该对象在其方法实现中需要自己的指针。只能向指向对象的指针发送消息(通常会忽略此细节)。如果您以某种方式实现了这一点,则消息的接收者(即您发送消息的对象)将不会有自己的指针。

假设可以向取消引用的对象发送消息。标准仍然是向指向对象的指针发送消息,因此该对象很可能仍然需要该指向自身的指针,这使得向取消引用的对象发送消息的假设能力变得毫无用处。

因为它没有用,所以他们完全放弃了它。该对象需要自己的指针(该指针对于对象的方法实现至关重要),因此您只能向其指针发送消息。

How do you know when something is allocated on the stack and on the heap? Are all local variables on the stack …

It doesn't matter. The stack and heap are implementation details; the C and Objective-C languages do not know about them, and you should generally not have any reason to care whether something is on the stack or the heap.

On Mac OS X, local variables are on the stack. But, for almost all purposes, this is of no consequence. Don't worry about it.

… and are all pointers on the heap?

No. Pointers are memory addresses; that's all.

Pointer variables can be anywhere any other variables can, which is to say, anywhere (subject to implementation-defined limitations that you needn't care about, as noted above).

See my pointer tutorial for more information.

Because you aren't dereferencing the pointer to the object and that pointer to the object is critical within the method implementation itself. When you say...

Huh

A pointer is a memory address. As such, it refers to the memory at that address. Dereferencing the pointer is accessing that memory.

You never, ever directly access the memory a Cocoa object takes up. You only send it messages, to either ask it questions or tell it to do things. Thus, you never dereference the pointer.

“…that pointer to the object is critical within the method implementation itself.” means that the object, in its method implementations, will need its own pointer. It's only possible to send a message to a pointer to an object (this detail is usually elided). If you somehow pulled this off, the receiver of the message (that is, the object you messaged) would not have its own pointer.

Suppose it were possible to send a message to a dereferenced object. The norm is still to send messages to pointers to objects, so in all likelihood, the object will still need that pointer to itself—making that hypothetical ability to message a dereferenced object useless.

Since it's useless, they left it out entirely. The object will need its own pointer (the pointer is critical to the object's method implementations), so you can only send a message to its pointer.

尘世孤行 2024-08-24 15:30:43

* 是 C、C++ 和 Objective-C 的解引用运算符。一般来说,理解解引用运算符和内存管理比 Objective-C 更广泛。这是任何 C/C++/Objective-C 开发人员的基本技能。查看网上大量的 C 简介教程以了解更多信息。

编辑:任何关于 c 指针的教程都可以。比如这个 http://home.netcom.com/~tjensen/ptr/pointers .htm

The * is the dereference operator for C, C++ and Objective-C. Understanding the dereference operator, and memory management in general is far broader than Objective-C. This is a fundamental skill for any C/C++/Objective-C developer. Have a look at the multitude of intro C tutorials on the net to learn more.

Edit: any tutorial on c pointers will do. Such as this http://home.netcom.com/~tjensen/ptr/pointers.htm

顾忌 2024-08-24 15:30:43

在 Cocoa 中,你永远不会使用堆栈分配的对象;所有对象都将以 * 开头(请记住,类型“id”实际上是“指向某个对象的指针”的另一个词)并在堆上创建。

您将始终拥有 this:

NSArray     *myArray;

而永远不会拥有 this:

NSArray     myArray;

您可以忽略第二个块,因为您总是取消引用指针。

In Cocoa, you'll never use stack allocated objects; ALL objects will be prefaced with a * (remember that the type "id"is really another word for "pointer to SOME object") and created on the heap.

You'll always have this:

NSArray     *myArray;

and never this:

NSArray     myArray;

You can ignore the second chunk, since you're always dereferencing the pointer.

小忆控 2024-08-24 15:30:43

希望这些简单的玩具示例可以帮助您。

在C中,在函数中,

int x; // x is a variable of type int in stack
int *xp; // xp is a variable of type int * (pointer to int) in stack
int *xp2 = (int *) malloc(sizeof(int)); // xp2 is a variable in stack, it points to a memory location(size is of int) in heap
xp = &x; // xp can point to x
xp = xp2; // xp can also point to what xp2 points to
free(xp2); // now xp and xp2 point to a freed memory, BAD to use xp and xp2 now.
int **y; // y is a variable in stack, type is int **
y = (int **) malloc(sizeof(int *)); // allocate a piece of memory in heap, to hold a pointer to int(int *)
*y = (int *) malloc(sizeof(int)); // allocate a piece of memory in heap, to hold an int
**y = 100; // now we can use it
free(*y);
free(y);

在C++中,在函数或成员函数(方法)中,

SomeClass a1; // a1 is an object of type SomeClass in stack
SomeClass *a2 = new SomeClass(); // a2 is a pointer(in stack) pointing to an object(of type SomeClass) located in heap
delete a2;

所以在C++中,对象可以存在于堆栈或堆中,

在Java中,在函数或方法中,

SomeClass b1; // b1 is just a reference, no object exists yet
b1 = new SomeClass(); // in java, objects can only exist in heap
int x; // however, primitive types are in stack, 

在Objective-C中,在函数或方法中,

SomeClass c1; // you can't do this.
SomeClass *c2 = [[SomeClass alloca] init]; // c1 is a pointer in stack, pointing to an object in heap
[c2 release];

Hope these naive toy examples can help you.

In C, in a function,

int x; // x is a variable of type int in stack
int *xp; // xp is a variable of type int * (pointer to int) in stack
int *xp2 = (int *) malloc(sizeof(int)); // xp2 is a variable in stack, it points to a memory location(size is of int) in heap
xp = &x; // xp can point to x
xp = xp2; // xp can also point to what xp2 points to
free(xp2); // now xp and xp2 point to a freed memory, BAD to use xp and xp2 now.
int **y; // y is a variable in stack, type is int **
y = (int **) malloc(sizeof(int *)); // allocate a piece of memory in heap, to hold a pointer to int(int *)
*y = (int *) malloc(sizeof(int)); // allocate a piece of memory in heap, to hold an int
**y = 100; // now we can use it
free(*y);
free(y);

In C++, in a function or member function(method),

SomeClass a1; // a1 is an object of type SomeClass in stack
SomeClass *a2 = new SomeClass(); // a2 is a pointer(in stack) pointing to an object(of type SomeClass) located in heap
delete a2;

So in C++, objects can exist in stack or heap

In Java, in a function or method,

SomeClass b1; // b1 is just a reference, no object exists yet
b1 = new SomeClass(); // in java, objects can only exist in heap
int x; // however, primitive types are in stack, 

In Objective-C, in a function or method,

SomeClass c1; // you can't do this.
SomeClass *c2 = [[SomeClass alloca] init]; // c1 is a pointer in stack, pointing to an object in heap
[c2 release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文