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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:54:05+00:00 2026-06-09T01:54:05+00:00

I have the following packet layout: struct PacketLayout { int GateCode; BYTE StringLen; char

  • 0

I have the following packet layout:

struct PacketLayout
{
  int GateCode;
  BYTE StringLen;
  char String[StringLen];
  BYTE ServerStatus;
  BYTE ServerStorage;
  BYTE ServerNumber;
}

The class is this:

class ServerInfo
{
  short PacketSize;   //Size of the whole packet
  BYTE TotalServers; //total of PacketLayout structs
  PacketLayout Server[TotalServers];
  int GlobalSecCode;
  short EncryptedPacketSize; //Same as the first member, just xored
}

So the problem i have is making an variable size array inside an class or an struct which size depends of the last member pointed by BYTE StringLen (for struct) and BYTE TotalServers (for the class).

I don’t know what is the solution to this, maybe implement a template?, if that’s so can i see an example (i am not familiar with templates yet) also i want to reference my member names without calculating the pointer position by myself (as i am currently doing now).

Thanks.

  • 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-09T01:54:06+00:00Added an answer on June 9, 2026 at 1:54 am

    Doing this with a template is possible, for example:

    template <int StringSize>
    struct PacketLayout
    {
      int GateCode;
      BYTE StringLen;
      char String[StringSize];
      BYTE ServerStatus;
      BYTE ServerStorage;
      BYTE ServerNumber;
    };
    

    lets you use this as:

    PacketLayout<100> pkt;
    

    Normally what you would want to do is simpler though. For example if you reorder the packet and know the upper limit on size you can do simply:

    struct PacketLayout
    {
      int GateCode;
      BYTE StringLen;
      BYTE ServerStatus;
      BYTE ServerStorage;
      BYTE ServerNumber;
      char String[MAX_POSSIBLE_SIZE];
    };
    

    Or alternatively:

    struct PacketLayout
    {
      int GateCode;
      BYTE StringLen;
      BYTE ServerStatus;
      BYTE ServerStorage;
      BYTE ServerNumber;
      char *String;
    };
    

    and allocate/set String during reading.

    Personally though I’d skip all this messy low level details and use something like protobuf to do the work for you and leave you free to concentrate on the more important higher level things that add value to your project.

    There’s a common but dirty trick used sometimes too:

    struct PacketLayout
    {
      int GateCode;
      BYTE StringLen;
      BYTE ServerStatus;
      BYTE ServerStorage;
      BYTE ServerNumber;
      char String[1];
    };
    

    Where people define the size of the variable part at the end to be 1 and then deliberately allocate more memory than needed for the struct so they can write past the end of it. This is evil and very much not recommended though.

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

Sidebar

Related Questions

I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code void PacketEncrypt(Packet* packet, int sizeofpacket) { int* pointer; pointer
I have the following packet format: 1B 4B 1B 1B string 2 bytes 1B
I have the following C struct from the source code of a server, and
I have an input string that will either be a JSON packet, ala: {PHONE:555-513-4318,FIRSTNAME:Austin,ARTISTID:2,LASTNAME:Weber}
I have a byte array that represents a complete TCP/IP packet. For clarification, the
Hi I have the following sub: static ustr *construct_packet(int ptype, int stype, int argc,
I have the following C code: static void* heap; static unsigned int ptr; int
If you have the following class as a network packet payload: class Payload {
I have the following code to send as audio RTP packet some DTMF digits:

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.