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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:48+00:00 2026-05-25T15:52:48+00:00

My struct looks like this: struct tlv_msg { uint8_t datatype; //type of data /*

  • 0

My struct looks like this:

struct tlv_msg
{
   uint8_t datatype;   //type of data
   /* data stored in a union */
   union{
     int32_t s32val;          /*  int */
     int64_t s64val;          /*  long long */
     uint32_t u32val;         /*  unsigned int */
     uint64_t u64val;         /*  unsigned long long */
     char* strval;            /*  string */
     unsigned char* binval;   /*  any binary data */
   };

   uint32_t bytelen;  /* no. bytes of union/data part */
};

This struct uses a union to hold some different data types. I have an alloc function which allocates memory for the struct on the heap. Am I correct in thinking that if I am allocating for an integral type (ie the first four types above in union) I only need to allocate as follows:

tlv_msg* msg = malloc(sizeof(tlv_msg));

sizeof(tlv_msg) returns 24. I presume this is enough bytes to hold the largest data type in the union plus the other data members. (not sure why 24 – can someone explain?).

But if the data type to be stored is a pointer type, eg char* then I then need to also do this:

msg->strval = (char*)malloc(sizeof(string_length+1);

That would make sense to me and that seems to work but just wanted to check.

  • 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-25T15:52:48+00:00Added an answer on May 25, 2026 at 3:52 pm

    That’s perfectly right.

    That said, you may want to create helper functions, to help you dealing with this.

    For instance:

    tlv_msg * new_tlv_msg( void );
    
    /* There, you need to free struct members, if applicable */
    void delete_tlv_msg( tlv_msg * msg ); 
    
    /* Here you may copy your string, allocating memory for it */
    tlv_msg_set_strval( tlv_msg * msg, char * str );
    

    Implementation may be (basic, of course)

    tlv_msg * new_tlv_msg( void )
    {
        return calloc( sizeof( tlv_msg ), 1 ); 
    }
    
    void delete_tlv_msg( tlv_msg * msg )
    {
        if( msg->strval != NULL )
        {
            free( msg-strval );
        }
        free( msg );
    }
    
    tlv_msg_set_strval( tlv_msg * msg, char * str )
    {
        if( msg->strval != NULL )
        {
            free( msg-strval );
        }
        msg->strval = strdup( str );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that looks something like: struct Data { int value; };
I have a C++ struct that looks like this: struct unmanagedstruct { int flags;
I have to write an object in to binary file.My struct looks like this.
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
Say I have a struct that looks like this (a POD): struct Foo {
I have an array of structs called leaders. The struct class looks like this,
I have two arrays of structs. array_of_structs1 array_of_structs2 The struct class looks like this,
I have a C structure that looks like this typedef struct event_queue{ Event* event;
I've got a struct that looks like this: public struct MyStruct { public const
Mock class looks like this : struct MockClass { MOCK_METHOD0( foo, void () );

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.