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

The Archive Base Latest Questions

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

I have several questions to ask that pertains to data position and alignment in

  • 0

I have several questions to ask that pertains to data position and alignment in C++. Do classes have the same memory placement and memory alignment format as structs?

More specifically, is data loaded into memory based on the order in which it’s declared? Do functions affect memory alignment and data position or are they allocated to another location? Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct. I’m just curious to know whether or not this is intrinsic to classes as it is to structs and whether or not it will translate well into classes if I chose to use that approach.

Edit: Thanks for all your answers. They’ve really helped a lot.

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

    Do classes have the same memory placement and memory alignment format
    as structs?

    The memory placement/alignment of objects is not contingent on whether its type was declared as a class or a struct. The only difference between a class and a struct in C++ is that a class have private members by default while a struct have public members by default.

    More specifically, is data loaded into memory based on the order in
    which it’s declared?

    I’m not sure what you mean by “loaded into memory”. Within an object however, the compiler is not allowed to rearrange variables. For example:

    class Foo {
        int a;
        int b;
        int c;
    };
    

    The variables c must be located after b and b must be located after a within a Foo object. They are also constructed (initialized) in the order shown in the class declaration when a Foo is created, and destructed in the reverse order when a Foo is destroyed.

    It’s actually more complicated than this due to inheritance and access modifiers, but that is the basic idea.

    Do functions affect memory alignment and data position or are they
    allocated to another location?

    Functions are not data, so alignment isn’t a concern for them. In some executable file formats and/or architectures, function binary code does in fact occupy a separate area from data variables, but the C++ language is agnostic to that fact.

    Generally speaking, I keep all of my memory alignment and position
    dependent stuff like file headers and algorithmic data within a
    struct. I’m just curious to know whether or not this is intrinsic to
    classes as it is to structs and whether or not it will translate well
    into classes if I chose to use that approach.

    Memory alignment is something that’s almost automatically taken care of for you by the compiler. It’s more of an implementation detail than anything else. I say “almost automatically” since there are situations where it may matter (serialization, ABIs, etc) but within an application it shouldn’t be a concern.

    With respect with reading files (since you mention file headers), it sounds like you’re reading files directly into the memory occupied by a struct. I can’t recommend that approach since issues with padding and alignment may make your code work on one platform and not another. Instead you should read the raw bytes a couple at a time from the file and assign them into the structs with simple assignment.

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

Sidebar

Related Questions

I've noticed that on several web pages e.g. StackOverflow instead of stackoverflow.com/questions/ask.php they have
I have seen several similar questions to this, but none that addresses my specific
I have several questions: 1. What's the difference between isoMDS and cmdscale? 2. May
I have several questions regarding filenames and the iPod Library. I understand I can
I have several questions concerning the controls like a button, if You could answer
I have several questions about properties displayed in propertyGrid i cant seem to find
I have several questions I need help with. I'll add both my code and
I thought about migrating subversion a lot and I have several questions still open.
I have a program shown as follows. For it I have several questions: 1).
Need to use own imaged markers instead built-in pins. I have several questions. 1.

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.