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! 4 2166
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<TestN ode>, 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
> #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
Oh, I get it! It seems I've made another mistake---that when the struct
is instantiated, the size should be 12 not 8.
On 24 Aug 2005 17:52:58 -0700, "Metro12" <sa*******@yaho o.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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
Besides that one can pass reference to allocatior into every STL container. This raises the following questions:
1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other....
|
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 (4*20000+10000) bytes.
|
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 sample at hand is
concerned about threads, it could be related to widgets for the
purpose of this question (that's so I don't get flamed and told to go
to comp.programming.threads :)
|
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 a problem which corrected
my way of thinking.
Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h...
|
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 SRB_HaId;
| |
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 headers &
structure definition).
it's very handy to use "new SCRIPT_ITEM;" however I want to handle
OutOfMemory gracefully,
that is dealloc everything 1st and then throw a managed exception, I also
|
by: Daniel |
last post by:
I have the following three classes
class A
{
public:
virtual void f() = 0;
};
class B: public A
{
|
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 constructor and get me an Array<int32>
with a size 0? The memory used is the stack, right?
ia = Array<int32>(10);
|
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 has a nice article you can
read. Anyway, I discovered that if you make all members in the
implementation class mutable, you can in fact use this idiom without any
"unnecessary" memory allocation. Here's a minimal example of the method:
// In the...
|
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 <iostream>
#include <cstddef>
using namespace std;
class TraceHeap {
int i;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |