Results 1 to 4 of 4
  1. #1

    Member Since
    Nov 18, 2008
    question about objective C memory allocation

    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;



  2. #2

    Member Since
    Dec 24, 2006
    N. Ireland
    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

    [dealloc arrayOfInts];

  3. #3

    Member Since
    Nov 18, 2008
    Thanks for the reply but I'm not so sure:


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

  4. #4

    xstep's Avatar
    Member Since
    Jun 25, 2005
    On the road
    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.
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to find GPU memory allocation.
    By Redheadsrule13 in forum Apple Notebooks
    Replies: 4
    Last Post: 01-11-2013, 07:08 AM
  2. dynamic mwmory allocation in objective c
    By drewangel in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 03-17-2010, 02:45 AM
  3. Memory allocation fails
    By skvikas in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 11-07-2008, 10:57 PM
  4. OS 9 applications: memory allocation
    By djmitch in forum OS X - Apps and Games
    Replies: 3
    Last Post: 06-14-2005, 02:46 PM
  5. memory allocation to apps
    By mogwai in forum OS X - Operating System
    Replies: 2
    Last Post: 05-18-2005, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts