473,507 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about the memory allocation of class members

Beyond my expectations, the result of the following program is 1
not 8. I want to know how the complier allocates memory for such a
class, and why the class Test's size does not contain the struct
TestNode's.

#include <iostream>
using namespace std;

class Test
{
public:
Test(){}
~Test() {}
private:
struct TestNode
{
int num;
char ch;
};
char ch1;
};

int main(void)
{
//the output is 1, not 8.Why?
cout << "size of Test: " << sizeof(Test) << endl;

return 0;
}

Thanks!

Aug 24 '05 #1
4 2155

Metro12 schreef:
Beyond my expectations, the result of the following program is 1
not 8. I want to know how the complier allocates memory for such a
class, and why the class Test's size does not contain the struct
TestNode's.
Because you didn't declare a Test member with type TestNode (nor
TestNode*, nor std::list<TestNode>, nor... ). The compiler has no
idea how you want to use TestNode.
#include <iostream>
using namespace std;

class Test
{
public:
Test(){}
~Test() {}
private:
struct TestNode
{
int num;
char ch;
};
TestNode node; // My guess, not the compiler's guess though.
char ch1;
};


HTH,
Michiel Salters

Aug 24 '05 #2
> #include <iostream>
using namespace std;

class Test
{
public:
Test(){}
~Test() {}
private:
struct TestNode
{
int num;
char ch;
};
char ch1;

};

int main(void)
{
//the output is 1, not 8.Why?
cout << "size of Test: " << sizeof(Test) << endl;

return 0;
}


struct TestNode is a nested struct. You have defined it but have not
*instantiated* it. So, there's no member object of type 'TestNode'
within the class 'Test'. The only member that Test has is the char
member whose size is what is the size of your class.

Srini

Aug 24 '05 #3
Oh, I get it! It seems I've made another mistake---that when the struct
is instantiated, the size should be 12 not 8.

Aug 25 '05 #4
On 24 Aug 2005 17:52:58 -0700, "Metro12" <sa*******@yahoo.com.cn>
wrote in comp.lang.c++:
Oh, I get it! It seems I've made another mistake---that when the struct
is instantiated, the size should be 12 not 8.


There is no 'should be', there is just 'is'. The implementation is
allowed wide latitude in using padding to align types on specific
boundaries. In some cases it is as simple as the fact that misaligned
values slow the program due to additional memory accesses. In other
cases, particularly with many RISC designs, the processor hardware
will generate a fault if memory accesses are misaligned.

Another compiler might well produce a different size. Even certain
options in the same compiler could do so as well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
1682
by: regisser | last post by:
I have a quastion for the C++ professionals and members of the C++ standartization commetee. As i know C++ standart requires an allocator to be a templated parameter in every STL container....
6
1814
by: John | last post by:
Following is a simple class: class myclass{ public: int AA; char BB; } The size of an object of myclass is 4*20000+10000. That is to say, an object of myclass occupies the memory of...
2
1455
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
5
1675
by: Lionel | last post by:
Hi all, Just wondering exactly how memory is allocated using the new operator? More specifically I am interested in how the memory is calculated for globable variables? I recently stumbled into...
16
2469
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
10
317
by: Lloyd Dupont | last post by:
how do I redefine the new operator? for all the structure I use at once! (some of them comes from C include files). The rationale: I'm writting a managed C++ wrapper around C API (external...
20
1979
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
9
2531
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
14
3144
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
15
1849
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.