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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:34:23+00:00 2026-06-14T18:34:23+00:00

I want to save the state of my structs as a binary file and

  • 0

I want to save the state of my structs as a binary file and load them again.
My structs look like this:

typedef struct
{
  uint8_t pointerLength;
  uint8_t *pointer;
  uint8_t NumBla;
  uinT16 Bla[MAX_NUM_Bla];
   ...
}
BAR_STRUCT, *BAR;

typedef struct
{
  int numBar;
  BAR bars[MAX_NUM_BAR];
}
FOO_STRUCT, *FOO;

Saving is no problem, but restoring the state.
Iam at the point where the bytestring from the file is on the heap and a pointer is pointing to the first adress of this string.
And I do as follows:

const void* dataPointer //points to adress in heap
unsigned char* bytePointer = (unsigned char*)dataPointer;

FOO foo = (FOO_STRUCT*)bytePointer;
bytePointer += sizeof(FOO_STRUCT);  

for (int i=0; i < MAX_NUM_BAR; i++) {
    foo->bars[i] = (BAR_STRUCT*)bytePointer;
}

The last assignment doesn’t work and I get an EXC_BAD_ACCESS.
Because bars is an array of pointers i need to correct the adresses of each element is pointing to. Because they are not valid anymore. So I try to assign the adress of the object I saved in the bytesteam to foo->bars[i];
But I can not change foo->bars[i] at all. Accessing works but but assigning a new adress doesn’t.
I wonder why.

Edit:
Iam working on OSX so to write bytes to file i use NSData:

bool saveCurrentState( FOO foo){
  NSMutableData *data = [NSMutableData data];
  [data appendBytes:templates length:sizeof(FOO_STRUCT)];
  for (int i=0; i < MAX_NUM_BAR; i++){
    [data appendBytes:&foo->bar[i] length:sizeof(INT_BAR_STRUCT)];
    if( foo->bar[i] != NULL )
      [data appendBytes:foo->bar[i]->pointer length:sizeof(uint8_t)*foo->bar[i]->length];
    ...
  }
}

I can’t really change the structs since they are used in a lib, which i don’t really have access too. I am especially interested in why it is not working as expected.
As you can see I copy the whole struct including the pointer itself and adding the structures the pointer were pointing to to the byte string. Hope this give a bit more clarification.

When reading byte stream to setup the structs i just need the change the values of the array with the new address and that for some reason doesn’t want to work.

Edit:
Iam also saving/reading the file with NSData:

NSData* data = [NSData dataWithContentsOfFile:@"path/to/file"];
const void* dataPointer = [data bytes];
  • 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-14T18:34:25+00:00Added an answer on June 14, 2026 at 6:34 pm

    Looks like what you are after is a serialization library. Try something like protocol buffers or tpl (choice depends on your exact needs, check the docs).

    This previous question seems to do a decent job of covering some of the issues.

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

Sidebar

Related Questions

I want to save the swtich state and load it when the program restarted.
Want to save the current state of my canvas as a picture, jpg file,
I have a problem. I want to save a XAML state into a file,
When I want to save some state behavior of the activity, The docs says
I have a Seekbar and I want to save the state to database when
I want to save values as an ArrayList of double in a file. Whenever
I want to save my program's DataModel objects to a file, and be able
I want to save a matrix to a text file, so I can read
I want to save and load my xml data using XmlReader. But I don't
I want to know how can I save state of my application in android.What

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.