By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Question about memory allocation

P: n/a
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
}

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.
My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?

Can I say that for any class, the memory allocated to all its data
members is adjacent to each other?

Thanks.

John
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* John:
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
Don't use all uppercase names for anything but macros.
}
Need a semicolon here.

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.
With a given compiler and compiler options on a given system.

My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?
That depends on the compiler and compiler options (memory alignment,
mostly).

Can I say that for any class, the memory allocated to all its data
members is adjacent to each other?


No.

To make access of following items efficient a single char may for
example be padded to occupy four bytes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
On 21 Nov 2004 08:50:45 -0800, jo*********@yahoo.com (John) wrote in
comp.lang.c++:
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
}

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.
My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?
What exactly do you mean by adjacent? There are several ways the term
could be applied.

If you mean is the address of BB[0] exactly equal to the (invalid)
address of AA[20000]? In that case, the answer is that it is up to
your compiler, and perhaps options that you specify to the compiler.
The language allows the implementation to place padding between any
two members of a structure or class, or after the last member. Any
place, in fact, except before the first member.
Can I say that for any class, the memory allocated to all its data
members is adjacent to each other?
No you can't, especially not when your structure or class includes
access specifiers.

C++ guarantees that a POD data structure (one that could be compiled
as a struct in a C program) has all its members arranged in memory in
the order of their appearance in the definition of the type. No such
guarantee exists once you add access specifiers to the class or
struct.
Thanks.

John


--
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 #3

P: n/a
John wrote:
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
}

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.
My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?

It will be close...There is allowed to be some padding between.
One thing for sure, since there is no access specifier separating
AA and BB, then in a given object AA will be at a "lower" value
than BB.

myclass c;

if( static_cast<void*>(&a.AA) < static_cast<void*>(&b.BB)) {
// guaranteed to be true!
Jul 22 '05 #4

P: n/a
Jack Klein <ja*******@spamcop.net> wrote in message news:<g7********************************@4ax.com>. ..
On 21 Nov 2004 08:50:45 -0800, jo*********@yahoo.com (John) wrote in
comp.lang.c++:
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
}

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.
My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?


What exactly do you mean by adjacent? There are several ways the term
could be applied.

If you mean is the address of BB[0] exactly equal to the (invalid)
address of AA[20000]? In that case, the answer is that it is up to
your compiler, and perhaps options that you specify to the compiler.
The language allows the implementation to place padding between any
two members of a structure or class, or after the last member. Any
place, in fact, except before the first member.


Could you show me a compiling option with g++ compiler?

Thanks.

John
Jul 22 '05 #5

P: n/a
John wrote in news:c3**************************@posting.google.c om in
comp.lang.c++:
If you mean is the address of BB[0] exactly equal to the (invalid)
address of AA[20000]? In that case, the answer is that it is up to
your compiler, and perhaps options that you specify to the compiler.
The language allows the implementation to place padding between any
two members of a structure or class, or after the last member. Any
place, in fact, except before the first member.


Could you show me a compiling option with g++ compiler?


You are unlikely to get an answer here, our expertise is
in Standard C++, for gcc help goto:

news:gnu.gcc.help

or

news:gnu.g++.help

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

P: n/a
jo*********@yahoo.com (John) wrote in message news:<c3*************************@posting.google.c om>...
Following is a simple class:

class myclass{
public:
int AA[20000];
char BB[10000];
}

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.
My question is:
Is the memory allocated to AA adjacent to the memory allocated to BB?

Can I say that for any class, the memory allocated to all its data
members is adjacent to each other?

Thanks.

John


an int is not always 4 bytes, though it often is and might be on your
platform. There could be padding in between AA and BB, however, since
AA
is an int and ends on a word boundry and BB is a char, BB probably i
right after AA. But this is all up to your complier
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.