Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Order of allocation of global and static variables

3 views
Skip to first unread message

Vikram

unread,
Aug 4, 2004, 2:08:28 AM8/4/04
to
Suppose in a source file global objects/variables are defined in the
following order

....a.cpp....

#include<iostream>

ClassA aObj;
char chArray[10];
ClassB bObj;
static int bInt;

func();

My question is in which order the memory is allocated to the global
objects on the heap space. Can we expect the memory of all the object
aObj,chArray, bObj and bInt to be in a sequence in memory. Or its a
compiler dependent? Does compiler optimization can affect the order in
which memory is allocated ?

TIA
-Vikram

aha...@gmail.com

unread,
Aug 4, 2004, 2:57:34 AM8/4/04
to
Ok. So this is not a reply to your question. Rather, I have a question
for you. Why should you care where the objects are allocated? The code
that you write shouldn't assume anything about the address of the
objects in memory.

Vikram

unread,
Aug 4, 2004, 3:04:26 AM8/4/04
to
Actually currently i am debugging a problem where the one particular
memory location gets corrupted. To localize the problem i want to know
what all variables and objects are there. As this problem is not easily
reproducible... and once it occurs the normal processing of the
application is affected and we need to restart the app back to resume
normal operations.

aha...@gmail.com

unread,
Aug 4, 2004, 3:13:11 AM8/4/04
to
How do you know that memory location is getting corrupted? Why don't
you use a debugger to "watch" that particular memory location. You
would then be able to pin point the exact instruction that is causing
the memory location to be corrupted.

Vikram

unread,
Aug 4, 2004, 4:33:41 AM8/4/04
to
Logs are showing that variable values have changed ?

j...@invalid.address

unread,
Aug 4, 2004, 9:49:36 AM8/4/04
to
"Vikram" <vsac...@gmail.com> writes:

They're not allocated on the heap, they're in the program's data
segment. They're initialized in the same order they're given in the
code.

Joe
--
"Surprise me"
- Yogi Berra when asked where he wanted to be buried.

Pascal Bourguignon

unread,
Aug 4, 2004, 10:13:48 AM8/4/04
to
j...@invalid.address writes:

> "Vikram" <vsac...@gmail.com> writes:
> > My question is in which order the memory is allocated to the global
> > objects on the heap space. Can we expect the memory of all the
> > object aObj,chArray, bObj and bInt to be in a sequence in memory. Or
> > its a compiler dependent? Does compiler optimization can affect the
> > order in which memory is allocated ?
>
> They're not allocated on the heap, they're in the program's data
> segment. They're initialized in the same order they're given in the
> code.

AFAIK, they're initialized in the order the compiler chooses to
initialize them.

--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.

j...@invalid.address

unread,
Aug 4, 2004, 10:53:39 AM8/4/04
to
Pascal Bourguignon <sp...@thalassa.informatimago.com> writes:

> j...@invalid.address writes:
> > "Vikram" <vsac...@gmail.com> writes:
> > > My question is in which order the memory is allocated to the global
> > > objects on the heap space. Can we expect the memory of all the
> > > object aObj,chArray, bObj and bInt to be in a sequence in memory. Or
> > > its a compiler dependent? Does compiler optimization can affect the
> > > order in which memory is allocated ?
> >
> > They're not allocated on the heap, they're in the program's data
> > segment. They're initialized in the same order they're given in
> > the code.
>
> AFAIK, they're initialized in the order the compiler chooses to
> initialize them.

I think that's true for C, but the example was in C++, which defines
the order.

Barry Margolin

unread,
Aug 4, 2004, 8:21:02 PM8/4/04
to

It defines the order in which their initializers are run, but I really
doubt that the language says anything about the order of their memory
addresses.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Barry Margolin

unread,
Aug 4, 2004, 8:23:11 PM8/4/04
to
In article <ceq715$v...@odah37.prod.google.com>,
"Vikram" <vsac...@gmail.com> wrote:

To Vikram and Ahalom -- why aren't your messages properly linked into
the thread that was started? Doesn't Google have a proper
reply/followup operation?

aha...@gmail.com

unread,
Aug 5, 2004, 2:05:36 AM8/5/04
to
Hi Barry,

I know that Vikram was using Google Groups 2
(http://groups-beta.google.com), the most buggy website on the WWW
right now. I am not surprised that even the most things don't work.

Adnan

PS - I am using Google Groups 2 too.

0 new messages