Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:58:25+00:00 2026-06-16T23:58:25+00:00

will autorelease release my non-object c array? I am wondering, because perhaps only objects

  • 0

will autorelease release my non-object c array?
I am wondering, because perhaps only objects know their reference count?
here’s my code:

-(int *)getCombination{
    int xIndex = arc4random() % [self._num1 count] + 1;
    int yIndex = arc4random() % [self._num2 count] + 1;
    int *combination;
    combination[0] = [[self._num1 objectAtIndex:xIndex]intValue];
    combination[1] = [[self._num2 objectAtIndex:yIndex]intValue];
    return combination;
}

and this is my main() function:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([YYAAppDelegate class]));
    }
}

So is the autorelease working for objects only or will it release my c array from getCombination?

Edit:
Since the answer is no, autorelease doesn’t work for c arrays/pointers I used the following code which uses NSArrays instead:

#import <Foundation/Foundation.h>

@interface Multiplication : NSObject

@property (strong, nonatomic) NSMutableArray *_combinations;

-(id)initArrays;
-(NSArray *)getCombination;

@end

#import "Multiplication.h"

@implementation Multiplication
@synthesize _combinations;

-(void)initializeArray{
    self._combinations = [[NSMutableArray alloc]init];
    for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            NSNumber *x = [NSNumber numberWithInt:i];
            NSNumber *y = [NSNumber numberWithInt:j];
            [self._combinations addObject:[NSArray arrayWithObjects:x, y, [NSNumber numberWithInt:([x intValue] * [y intValue])], nil]];
        }
    }
}

-(NSArray *)getCombination{
    if ([self._combinations count] == 0) {
        [self initializeArray];
    }

    int index = arc4random() % [self._combinations count];
    NSArray *arr = [self._combinations objectAtIndex:index];
    [self._combinations removeObjectAtIndex:index];
    return arr;
}

-(id)initArrays{
    self = [super init];
    if (self) {
        [self initializeArray];
    }
    return self;
}

@end

BTW this function is supposed to provide a method for randomly displaying every combination in the multiplication table of 10X10 and restart when all combinations have been displayed and equal number of times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:58:26+00:00Added an answer on June 16, 2026 at 11:58 pm

    autorelease is a message that can be sent to Objective C objects. Before ARC, you needed to do it explicitly, like this:

    MyObject *obj = [[[MyObject alloc] init] autorelease];
    

    Under ARC, the compiler figures out the autorelease part for you, but the message is stil sent to the object; that object gets added to the autorelease pool as a result. When autorelease pool gets drained, all objects inside it are sent a release message. If there are no other references to the object, its reference count drops to zero, and the object get cleaned up.

    C arrays do not respond to autorelease or release message: they are not Objective C entities, so they do not respond to messages at all. Therefore, you must deal with the memory that you allocate for these arrays manually, by calling malloc1 and free.

    Of course you can place autoreleased objects inside a C array, and these objects will be cleaned up in the regular course of action. For example

    NSNumber **numbers = malloc(2, sizeof(NSNumber*));
    numbers[0] = [NSNumber numberWithInt:123];
    numbers[1] = [NSNumber numberWithInt:456];
    return numbers;
    

    If the caller does not retain NSNumber objects inside the numbers array, these objects will be autoreleased2. The numbers object, however, needs to be cleaned up separately:

    free(numbers);
    

    1 You did not call malloc in your code, so accessing combinatopn[...] is undefined behavior.

    2 Causing hanging references in the process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the basic principles about memory management (retain count, autorelease pools etc) in
Question #1 Will NSStringFromCGPoint() return an autorelease object or does the object needs to
Does autorelease guaranty that at the end of blocks the object will get released?
Will it auto release when the user quit the application? or it will stay
Will we see a release of VS2012 RC on Dreamspark (Student version of MSDN)?
I have a piece of code which will run only once in the background
I know in vast of the cases I don't have to release static variable.
In my application,I will be displaying only one row on the UITableView initially. I
I am a little confused about retain/release count when a local variable is allocated
Can I do any of the following? Will they properly lock/unlock the same object?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.