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 964125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:46:41+00:00 2026-05-16T01:46:41+00:00

I’m currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional

  • 0

I’m currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional NSMutableArray of NSNumbers. I keep an array in my SudokuGridView, for displaying the numbers in the grid. When it comes time to solve the puzzle, I pass a [grid numberGrid] to a subclass of NSOperation I’ve created that solves the puzzle.

The grid’s array is defined as a property as such:

@property (readonly) NSMutableArray *numberArray; 

When passing it to the sudoku grid solver I go:

MESudokuSolver *solvePuzzleOperation  = [[MESudokuSolver alloc] initWithPuzzle: [grid numberArray]];

initWithPuzzle is defined as so:

- (id)initWithPuzzle:(NSMutableArray *)puzzleArray  {
    if(self = [super init]) {
        puzzle = [[NSMutableArray alloc] initWithArray: puzzleArray];
    }
    return self;    
}

When I then convert the puzzle to a primitive int array to solve it, and then back into the puzzle NSMutableArray. What’s funny, is that now, the grid’s NSMutableArray now has the solution… Which means that somehow inside the MESudokuSolver the grid’s array is being modified. So I did some investigation, the pointer to the array that is passed into the MESudokuSolver instance is different than the MESudokuSolver’s puzzle NSMutableArray. Strange, right? I know.

Upon FURTHER investigation, the pointer to the NSNumbers inside the arrays with different pointers are actually the SAME.

To you StackOverflow, I ask, WTF?

  • 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-05-16T01:46:42+00:00Added an answer on May 16, 2026 at 1:46 am

    When you initialise an array with the contents of the another array, the contents of both arrays will reference the same objects. What you want to do is perform a deep copy. This ensures that each array references its own copy of the object, so that if you modify the object in one array, it does not affect the object in the other array because they are actually different objects. This applies even to arrays of arrays. There are a number of approaches to performing deep copies. Since you want mutable copies of your mutable arrays inside your mutable array, it is a little trickier, but easy nonetheless:

    // Implemented as a free function here, but this is not required.
    
    NSMutableArray *MECopyGrid(NSMutableArray *outer)
    {
        NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[outer count]];
    
        for (NSMutableArray *inner in outer)
        {
            NSMutableArray *theCopy = [inner mutableCopy];
            [result addObject:theCopy];
            [theCopy release];
        }
    
        return result;
    }
    

    Beware also of NSNumber optimisations. Cocoa (and I assume Cocoa Touch also) caches a few different NSNumber instances. Since NSNumber instances are immutable, if you ask for [NSNumber numberWithInteger:1], Cocoa may give you a reference to an existing instance containing the same value. If you notice that NSNumber instance pointers are the same, it is likely because Cocoa has given you an old instance. This would save memory especially in situations like yours (without optimisations you would need 81 independent instances of NSNumber, but with optimisations you’ll need at most only 9).

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

Sidebar

Related Questions

No related questions found

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.