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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:54:34+00:00 2026-05-17T20:54:34+00:00

Back with my daily silly question. Today I’m trying to put a struct in

  • 0

Back with my daily silly question. Today I’m trying to put a struct in an NSArray. Alright, that’s easy, wrap it in a NSData. If you know me, you know you’re about to see code. In this example, I’m loading a vertex line from a Wavefront OBJ into a struct and sticking it in an array.

        NSArray *verticeLineParts = [currentLine componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        Coordinate c = CoordinateMake([[verticeLineParts objectAtIndex:1] floatValue],
                                      [[verticeLineParts objectAtIndex:2] floatValue],
                                      [[verticeLineParts objectAtIndex:3] floatValue]);
        [vertices addObject:[[NSData alloc] initWithBytes:&c length:sizeof(c)]];

This seems to work fine. The first few objects in the output:

2010-10-18 14:18:08.237 ObjLoader[3778:207] (
    <5d1613bd 13f3da3f 8ac745bd>,
    <f04c28bd 13f3da3f d88048bd>,
    <649427bd d61ddb3f d88048bd>,
    <477625bd d845db3f d88048bd>,
    <4c1722bd 1668db3f d88048bd>,

When it comes to printing them out after pulling them from the array, not so much. The offending code:

for (int i = 0; i < [vertices count]; i++) {
    Coordinate c;
    fillCoordinateFromData(c, [vertices objectAtIndex:i]);
    NSLog(@"%@", CoordinateToNSString(c));
}

[snip]

void fillCoordinateFromData(Coordinate c, NSData *d) {
    void *bytes = malloc(12);
    [d getBytes:bytes];
    memcpy((void *)&c, &bytes, 12);
    free(bytes);
}

Of course, the offending output:

2010-10-18 14:22:00.692 ObjLoader[3825:207] 9.885923, -6.837428, 0.000000
2010-10-18 14:22:00.693 ObjLoader[3825:207] 9.885923, -6.837428, 0.000000
2010-10-18 14:22:00.694 ObjLoader[3825:207] 9.885923, -6.837428, 0.000000
2010-10-18 14:22:00.695 ObjLoader[3825:207] 9.885923, -6.837428, 0.000000
2010-10-18 14:22:00.695 ObjLoader[3825:207] 9.885923, -6.837428, 0.000000

I think this is caused by C wizardry attempted by someone who’s not a C wizard, kind of like The Sorcerer’s Apprentice. I think this should be obvious to a wizard of the 99th degree, or if someone can suggest a more modern way to do this, let me know.

Thanks,
Will

  • 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-17T20:54:34+00:00Added an answer on May 17, 2026 at 8:54 pm
    void fillCoordinateFromData(Coordinate c, NSData *d) {
    

    This passes c by value. The modification of c in this function won’t be reflected back to the caller.

    You need to pass by pointer (reference) instead.

    fillCoordinateFromData(&c, [vertices objectAtIndex:i]);
    
    void fillCoordinateFromData(Coordinate* pc, NSData *d) {
        void *bytes = malloc(12);
        [d getBytes:bytes];
        memcpy((void *)c, &bytes, 12);
        free(bytes);
    }
    

    but it is better to just return the Coordinate.

    Coordinate c = getCoordinateFromData([vertices objectAtIndex:i]);
    
    Coordinate getCoordinateFromData(NSData* d) {
      Coordinate c;
      [d getBytes:&c];   // and let's not waste time allocating 12 bytes.
      return c;
    }
    

    BTW, by convention structs are wrapped in NSValue, not NSData.

    [vertices addObject:[NSValue valueWithBytes:&c withObjCType:@encode(Coordinate)]];
    
    Coordinate getCoordinateFromData(NSValue* d) {
      Coordinate c;
      [d getValue:&c];
      return c;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got 10 GB of files to back up daily to another site. The
Back when I was learning HTML, I loved how easy it to build pages
Back in the past, i found a third party webpage that was able to
I have an application that i run daily to send contract updates to CRM
I have a database called RankHistory that is populated daily with each user's username
I want to run a daily back up of an SQL database. I was
Hi I have multiple databases need to back up daily. Currently, I am using
I have a table which is populated by a daily scheduled job that deletes
Coming back to flex after not using it for a while, trying to load
I am working on an internal app that displays the daily schedule for a

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.