Hi all:
I have a question about memory allocation to an object of a class.
For example, I define the following class:
class example_class{
public:
example_class() ;
void funtion_1();
void function_2();
int variable_1;
double variable_2;
protected:
struct st {
int variable_3;
double variable_4;
} s1;
char *variable_5;
private:
int *variable_6;
}
Then create an object:
example_class *object_1 = new example_class;
My question is:
1. How much memory space will be allocated to object_1? Is the
following formula correct?
sizeof(variable _1)+sizeof(vari able_2)+sizeof( variable_3)+siz eof(variable_4) +sizeof(variabl e_5)+sizeof(var iable_6)
= sizeof(object_1 )
2. What is the entry address of object_1?
object_1 = &(variable_1 ), i.e., the entry address of object_1 is the
address of variable_1.
Or object_1 = &(s1) = &(variable_3 ), i.e., the entry address of
object_1 is the address of s1 or the address of variable_3.
Which is correct? Or both are wrong?
Thanks a lot.
Jack 4 2729
"C++fan" <jw****@excite. com> wrote in message
news:15******** *************** ***@posting.goo gle.com... Hi all:
I have a question about memory allocation to an object of a class. For example, I define the following class:
class example_class{
public: example_class() ; void funtion_1(); void function_2(); int variable_1; double variable_2;
protected: struct st { int variable_3; double variable_4; } s1;
char *variable_5;
private: int *variable_6;
}
Then create an object:
example_class *object_1 = new example_class;
My question is:
1. How much memory space will be allocated to object_1? Is the following formula correct?
sizeof(variable _1)+sizeof(vari able_2)+sizeof( variable_3)+siz eof(variable_4) +
sizeof(variable _5)+sizeof(vari able_6) = sizeof(object_1 )
It's not gauranteed.
Whether it is likely to be true depends largely on the alignment
requirements of your implementation.
For example - if your platform uses 4 byte ints then the size of the
following is almost certainly 8 rather than 5:
class X { char c; int i; }
This is because it will need to add 3 bytes of padding to get i aligned.
Sometimes size will depend on order of members e.g. struct X { char
c1,c2,c3,c4; int i; } will be 8 but
struct X { char c1; int i; char c2; } will be 12! Therefore you should
always put members in descending order of size.
Note that even reordering the first example to struct X { int i; char c; }
it will still have size 8 because otherwise you couldn't have an array of
them.
Strictly - the compiler is allowed a lot of leeway to rearrange the
members - I don't know the details off hand except that it can
definitely rearrange if you use any type of access decl
(public,private ,protected) and probably if it is anything other than POD.
NB If the class has virtual base classes or functions it will be at least
sizeof(void*) bigger than you would expect. 2. What is the entry address of object_1? object_1 = &(variable_1 ), i.e., the entry address of object_1 is the address of variable_1. Or object_1 = &(s1) = &(variable_3 ), i.e., the entry address of object_1 is the address of s1 or the address of variable_3. Which is correct? Or both are wrong?
It is definitely undefined in this case because of the
public,protecte d,private.
If you used a plain old struct without access decls it would be &variable_1
on any implementation that anyone would buy whether the standard gaurantees
it or not.
Thanks a lot.
Jack
"C++fan" <jw****@excite. com> wrote... I have a question about memory allocation to an object of a class. For example, I define the following class:
class example_class{
public: example_class() ; void funtion_1(); void function_2(); int variable_1; double variable_2;
protected: struct st { int variable_3; double variable_4; } s1;
char *variable_5;
private: int *variable_6;
}
Then create an object:
example_class *object_1 = new example_class;
My question is:
1. How much memory space will be allocated to object_1? Is the following formula correct?
sizeof(variable _1)+sizeof(vari able_2)+sizeof( variable_3)+siz eof(variable_4) +
sizeof(variable _5)+sizeof(vari able_6) = sizeof(object_1 )
Not necessarily. The object may have padding, the 's1' may have padding.
2. What is the entry address of object_1? object_1 = &(variable_1 ), i.e., the entry address of object_1 is the address of variable_1. Or object_1 = &(s1) = &(variable_3 ), i.e., the entry address of object_1 is the address of s1 or the address of variable_3. Which is correct? Or both are wrong?
Since the 'example_class' is not a POD, there is no guarantee that its
address coincides with the first member declared in it. Such address
coincidence is only guaranteed for PODs (see 9.2/17).
Victor
The result depends.
(1) Byte Allignment.
(2) There are virtual table if there are virtual function.
I think the entry address depends on the implementation of compiler.
"C++fan" <jw****@excite. com> wrote in message
news:15******** *************** ***@posting.goo gle.com... Hi all:
I have a question about memory allocation to an object of a class. For example, I define the following class:
class example_class{
public: example_class() ; void funtion_1(); void function_2(); int variable_1; double variable_2;
protected: struct st { int variable_3; double variable_4; } s1;
char *variable_5;
private: int *variable_6;
}
Then create an object:
example_class *object_1 = new example_class;
My question is:
1. How much memory space will be allocated to object_1? Is the following formula correct?
sizeof(variable _1)+sizeof(vari able_2)+sizeof( variable_3)+siz eof(variable_4) +
sizeof(variable _5)+sizeof(vari able_6) = sizeof(object_1 )
2. What is the entry address of object_1? object_1 = &(variable_1 ), i.e., the entry address of object_1 is the address of variable_1. Or object_1 = &(s1) = &(variable_3 ), i.e., the entry address of object_1 is the address of s1 or the address of variable_3. Which is correct? Or both are wrong?
Thanks a lot.
Jack This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John |
last post by:
Hi all:
When I run my code, I find that the memory that the code uses keeps
increasing.
I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
memory by the time it finishes execution. But I do not think it needs
so much memory. About 500M memory should be enough. I have following
questions about memory leak.
(1).If in my code I only define constructor for my class, and do not
define destructor, will it cause memory leak?
|
by: Jonathan Ames |
last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory
cleanup operations.
For example, let's say I have a class MovingObject which maintains a
pointer to another class MovementAlgorithm.
MovingObject has a method
SetMovementAlgorithm(MovementAlgorithm* movementAlgorithm)
if the body of this method reads
|
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: mosfets |
last post by:
Hi,
I'm having a little trouble figuring out the difference in terms of
memory allocation between:
class person_info;
class A {
private:
|
by: Gurikar |
last post by:
HI,
class A()
{
private:
int i;
char c;
public:
A();
| |
by: Rich S |
last post by:
Or just loosing it?
I have a small c++ app which generates millions combinations of bitset
class objects and adds them to a STL map.
(on a side thanks to all who responded to my earlier posting about
increasing the programs speed)
Anyway, I calculate my bitset class object size to be 8 bytes and when
I run this in debug mode I can generate 19,675,656 objects before an
|
by: Fred Zwarts |
last post by:
Hello,
I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.
However, I noticed that my replacing delete operator is not called
|
by: Hermann Maier |
last post by:
hi,
i need to find out the memory usage of a specific function that i use in
my program. this function does some recursive calculations and i want my
program to display the amount of memory the function used to calculate a
specific value.
thx
|
by: mangesh |
last post by:
This code is from c++ faq in section 11 :
void someCode()
{
char memory;
void* p = memory;
Fred* f = new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
object
}
|
by: shsingh |
last post by:
I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).
How to call a constructor explicitly if we want to allocate memory
using malloc ?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |