473,397 Members | 2,077 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,397 software developers and data experts.

Question about memory allocation

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
6 1811
* 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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
8
by: jason | last post by:
i'm a little new to VC++, so i'm curious how to appropriate perform the following task. there is a pointer which i wish you point to a buffer of frequently changing size. i'm wondering what is...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
16
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 ...
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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,...
0
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...

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.