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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:56:04+00:00 2026-05-10T17:56:04+00:00

I have a C++ program representing a TCP header as a struct: #include stdafx.h

  • 0

I have a C++ program representing a TCP header as a struct:

#include 'stdafx.h'  /*  TCP HEADER      0                   1                   2                   3        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |          Source Port          |       Destination Port        |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |                        Sequence Number                        |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |                    Acknowledgment Number                      |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |  Data |           |U|A|P|R|S|F|                               |    | Offset| Reserved  |R|C|S|S|Y|I|            Window             |    |       |           |G|K|H|T|N|N|                               |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |           Checksum            |         Urgent Pointer        |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |                    Options                    |    Padding    |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |                             data                              |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  */  typedef struct {        // RFC793     WORD         wSourcePort;     WORD         wDestPort;     DWORD        dwSequence;     DWORD        dwAcknowledgment;     unsigned int byReserved1:4;     unsigned int byDataOffset:4;     unsigned int fFIN:1;     unsigned int fSYN:1;     unsigned int fRST:1;     unsigned int fPSH:1;     unsigned int fACK:1;     unsigned int fURG:1;     unsigned int byReserved2:2;     unsigned short wWindow;     WORD         wChecksum;     WORD         wUrgentPointer; } TCP_HEADER, *PTCP_HEADER;   int _tmain(int argc, _TCHAR* argv[]) {     printf('TCP header length: %d\n', sizeof(TCP_HEADER));     return 0; } 

If I run this program I get the size of this header as 24 bytes, which is not the size I was expecting. If I change the type of the field ‘wWindow’ to ‘unsigned int wWindow:16’, which has the same number of bits as an unsigned short, the program tells me the size of the struct is now 20 bytes, the correct size. Why is this?

I am using Microsoft Visual Studio 2005 with SP1 on a 32-bit x86 machine.

  • 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. 2026-05-10T17:56:05+00:00Added an answer on May 10, 2026 at 5:56 pm

    See this question: Why isn't sizeof for a struct equal to the sum of sizeof of each member? .

    I believe that compiler takes a hint to disable padding when you use the ‘unsigned int wWindow:16’ syntax.

    Also, note that a short is not guaranteed to be 16 bits. The guarantee is that: 16 bits <= size of a short <= size of an int.

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

Sidebar

Ask A Question

Stats

  • Questions 205k
  • Answers 205k
  • 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 Storing Session State is the right key word. http://msdn.microsoft.com/en-us/library/ms972429.aspx http://www.devarticles.com/c/a/ASP/Maintaining-Session-State-With-ASP/… May 12, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer Two options; params, or just new[] {value} Meaning: WriteAllLines(string file,… May 12, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer Ok, I fixed it. For anyone else that has this… May 12, 2026 at 8:54 pm

Related Questions

I'm new to C++, this is my first week since the upgrade from fortran.
I have the requirement of generating UML Diagrams for one of my C++ assignments.
I have a C++ program on Linux that crashes after some time with the
I have a C++ program that benchmarks various algorithms on input arrays of different

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.