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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:30:07+00:00 2026-05-12T07:30:07+00:00

I am writing a light weight serialization function and need to include two variable

  • 0

I am writing a light weight serialization function and need to include two variable sized arrays within this.

  • How should I track the size of each?
  • How should I define the struct?
  • Am I going about this all wrong?

EDIT: the result must be a contiguous block of memory

  • 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-12T07:30:07+00:00Added an answer on May 12, 2026 at 7:30 am

    Since the data should be continuous in memory it is necessary to malloc a chunk of memory of the right size and manage it’s contents more or less manually. You probably best create a struct that contains the “static” information and related management functions that do the memory management and give access to the “dynamic” members of the struct:

    typedef struct _serial {
      size_t sz_a;
      size_t sz_b;
      char data[1]; // "dummy" array as pointer to space at end of the struct
    } serial;
    
    serial* malloc_serial(size_t a, size_t b) {
      serial *result;
      // malloc more memory than just sizeof(serial), so that there
      // is enough space "in" the data member for both of the variable arrays
      result = malloc(sizeof(serial) - 1 + a + b);
      if (result) {
        result->sz_a = a;
        result->sz_b = b;
      }
      return result;
    }
    
    // access the "arrays" in the struct:
    
    char* access_a(serial *s) {
      return &s->data[0];
    }
    
    char* access_b(serial *s) {
      return &s->data[s->sz_a];
    }
    

    Then you could do things like this:

      serial *s = ...;
      memcpy(access_a(s), "hallo", 6);
      access_a(s)[1] = 'e';
    

    Also note that you can’t just assign one serial to another one, you need to make sure that the sizes are compatible and copy the data manually.

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This regular expression: ^https://.*/MyTest$ will do what you ask. ^… May 12, 2026 at 12:15 pm
  • Editorial Team
    Editorial Team added an answer The Google Maps API provides a geocoder webservice that you… May 12, 2026 at 12:15 pm
  • Editorial Team
    Editorial Team added an answer I split the repository into two interfaces, one for reading… May 12, 2026 at 12:15 pm

Related Questions

I am writing a simple database with web access. I have previous experience with
If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
I am deciding on how to develop a GUI for a small c++/win32 api
Ok, I admit it - I wrote my own view state facility for ASP.NET
I am writing a C++/MFC application that will use Microsoft Access to store data.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.