Quick question about C++ classes...

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

Quick question about C++ classes...

Postby Slater » Fri Jul 11, 2008 7:52 pm

I just got through a semester of assembly programming: learning about what goes on inside of the computer when a C-program is compiled and run. It was pretty fun and made a lot of sense to me.

Being bored, I decided to take a look at the next logical link in the chain: how classes in C++ are represented in memory. Here's what came out:
Code: Select all
libra% cat classTest.cpp
#include <stdio.h>
using namespace std;

/*******************************************
** Class SomeClass                        **
** Description:                           **
**      A simple class that has 3 fields: **
**              1. integer array (5 ints) **
**              2. single integer         **
**              3. single character       **
**      Constructor initalizes all fields **
*******************************************/
class SomeClass {
        public:
                SomeClass();
                int  a[5];
                int b;
                char c;
};

// Constructor for SomeClass
SomeClass::SomeClass() {
                a[0] = 1;
                a[1] = 4;
                a[2] = 16;
                a[3] = 64;
                a[4] = 0x00ff00ff;
                b = 42;
                c = 'q';
}

int main() {
        int sizeOfSomeClass = sizeof(SomeClass);// # of bytes in memory an
                                                // instance of SomeClass takes

        SomeClass * SC = new SomeClass();       // Create a new instance of
                                                // SomeClass and point to it
                                                // with SC

        // info on SC
        printf( "Size of SomeClass SC in memory: %i bytes.\n", sizeOfSomeClass);
        printf( "Location of class in memory: %x \n\n", (int)SC);

        // Printing memory contents for the SomeClass instance.
        // This loop prints out the memory location and contents for each byte
        // that the class takes up.
        for (int i = 0; i < sizeOfSomeClass; i++) {
                char memContent = *((char*)SC + i);
                int value = (int)memContent;
                value = value & 0x0ff;
                printf ("Address 0x%x : 0x%x \n", ((int)SC + i), value);
        }
        return 0;
}
libra% g++ classTest.cpp -o classTest.exe
libra% chmod 550 classTest.exe
libra% classTest.exe
Size of SomeClass SC in memory: 28 bytes.
Location of class in memory: 27680

Address 0x27680 : 0x0
Address 0x27681 : 0x0
Address 0x27682 : 0x0
Address 0x27683 : 0x1
Address 0x27684 : 0x0
Address 0x27685 : 0x0
Address 0x27686 : 0x0
Address 0x27687 : 0x4
Address 0x27688 : 0x0
Address 0x27689 : 0x0
Address 0x2768a : 0x0
Address 0x2768b : 0x10
Address 0x2768c : 0x0
Address 0x2768d : 0x0
Address 0x2768e : 0x0
Address 0x2768f : 0x40
Address 0x27690 : 0x0
Address 0x27691 : 0xff
Address 0x27692 : 0x0
Address 0x27693 : 0xff
Address 0x27694 : 0x0
Address 0x27695 : 0x0
Address 0x27696 : 0x0
Address 0x27697 : 0x2a
Address 0x27698 : 0x71
Address 0x27699 : 0x0
Address 0x2769a : 0x0
Address 0x2769b : 0x0
libra%


Nothing too surprising... until the end. Everything looks cool up until the last 3 bytes, which are simply 0s.

Why is this? Now, if I change "char c" into "char c[4]" and init the char-sting to "qabc" (no null char, just for simplicity), the memory looks something like this:
Address 0x27698 : 0x71
Address 0x27699 : 0x61
Address 0x2769a : 0x62
Address 0x2769b : 0x63
[end of class]

That makes perfect sense; the size of each char is one byte here. But why add in the trailing 0s in the class data when there's only one character? Does this have some deep significance inside the computer, or is it my compiler just trying to keep all the data in memory aligned in a particular way?
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby blkmage » Fri Jul 11, 2008 9:33 pm

I'm going to guess that it's because the size of a word is four bytes.
User avatar
blkmage
 
Posts: 4529
Joined: Mon May 03, 2004 5:40 pm

Postby Slater » Sat Jul 12, 2008 1:02 am

I see... so it sorta is a memory alignment thing.

But does that mean that every time I declare a char all by itself that the program will automatically allocate at least 4 bytes for it?
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Warrior4Christ » Sat Jul 12, 2008 9:41 pm

Slater (post: 1244192) wrote:I see... so it sorta is a memory alignment thing.

But does that mean that every time I declare a char all by itself that the program will automatically allocate at least 4 bytes for it?

Potentially. But it might not.
If you have:
char a;
char b;
..then you'd only need two bytes of padding, as it would probably put the two chars together. Likewise, only one byte of padding with three chars.
Everywhere like such as, and MOES.

"Expect great things from God; attempt great things for God." - William Carey
User avatar
Warrior4Christ
 
Posts: 2045
Joined: Sat Aug 20, 2005 8:10 pm
Location: Carefully place an additional prawn on the barbecue

Postby blkmage » Sat Jul 12, 2008 9:48 pm

Yeah, I think that it rounds it out at the end, but not for every single char. Otherwise, it wouldn't make sense to use short ints for anything since they'd just end up taking up four bytes anyway. Of course, we could just play around with that class you wrote and find out.
User avatar
blkmage
 
Posts: 4529
Joined: Mon May 03, 2004 5:40 pm


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 213 guests