C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

global variables, memory and performance
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
axis
Guest





PostPosted: Mon Oct 13, 2003 4:26 am    Post subject: global variables, memory and performance Reply with quote



As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables? Are there
any limits that aren't there when allocating on the stack?

In my case, I am making a large openGL application, and am finding
that I must keep lots of data in the global scope because the callback
function that draws the window doesn't take any parameters. I haven't
run into problems up to this point, and I try to keep the memory use
at a minimum by allocating only when the data is necessary and
deallocating when it's not needed any more, but it would surprise me
to find out that Quake 2 held all it's map data in global memory (or
maybe it shouldn't surprise me if it's not that big of a deal).

Thanks!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Mon Oct 13, 2003 8:40 pm    Post subject: Re: global variables, memory and performance Reply with quote



Hi,

Quote:
As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables?

This very much depends on the architecture you're developing on. At
least, it is off-topic as far as C++ is concerned.

Memory issues: The memory required for the object is reserved before
main() is called, in a way that it is of no concern for C++. The
lifetime of the objects end as soon as the program ends, i.e. they
last *at least* until main() exits.

Performance issues: Depends on the platform. On the platforms I know
(i386 and co, Motorola 68K), it doesn't make a difference except possibly
for caching issues (i.e. placement of data such that it fits well into the
processor cache). Typically, the memory for these issues is part of the
program binary and gets allocated by the loader of the operating system.
Their constructors are called implicitly by the startup code of your binary,
and their destructors are called implicitly by the shut-down code.

Quote:
Are there
any limits that aren't there when allocating on the stack?

The life time of the objects are different, namely it is global. Whether
this is a "limit" depends on your application.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Tue Oct 14, 2003 4:12 pm    Post subject: Re: global variables, memory and performance Reply with quote



"axis" <google (AT) luisma (DOT) com> wrote in message

Quote:
In my case, I am making a large openGL application, and am finding
that I must keep lots of data in the global scope because the callback
function that draws the window doesn't take any parameters. I haven't
run into problems up to this point, and I try to keep the memory use
at a minimum by allocating only when the data is necessary and
deallocating when it's not needed any more, but it would surprise me
to find out that Quake 2 held all it's map data in global memory (or
maybe it shouldn't surprise me if it's not that big of a deal).

So you set some global variables, then call a function that takes no
arguments, and that function kind of takes arguments in the form of the
global variables, right? Will you program work correctly in a
multi-threaded environment?

Other than that, I can't think of any problem with the use of global
variables, but others may have better ideas.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michael Jørgensen
Guest





PostPosted: Tue Oct 14, 2003 4:30 pm    Post subject: Re: global variables, memory and performance Reply with quote

"axis" <google (AT) luisma (DOT) com> wrote

Quote:
In my case, I am making a large openGL application, and am finding
that I must keep lots of data in the global scope because the callback
function that draws the window doesn't take any parameters.

Just a suggestion. To avoid cluttering the global space, you could keep
all your global data in a singleton class, see below.

-Michael.

================================================

#include <iostream>

class Global
{
public:
Global()
{
a = 1;
}

static Global& GetGlobal()
{
static Global global;
return global;
}

int a;
};

int main()
{
std::cout << "a=" << Global::GetGlobal().a << std::endl;
exit(0);
}





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
spipyeah
Guest





PostPosted: Wed Oct 15, 2003 10:41 am    Post subject: Re: global variables, memory and performance Reply with quote

Quote:
As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables?

Yes. By using global variables, you have poor data locality. If these
variables are simply passed to function calls, it doesn't make any
difference I guess. But global variables can always be aliased. They
are not local to a function. Therefore, if you refer to them in a
function for multiple calculations, they will probably have to be
refetched to cache each time they are accessed. In other words, avoid
globals if you use them for intensive calculations.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
NJS
Guest





PostPosted: Wed Oct 15, 2003 10:42 am    Post subject: Re: global variables, memory and performance Reply with quote

Doesn't it make more sense to use namespaces for this? The syntax
would certainly be cleaner. I don't see any benefits to using a
singleton unless you are using a compiler that doesn't comform well to
the standard.

- Niek


Quote:
[snip]

Just a suggestion. To avoid cluttering the global space, you could keep
all your global data in a singleton class, see below.

-Michael.

[snip]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Wed Oct 15, 2003 10:46 am    Post subject: Re: global variables, memory and performance Reply with quote

"Thomas Richter" <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote


Quote:
Memory issues: The memory required for the object is reserved before
main() is called, in a way that it is of no concern for C++. The
lifetime of the objects end as soon as the program ends, i.e. they
last *at least* until main() exits.

The OP is using maps, vectors, and such which manage dynamic memory. The
sizeof vector includes just the start pointer, size, and capacity (a pointer
and two integers, or two pointers and one integer). Map is similar. The
memory actually used by the object is reserved at runtime.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Wed Oct 15, 2003 12:51 pm    Post subject: Re: global variables, memory and performance Reply with quote

axis wrote:
Quote:
As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables? Are there
any limits that aren't there when allocating on the stack?

The primiary issue to worry about is initialization/destruction order
dependencies. That is: correctness.

For performance, you may want to look at your program's memory access
patterns to see if you have locality-of-reference issues to worry
about (CPU cache, page faults), and whether using global variables
have any material effect on them. Here you have two dimensions to
analyze: call graphs and data access patterns, and their interactions.

As for limits: there are limits on static storage space, stack size,
and heap space, on most platforms. On embedded platforms (such as
PalmOS), these limits can be quite stringent. On general purpose
platforms, such as Win32/Linux/Unix, you usually get to use the
whole virtual address space. The stack is sometimes capped at
some static limit that can be modified with a linker option (Win32)
and/or operating environment parameters.

If your static storage exceeds some limit, usually the linker or
compiler will tell you; however, if your heap or stack exceeds the
limit, you'll have to discover that at runtime.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Oct 15, 2003 4:06 pm    Post subject: Re: global variables, memory and performance Reply with quote

In article <eaf86cc1.0310121317.229101c0 (AT) posting (DOT) google.com>, axis
wrote:
Quote:
As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables? Are there
any limits that aren't there when allocating on the stack?

In my case, I am making a large openGL application, and am finding
that I must keep lots of data in the global scope because the callback
function that draws the window doesn't take any parameters.
snip


I assume you're talking about GLUT?

Have a look at this C++ wrapper for it:
<http://www.duke.edu/~stetten/GlutMaster/GlutMaster.html>.

I don't see any licence terms on that, so you can't just go
copying that without asking. However, the key idea I saw in
it was to get an ID and use that as an index into an array of
pointers to an abstract class. There is apparently a
function called glutGetWindow() that returns an ID number for
the window currently being processed. So with the following
declarations:

class window {
public:
virtual /*result*/ foo(/*params*/);
virtual /*result*/ bar(/*params*/);
// ...
};
static const int MAX_WINDOWS = /* don't know */;
window * window_table[MAX_WINDOWS];

each of the callback functions can look like this:

/*result*/ foo(/*params*/)
{
return window_table[glutGetWindow()]->foo(/*params*/);
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Oct 16, 2003 2:02 pm    Post subject: Re: global variables, memory and performance Reply with quote

In article <882fe461.0310141405.4a498943 (AT) posting (DOT) google.com>, spipyeah
<spip_yeah (AT) yahoo (DOT) com> writes
Quote:
As I understand it, global variables are allocated in a memory area
outside of the stack and the heap. I know design-wise, global
variables should be avoided when possible. But are there any memory or
performance issues to consider when using global variables?

Yes. By using global variables, you have poor data locality. If these
variables are simply passed to function calls, it doesn't make any
difference I guess. But global variables can always be aliased. They
are not local to a function. Therefore, if you refer to them in a
function for multiple calculations, they will probably have to be
refetched to cache each time they are accessed. In other words, avoid
globals if you use them for intensive calculations.

Actually it is worse. Global variables are very bad news because of
aliasing. Any function that has a reference/pointer parameter which
could refer to a visible global variable and that uses global variable
in its definition must assume that aliasing is possible between the
(possibly dereferenced) parameter and the global variable. That
completely shoots a wide range of optimisation opportunities.


--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 16, 2003 2:37 pm    Post subject: Re: global variables, memory and performance Reply with quote

"spipyeah" <spip_yeah (AT) yahoo (DOT) com> wrote in message

Quote:
Yes. By using global variables, you have poor data locality. If these
variables are simply passed to function calls, it doesn't make any
difference I guess. But global variables can always be aliased. They
are not local to a function. Therefore, if you refer to them in a
function for multiple calculations, they will probably have to be
refetched to cache each time they are accessed. In other words, avoid
globals if you use them for intensive calculations.

But the OP is using vectors and maps. Even if you declare these as
local or
member variables and pass them by const reference, you still have
aliasing
problems. It's only when you pass by value that you can get caching
optimizations due to lack of aliasing. We're unlikely to pass vector by
reference because it would be expensive, and also because the vector has
a
pointer to an array of elements and even if we receive a vector by value
the
pointer means that data access to these elements can probably not be
cached
anyway.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Oct 16, 2003 2:40 pm    Post subject: Re: global variables, memory and performance Reply with quote

[email]njs8030 (AT) yahoo (DOT) com[/email] (NJS) wrote in message
news:<c57a219f.0310141437.4ec45ba0 (AT) posting (DOT) google.com>...

Quote:
[snip]

Just a suggestion. To avoid cluttering the global space, you could
keep all your global data in a singleton class, see below.

Doesn't it make more sense to use namespaces for this? The syntax
would certainly be cleaner. I don't see any benefits to using a
singleton unless you are using a compiler that doesn't comform well to
the standard.

Controlling order of initialization, perhaps. The one time I did
something like this was before namespaces, but even then, the motivation
wasn't to avoid namespace pollution, but to control the order of
initialization.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Thu Oct 16, 2003 8:59 pm    Post subject: Re: global variables, memory and performance Reply with quote

Hi,

spipyeah wrote:

Quote:
But global variables can always be aliased. They
are not local to a function. Therefore, if you refer to them in a
function for multiple calculations, they will probably have to be
refetched to cache each time they are accessed.

Why?

int i;
void fun()
{
// some intensive calculations using i
}

There is no reason to refetch "i" every time it is used, IF fun() does
not call other functions as part of computations. On the other hand, IF
fun() does call other functions, then yes - the compiler cannot assume
that the other function does not change the value of "i" and it should
ensure that all functions operate on a single value.

Note that if fun() does not call other functions during calculations,
then the only aliasing can probably result from having multiple threads.
Even then, the compiler should be thread-agnostic by its own, because
even with refetching it cannot guarantee proper access synchronization.

Quote:
In other words, avoid
globals if you use them for intensive calculations.

Depends on the structure of code used for calculations. If the code
operates only on global data, then there is no problem, because the
locality of reference can be as good as with local data.
Design issues aside, of course.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
spipyeah
Guest





PostPosted: Fri Oct 17, 2003 11:43 am    Post subject: Re: global variables, memory and performance Reply with quote

Quote:
Why?

int i;
void fun()
{
// some intensive calculations using i
}

There is no reason to refetch "i" every time it is used, IF fun() does
not call other functions as part of computations. On the other hand, IF
fun() does call other functions, then yes - the compiler cannot assume
that the other function does not change the value of "i" and it should
ensure that all functions operate on a single value.

Ok, now take:

int i;
void fun( int *a )
{
do
{
// some stuff

i = a * something;

} while( go_on );
// some intensive calculations involving both i and (*a)
// among which:

}

a can potentially alias i, so because of the assignement to i, a has
to be refetched each iteration.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
spipyeah
Guest





PostPosted: Sat Oct 18, 2003 10:08 am    Post subject: Re: global variables, memory and performance Reply with quote

Quote:
But the OP is using vectors and maps.

You're right. I lost the scope of the discussion and talked about
globals and aliasing in general.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.