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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:16:46+00:00 2026-06-12T20:16:46+00:00

What is the data structure of C++ class? how it works in assembly level?

  • 0

What is the data structure of C++ class?
how it works in assembly level?

IF statement is the compare + conditional jump of code line.

Array and string is the chain link of datas.

  • 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-12T20:16:50+00:00Added an answer on June 12, 2026 at 8:16 pm

    The compiler assigns offsets to all members, and includes these in all load/store operations on members:

    struct foo {
        uint32_t bar;
        uint32_t baz;
    
        uint32_t get_baz() { return baz; }
    };
    
    uint32_t get_baz_from_foo(foo *f) { return f->baz; }
    

    becomes (ARM assembler code used for simplicity):

    foo__get_baz:
        ; calling convention: this pointer in r3
        ; load 32 bit value from r3 + 4 bytes into r0
        ldr r0, [r3, #4];
        ; calling convention: return value in r0
        ; return from subroutine
        b lr
    
    get_baz_from_foo:
        ; calling convention: first parameter in r0
        ; load 32 bit value from r0 + 4 bytes into r0
        ldr r0, [r0, #4]
        ; calling convention: return value in r0
        ; return from subroutine
        b lr
    

    As struct respective class layout does not change after compilation, the 4 is hardcoded into the instruction stream here.

    Creating an instance works by allocating memory, and passing the pointer from the allocation function to everyone expecting a pointer to the struct:

    new__foo:
        ; two 32 bit integers need 8 bytes
        ; calling convention: first parameter in r0
        mov r0, #8
        ; call allocator, which will then return to the function invoking new
        bra malloc
    

    If there is a constructor

    struct foo2 {
        foo2() : bar(5), baz(7) { }
        uint32_t bar;
        uint32_t baz;
        uint32_t get_baz() { return baz; }
    };
    

    We end up with a slightly more complicated way to create objects (which you should be able to figure out without comments):

    new__foo2:
        strdb lr, ![sp]
        mov r0, #8
        bl malloc
        mov r1, #5
        str r1, [r0]
        mov r1, #7
        str r1, [r0, #4]
        ldaia lr, ![sp]
        b lr
    

    The get_baz implementation is the same as for the foo class.

    Now if I construct such an object and get the baz value:

        bl new__foo2
        ; remember: the this pointer goes to r3
        mov r3, r0
        bl foo2__get_baz
    

    I end up with r0 containing the value 7.

    For virtual methods, a hidden data member is created, which is a pointer to a table of functions:

    struct base {
        virtual uint32_t get_baz() = 0;
    };
    
    struct derived : base {
        derived() : baz(5) { }
        virtual uint32_t get_baz();
        uint32_t bar;
        uint32_t baz;
    };
    

    becomes

    new__derived:
        strdb lr, ![sp]
        mov r0, #12
        bl malloc
        mov r1, #5
        str r1, [r0, #8]
        ; get the address of the vtable
        ldr r1, =vtable__derived
        ; vtable typically goes to the end of the class defining it
        ; as this is the base class, it goes before derived's data members
        str r1, [r0]
        ldria lr, ![sp]
        b lr
    
    vtable__derived:
        ; pointer to function
        dw derived__get_baz
    
    derived__get_baz:
        ldr r0, [r3, #8]
        b lr
    

    Calling this function is done indirectly:

        ; construct normally
        bl new__derived
        ; here, we forget that this is a "derived" object
        ; this pointer to r3
        mov r3, r0
        ; get vtable ptr
        ldr r0, [r3]
        ; get function ptr from vtable
        ldr r0, [r0]
        ; call function
        bl r0
    

    Here, r0 is now 5, because that is what the constructor stored there.

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

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I've created a data structure like below public class LogList { public int _id;
I have following data structure (simplified): A giant list of the class TestClass public
I have a data structure which is as given below: class File { public
My goal is to parse a class and return a data-structure (object, dictionary, etc)
Say you have a simple class with some storage data structure (list, vector, queue,
for my data structures class, we are making a data structure that we can
Consider the following data structure: List<Person> People; class Person { List<Car> Cars; List<Hobby> Hobbies;
I am trying to build a BehaviourTree data structure. The main class is BTNode,
I always think of xml like a set data structure. Ie: <class> <person>john</person> <person>sarah</person>

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.