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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:22:17+00:00 2026-05-26T10:22:17+00:00

i coded a small program to show you the casting problem #include <stdlib.h> struct

  • 0

i coded a small program to show you the casting problem

#include <stdlib.h>

struct flags {

    u_char flag1;
    u_char flag2;
    u_short flag3;
    u_char flag4;
    u_short flag5;

    u_char flag7[5];
};


int main(){

    char buffer[] = "\x01\x02\x04\x03\x05\x07\x06\xff\xff\xff\xff\xff";

    struct flags *flag;

    flag = (struct flags *) buffer;

    return 0;
}

my problem is when i cast the flag 5 wrongly takes the “\x06\xff” bytes ignoring the “\x07” and the flag 7 wrongly takes the next 4 “\xff” bytes plus a nul which is the next byte.I also run gdb

(gdb) p/x flag->flag5 
$1 = 0xff06
(gdb) p/x flag->flag7 
$2 = {0xff, 0xff, 0xff, 0xff, 0x0}
(gdb) x/15xb flag
0xbffff53f: 0x01    0x02    0x04    0x03    0x05    0x07    0x06    0xff
0xbffff547: 0xff    0xff    0xff    0xff    0x00    0x00    0x8a

why this is happening and how i can handle it correctly?
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-26T10:22:18+00:00Added an answer on May 26, 2026 at 10:22 am

    It seems like structure member alignment issues. Unless you know how your compiler packs structure members, you should not make assumptions about the positions of those members in memory.

    The reason that the 0x07 is apparently lost, is because the compiler is probably aligning the flag5 member on a 16-bit boundary, skipping the odd memory location that holds the 0x07 value. That value is lost in the padding. Also, what you are doing is overflowing the buffer, a big no-no. In other words:

    struct flags { 
        u_char flag1;    // 0x01
        u_char flag2;    // 0x02
        u_short flag3;   // 0x04 0x03
        u_char flag4;    // 0x05
        // 0x07 is in the padding
        u_short flag5;   // 0x06 0xff 
        u_char flag7[5]; // 0xff 0xff 0xff 0xff ... oops, buffer overrun, because your
                         // buffer was less than the sizeof(flags)
    }; 
    

    You can often control the packing of structure members with most compilers, but the mechanism is compiler specific.

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

Sidebar

Related Questions

I need to write a small program for the university. The problem is, it
I'm coding a small program to time and show, in a ordered fashion, my
I have a small program <350ish lines of code on 5 forms>. It works
I have a game engine that uses OpenGL for display. I coded a small
I have a gallery I quickly coded up for a small site, and under
I've been quite used to working on small projects which I coded with 1,000
Currently I'm working on a small program that reads large files and sorts them.
I've written a small java program to download all friends' and followers' profile pictures
I have a program to process very large files. Now I need to show
I'm writing a small program to convert a standard definition 4:3 video to a

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.