?
Solved

sizeof struct

Posted on 2005-03-29
1
Medium Priority
?
923 Views
Last Modified: 2008-04-05
Hi,

This might be elementary.I have a structure with an int and char . The size of the structure returned is 8. Shouldn't it be 5?

struct temp2
      {
            int i;
            char j;
      };
      temp2 objtemp;
      cout<<sizeof(objtemp.i);   //returns 4
      cout<<sizeof(objtemp.j);   //returns 1

      cout<<sizeof(temp2);  //returns 8

Regards,
0
Comment
Question by:ink777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
novitiate earned 200 total points
ID: 13650668
no, it should be 8.

Its the alignment, what matters.

if you want to change it, you can use

#pragma pack(2)

now size will be 6

#pragma pack(1)

size will be 5

_novi_
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question