English 中文(简体)
射线记忆管理(Not NSArray,但类型[23])
原标题:Array memory management (Not NSArray, but type[23])

我刚刚开始开发SOS应用程序,我的问题涉及简单阵列。 我的申请是一种游戏,它大量利用了CG Point 构件。 由于这不是一个目标,我不能把它直接置于国家安全局的手中,不得不在国家安全局内加以总结。 罚款。

In other, mostly example code, I see the usage of simple arrays (i guess that s C?):

CGPoint manyPoints[40];
manyPoints[0] = CGPointMake(23, 42);
// And so on...

记忆的分配在哪里? 何时处置? 什么时候通过? 它是否被复制? 在目标C方面,我找不到任何关于这个专题的内容,但我不敢肯定会看什么。

博努斯问题: 如何将这种数据与NSArray相比较? 是否更快?

最佳回答

The statement CGPoint manyPoints[40]; allocates 40 CGPoints on the stack which means that (in general) you can t pass them around. When you leave the current stack frame (falls out of the current set of curly brackets) the data will automatically be deallocated.

也可以做以下工作:

CGPoint* manyPoints = malloc(40 * sizeof(CGPoint);

// blah

manyPoints[0] = CGPointMake(23, 42);

// I m done now
free(manyPoints);

你也许会更幸运地在C周围搜寻(目标C)。

是的,几乎肯定比使用<代码>更快。 NSArray/NSValue

问题回答

右铭——CG只是一个C 构件。

记忆的分配在哪里?

如前所述, st。

如果你使用<代码>小型,则该编码将出现在(除非选择不适用)。

何时处置?

as declared, when the scope exits, the region is available for reuse on the stack.

如果您的<代码>小已贴出,则您必须填写<>无<>>>。

What happens when i pass it around? Is it copied?

这取决于你如何通过。 语言提供了按价值和参考加以利用的选择。

在目标C方面,我找不到任何关于这个专题的内容,但我不敢肯定会看什么。

a C书记本

博努斯问题: 如何将这种数据与NSArray相比较? 是否更快?

批量分配和物体较快,尽管它们可以被滥用,而且对所有人都有很好的用途。

<代码>CGPoint为“结构类型”。 《宣言》将记忆直接分配给ack。 在C中,你可以直接把 st作为价值观。 CG PointMake()可能认为:

struct CGPoint {
     CGFloat x;
     CGFloat y;
};
typedef struct CGPoint CGPoint;

 CGPoint CGPointMake(CGFloat x, CGFloat y)
 {
     CGPoint returnValue; // allocated directly on the stack
     returnValue.x = x;   // Do not confuse this with Objective-C dot notation.  It is NOT the same thing
     returnValue.y = y;
     return returnValue;
 }

这完全合法,整个结构如下:copied,不作为回报值。

博努斯问题: 如何将这种数据与NSArray相比较? 是否更快?

但我要告诉大家,答案是肯定的。 没有目标C的信息。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签