New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

question about objective C memory allocation


Post Reply New Thread Subscribe

 
Thread Tools
thuffir

 
Member Since: Nov 18, 2008
Posts: 2
thuffir is on a distinguished road

thuffir is offline
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] = 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.
QUOTE Thanks
Del

 
Member Since: Dec 24, 2006
Location: N. Ireland
Posts: 901
Del has a spectacular aura about
Mac Specs: Mac Pro 2xQuad core 2.8GHZ

Del is offline
Haven't yet started to teach myself objective C (have just read a tiny wee bit), but i think its

Code:
[dealloc arrayOfInts];
QUOTE Thanks
thuffir

 
Member Since: Nov 18, 2008
Posts: 2
thuffir is on a distinguished road

thuffir is offline
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.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« Translation of Iris script into Darwin | Problems compiling a library »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Memory allocation fails skvikas OS X - Development and Darwin 1 11-07-2008 09:57 PM
My PowerMac G5 is acting screwy! aedolch OS X - Operating System 5 08-01-2008 06:12 PM
Question on calculating memory used in a program catsophie OS X - Development and Darwin 2 12-23-2007 01:02 PM
Memory Question (Using PC Memory in Mac Pro) dj_hype Switcher Hangout 4 12-17-2007 05:14 PM
Power PC G3 Memory question Malibu Other Hardware and Peripherals 1 10-13-2003 08:54 AM

All times are GMT -4. The time now is 08:45 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?