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

  • SEARCH
  • Home
  • 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 6219031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:42:35+00:00 2026-05-24T07:42:35+00:00

This is an interview question. Till now, I used to think such questions were

  • 0

This is an interview question. Till now, I used to think such questions were purely compiler dependent and shouldn’t worry me, but now, I am rather curious about it.

Suppose you are given two structures as:

struct A {  
  int* a;  
  char b;  
 }  

and ,

struct B {  
  char a;  
  int* b;  
}  

So which one would you prefer and why?
My answer went like this (though I was somewhat shooting in the dark) that the first structure should be preferred since the compiler allocates space for a structure in some multiples of the word size (which is the size of the pointer – 4 bytes on 32 bit machines and 8 bytes on 64 bit ones). So, for both the structures the compiler would allocate 8 bytes(assuming its a 32 bit machine). But, in the first case, the padding would be done after all my variables(i.e. after a and b). So even if by some chance, b gets some value that overflows and destroys my next padded bytes, but my a is still safe.

He didn’t seemed much pleased and asked for one disadvantage of the first structure over the second. I didn’t have much to say. 😀

Please help me with the answers.

  • 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-24T07:42:36+00:00Added an answer on May 24, 2026 at 7:42 am

    I don’t think there’s an advantage for any of this structures. There is one(!) constant in this equation. The order of the members of the struct is guaranteed to be as declared.

    So in case like the following, the second structure might have an advantage, since it probably has a smaller size, but not in your example, as they will probably have the same size:

    struct {
        char a;
        int b;
        char c;
    } X;
    

    Vs.

    struct {
        char a;
        char b;
        int c;
    } Y;
    

    A little more explanation regarding comments below:

    All the below is not a 100%, but the common way the structs will be constructed in 32 bits system where int is 32 bits:

    Struct X:

    |     |     |     |     |     |     |     |     |     |     |     |     |
     char  pad    pad   pad   ---------int---------- char   pad   pad   pad   = 12 bytes
    

    struct Y:

    |     |     |     |     |     |     |     |     |
     char  char  pad   pad   ---------int----------        = 8 bytes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an interview question I think of a solution. It uses queue. public
This was an interview question. I said they were the same, but this was
This is an interview question. Given a number of strings find such strings, which
I was asked this question in an interview and I gave various solutions but
This is an interview question I faced recently. Given an array of 1 and
This is an interview question asked a month ago.... Do session use cookies? If
This is an interview question. I have K machines each of which is connected
I got this in an interview question -- the question was more about what
This is probably a Microsoft interview question. Find the kth smallest element (ignoring duplicates)
This is an open ended interview question. I am not able to get 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.