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

  • Home
  • SEARCH
  • 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 7735743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:34:38+00:00 2026-06-01T07:34:38+00:00

I did some research but cannot find a definite approval or disapproval. What I

  • 0

I did some research but cannot find a definite approval or disapproval.

What I want is, a fixed size structure + variable length part, so that serialization can be expressed in simple and less error prone way.

struct serialized_data
{
    int len;
    int type;
    char variable_length_text[0];
};

And then:

serialize_data buff = (serialize_data*)malloc(sizeof(serialize_data)+5);
buff->len=5;
buff->type=1;
memcpy(buff->variable_length_text, "abcd", 5);

Unfortunately I can’t find if MSVC, GCC, CLang etc., are ok with it.

Maybe there is a better way to achieve the same?

I really don’t want those ugly casts all around:

memcpy((char*)(((char*)buffer)+sizeof(serialize_data)), "abcd", 5);
  • 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-01T07:34:39+00:00Added an answer on June 1, 2026 at 7:34 am

    This program is using a zero length array. This is not C but a GNU extension.

    http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

    A common idiom in C89, called the struct hack, was to use:

    struct serialized_data
    {
        int len;
        int type;
        char variable_length_text[1];
    }; 
    

    Unfortunately its common use as a flexible array is not strictly conforming.

    C99 comes with something similar to perform the same task: a feature called the flexible array member.
    Here is an example right from the Standard (C99, 6.7.2.1p17)

    struct s { int n; double d[]; };
    int m = 12;  // some value
    struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did some research but all I could find was syncing data core with
I did some research but couldn't find the answer I was looking for so
I did comprehensive Google research but I cannot find any good solution, so any
I did some research but only find ugly hacks , which uses unique name
I did some research on the topic but could not find anything similar to
I did some research, but I couldn't reach the answer for my problem. I
Maybe my question is simple or not, but I did some research and I
I did some research and found that the only way to vertically center a
I did some research into the easiest/best way to generate a pdf in my
Did some googling and couldn't find a clear answer on this. My assumption is

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.