Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

sizeof struct

Posted on 2005-03-29
1
Medium Priority
?
930 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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

577 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