question about objective C memory allocation

Joined
Nov 18, 2008
Messages
2
Reaction score
0
Points
1
Hi,

I'm a newcomer to Objective C and Cocoa (C++ background mainly). I found the following code (simplified) in a method of an objective C class:

-(void) methodName
{
int i;
int size = 4;
int arrayOfInts[size];
for (i = 0; i< size; i++)
{
arrayOfInts = i;

}

}

.... so it looks like you can dynamically allocate an array of length size like this (which you can't do in C++). Is this the case or am I missing something? If it is the case, what is the appropriate way to deallocate the memory? Something like:

delete [] arrayOfInts;

?

Thanks.
 

Del


Joined
Dec 24, 2006
Messages
901
Reaction score
15
Points
18
Location
N. Ireland
Your Mac's Specs
Mac Pro 2xQuad core 2.8GHZ
Haven't yet started to teach myself objective C (have just read a tiny wee bit), but i think its

Code:
[dealloc arrayOfInts];
 
OP
T
Joined
Nov 18, 2008
Messages
2
Reaction score
0
Points
1
Thanks for the reply but I'm not so sure:

Using

[dealloc arrayOfInts]

gave a compiler error: 'dealloc not declared in this scope' which makes sense, since this syntax is for passing a message (in this case 'arrayOfInts') to an object (here dealloc). I also tried

[arrayOfInts dealloc]

which gives the warning 'invalid receiver type 'int [1]''. This also makes a certain amount of sense, since arrayOfInts is not an object.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
That looks like standard C to me, which means C++ should have the same ability if you choose to code it that way.

A quick test says, yes, in your case scenario you can dynamically allocate the size of that array. I think it may be because you are within an function. This wikipedia entry suggests it is freed automatically after exiting the function. See the RAII entry and read the first paragraph below the samples.

For more on this you might want to check out this link.

To get started in Objective-C, Cocoa Dev Central has some nice tutorials. Also set up a free Apple Developer membership so you can see further writings there.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top