473,799 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory allocated to an object of a class

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
Jul 22 '05 #1
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

Jul 22 '05 #2
"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
Jul 22 '05 #3

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

Jul 22 '05 #4
On Tue, 6 Jan 2004 06:50:11 -0000, "Nick Hounsome"
<nh***@blueyond er.co.uk> wrote in comp.lang.c++:

[snip]
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.


The standard does guarantee this for POD structures.

--
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
Jul 22 '05 #5

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

Similar topics

32
3866
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?
10
1942
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
6
1839
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.
2
2653
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:
4
4342
by: Gurikar | last post by:
HI, class A() { private: int i; char c; public: A();
16
3062
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
6
2702
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
4
3433
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
15
4666
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 }
13
9730
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 ?
0
10485
Oralloy
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...
0
10252
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 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...
1
10231
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,...
0
10027
tracyyun
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...
0
9073
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, 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...
1
7565
isladogs
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...
0
6805
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();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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

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.