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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:12:02+00:00 2026-06-04T12:12:02+00:00

This is for an iOS project. I am reworking a section of my dataController

  • 0

This is for an iOS project. I am reworking a section of my dataController to use a 2D C array rather than nested NSMutableArrays for speed optimization. I am finding I need to perform thousands of integer addition operations on various sections of the array, and the object model is rather slow.

My array dimension is currently 710 x 55, the 710 number being dynamic. I also have 5 other arrays of the same size, possibly more in the future, hence my need to avoid NSArrays.

I wont post the entire source, so just the relevant parts:

@implementation MMEventDataController

int **wbOcMatrix = NULL;
int numEvents = 0;

-(void)generateMatrix {

for (NSDictionary *item in JSONData) {

{...}
// Here I parse some JSON data and bring part of it into newEvents.wb which is an
// NSMutableArray of ints. These can be ints 1 thru 55, which represent various
// flags that can be set. Only 5 flags will be inside each newEvent.wb. 
{...}

// Create some empty C arrays. This part is probably where I go wrong.

    wbOcMatrix = (int **) realloc (wbOcMatrix, (numEvents+1) * sizeof(int *));

    wbOcMatrix[numEvents] = malloc (55 * sizeof(int));

    int wbOcArray[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


// Here we find which 5 flags are set in newEvent.wb and set the corresponding index of
// wbOcArray to 1.

    for (id object in newEvent.wb) {

        int v = wbOcArray[[object intValue]-1];
        v++;
        wbOcArray[[object intValue] -1] = v;
        }

// Then we bring the new wbOcArray into the next index of the wbOcMatrix and increment.

    wbOcMatrix[numEvents] = wbOcArray;
    numEvents++;

}

// This process repeats for all items in the JSON data, at the moment is 710, thus
// creating an array 710 x 55.

The 2D array seems to be created well, meaning I have array of the proper size with data in it, HOWEVER, each row of the array contains the same data! And that data is from iteration 710.

My suspicion is that since my array is an array of pointers, each iteration is altering the data at the original pointer, and all rows point to the same place. So how can I allocate new memory space for each iteration? I thought that’s what the malloc was for…

  • 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-04T12:12:04+00:00Added an answer on June 4, 2026 at 12:12 pm

    You problem is here:

    int wbOcArray[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    

    This will free automatically when the loop is over. If you just put a NSLog(@"%p", wbOcArray); directly after the above line, you will see, that its always pointing to the same address.

    Replace this line with:

    int* wbOcArray = (int*)malloc(sizeof(int)*55);
    for(int i = 0; i < 55; i++) wbOcArray[i] = 0;
    

    Best,
    Christian

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

Sidebar

Related Questions

in my iOS project I use InAppSettings . This is missing a delegate in
This question is related to a iOS project written in ObjectiveC that makes use
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
Hopefully this helps somebody else Trying to compile an iOS project with Cedar BDD
I am using a Wordpress RSS to use in my iOS project. The feed
I am attempting to use both storyboards for my iOS project, but I cannot
I've created a blank project (iOS) and put this in my viewDidLoad: NSString *moviePath
I'm using a binary framework in my iOS project. This project is only available
This is my first time building an iOS project using any kind of advanced
Ok, so I picked up this iOS app dev project for something to work

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.