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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:18:10+00:00 2026-05-13T23:18:10+00:00

I have the following casting problem when my data structure sSpecificData contains a field

  • 0

I have the following casting problem when my data structure sSpecificData contains a field of type tm:

typedef struct
{
   unsigned char  data[10000];
} sDataBuffer;

typedef struct
{
   int                 m_Id;
   sDataBuffer         m_Data;
} sData;

typedef struct {
   int                       m_value1;
   int                       m_value2;
   tm                        m_Date;
} sSpecificData;

const int SPECIFIC_SVC_DATA_SIZE = sizeof(sSpecificData);

typedef struct {
   int                    m_Id;
   sSpecificData          m_Data;
} sMyData;

int main(void)
{
       sData svc;
       sMyData* ptr1 = (sMyData*) &svc;
       sSpecificData* ptr2;
       ptr2 = (sSpecificData*) &svc.m_Data;
       ptr1->m_Data.m_value1 = 90;
       ptr1->m_Data.m_value2 = 80;
       cout << ptr1->m_Data.m_value1 << endl;
       cout << ptr1->m_Data.m_value2 << endl;
       cout << ptr2->m_value1 << endl;
       cout << ptr2->m_value2 << endl;       
       return 0;
}

Without the field “tm m_Date;” as part of the sSpecificData, the output is correct:

90
80
90
80

With the field “tm m_Date;” as part of the sSpecificData, the output is wrong:

90
80
0   <-- !
90  <-- !

Is there any idea why my example doesn’t work when there is field of type tm as part of the sSpecificData?

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-05-13T23:18:11+00:00Added an answer on May 13, 2026 at 11:18 pm

    this has to do with structure packing. by adding the tm field to the sSpecificData, you are changing it from a structure that has a natural alignment of int (4 bytes), to something that has a natural alignment of what appears to be 8 bytes.

    so with tm as part of sSpecificData, the structure sMyData is effectively this

    typedef struct {
       int                    m_Id;
       // 4 bytes of padding inserted to align sSpecificData on an 8 byte boundary.
       sSpecificData          m_Data; // with tm, this has a alignment of 8
    } sMyData;
    

    this explains what you are seeing. To fix this, you can add an explicit padding value to sMyData AND to sData.

    typedef struct
    {
       int                 m_Id;
       int                 m_padding;
       sDataBuffer         m_Data; // this accepts structures with an alignment of up to 8 
    } sData;
    
    typedef struct {
       int                    m_Id;
       int                    m_padding;
       sSpecificData          m_Data; // with tm, this has a alignment of 8
    } sMyData;
    

    Or you can use #pragma pack(4) (if your c compiler supports it) to force the padding to be left out of sMyData, but this will result in tm being misaligned which isn’t a good idea. see this related question.

    a very similar question came up today about C#, but the issues are the same, (the syntax for specifying packing is different though)
    Size of structures in .NET

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The application ID is stored in a the aspnet_Applications table… May 15, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer Grab a copy of Professional Apache Tomcat 6 from Wrox.… May 15, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer You will have to update the insert/update commands to specify… May 15, 2026 at 9:58 pm

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.