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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:02:12+00:00 2026-06-13T01:02:12+00:00

In C and C++, the compiler is not allowed to reorder data members of

  • 0

In C and C++, the compiler is not allowed to reorder data members of structs, so if you’re not careful with how you order them, you end up wasting space. For example:

struct S {
    int i;
    void *p;
    int i2;
};

On a platform with 32-bit ints and 64-bit pointers, i will be placed first, followed by 32 bits of padding so that p can be 64-bit–aligned. i2 then takes up half of the next word, followed by another 32 bits of padding. The resulting struct is 24 bytes long, whereas if p were declared first, it would be only 16 bytes long. If you have a lot of these structs in an array, finding and removing the padding can sometimes be an important optimization, to save memory and reduce cache churn.

I’m curious to know if Java has the same feature. Are unboxed types (such as int and boolean) the same size as references or smaller? And if they’re smaller, is the compiler allowed to reorder them to avoid inserting padding to align subsequent fields? Finally, if it is, do any compilers do this?

I don’t have a particular optimization need for this right now, I’m just curious to know if I should bear this in mind when choosing what order to declare my fields, like I do in C.

  • 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-06-13T01:02:13+00:00Added an answer on June 13, 2026 at 1:02 am

    int types are always 32-bit and references are usually 32-bit even in 64-bit JVMs.

    On the down side, Java has a 8-12 byte header at the start of each object and uses an 8-byte alignment. BTW some C++ environments have a 16-byte alignment.

    Are unboxed types (such as int and boolean) the same size as references or smaller?

    You can expect them to be smaller for boolean, byte, char and short, but the primitives can be larger for long and double than a reference.

    And if they’re smaller, is the compiler allowed to reorder them to avoid inserting padding to align subsequent fields?

    The JIT can re-organize the fields or even optimize them away.

    Finally, if it is, do any compilers do this?

    The javac compile does next to no optimizations and looking at the byte code will give you little clues as to what will happen at runtime. The JIT can optimize the fields in an objects anyway it chooses.

    I’m just curious to know if I should bear this in mind when choosing what order to declare my fields, like I do in C.

    IMHO, You can assume that just about every optimization trick you might have used in C no longer applies in Java. Of the few that do, they may not be exactly the same.

    You should assume the JIT will optimize the code as required and use profilers to determine if and when you have a problem. Only then consider altering the code for performance reasons.

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

Sidebar

Related Questions

The compiler gives me the error 'void' type not allowed here...<= operator cannot be
In java why is the following code not allowed by the compiler? public class
Why does the C# compiler not even complain with a warning on this code?
volatile is to tell the compiler not to optimize the reference, so that every
The problem is that the Eclipse compiler do not see any layout or ids,
I was wondering if there is a possible optimization where the compiler does not
Consider the code below. I don't understand why my GCC compiler does not try
Can a C++ compiler produce a not so good binary? You can think here
I am not overly competent in C++ and this compiler error is just making
I know compiler may or may not perform inline expansion of a function whether

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.