C language Variable length message definition: Flexible array

Source: Internet
Author: User

In the process of exchange between the front and back of the game, often used to become the message body, because some content is the size of the location, such as a microblog, the content size of the microblog is unknown.

The general practice is to define a pointer of type char*, and then specify its length, as follows:

typedef struct{unsigned len;char* PData;} MSG;

The use of the time is this:

Char str[] = "Hello world!"; unsigned len = sizeof (str); msg* m = (msg*) malloc (sizeof (MSG) +len*sizeof (char)), M->len = Len;m->pdata = (char*) (m+1); memcpy (m+1, str, len); printf ("%d,%s\n", M->len, M->pdata);

Do you think the timing of char* pdata a lot more?

Because the data time is stored in m+1, we can directly get this pointer, without redefining a char* pdata to error this position.

This brings us to another problem where access is inconvenient, we cannot access it in the way of struct members, we can use a flexible array, and see:

typedef struct{unsigned LEN;CHAR data[];} Message;

That's how it's used:

message* msg = (message*) malloc (sizeof (Message) + len*sizeof (char)); Msg->len = len;memcpy (Msg->data, str, len); printf ("%d,%s\n", Msg->len, Msg->data); free (msg);

To divide the complete code comparison:


Array0.h

typedef struct{unsigned len;char* PData;} Msg;typedef struct{unsigned Len;char data[];} Message;

Main.c

Test for 0 size array#include <stdio.h> #include <stdlib.h> #include <string.h> #include "array0.h" int main () {char str[] = "Hello world!"; unsigned len = sizeof (str);//general usage msg* m = (msg*) malloc (sizeof (MSG) +len*sizeof (char)); M->len = Len;m->pdata = (char *) (m+1); memcpy (m+1, str, len);p rintf ("%d,%s\n", M->len, M->pdata); free (m);//flexible array message* msg = (message*) malloc (sizeof (Message) + len*sizeof (char)); Msg->len = len;memcpy (Msg->data, str, len);p rintf ("%d,%s\n", Msg->len, Msg->data); free (msg); System ("pause"); return 0;}


C language Variable length message definition: Flexible array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.