473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(variable_2)+sizeof(varia ble_3)+sizeof(variable_4)+sizeof(variable_5)+sizeo f(variable_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 2715

"C++fan" <jw****@excite.com> wrote in message
news:15**************************@posting.google.c om...
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(variable_2)+sizeof(varia ble_3)+sizeof(variable_4)+
sizeof(variable_5)+sizeof(variable_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,protected,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(variable_2)+sizeof(varia ble_3)+sizeof(variable_4)+
sizeof(variable_5)+sizeof(variable_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.google.c om...
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(variable_2)+sizeof(varia ble_3)+sizeof(variable_4)+
sizeof(variable_5)+sizeof(variable_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***@blueyonder.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.learn.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
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...
10
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...
6
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
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
by: Gurikar | last post by:
HI, class A() { private: int i; char c; public: A();
16
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...
6
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...
4
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...
15
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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,...

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.