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

global objects

P: n/a
whats the best way to declare Global objects (holding say 10 member
variables and functions) of an unknown size in c++, memory efficient as
possible

thanks
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Your question is not clear.

You can declare a global object just like any global variable. The
important
point to note is that the constructor will be called before main is
called.

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation

Jul 23 '05 #2

P: n/a
john townsley wrote:
What's the best way to declare global objects
(holding say 10 member variables and functions)
of an unknown size in C++, memory efficient as possible


Declare them as constants:

class X {
// . . .
};

const X x;
Jul 23 '05 #3

P: n/a

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
whats the best way to declare Global objects (holding say 10 member
variables and functions) of an unknown size in c++, memory efficient as
possible

thanks


global objects of an a unknown size cant be defined as an array since we
dont know the size until run time

if I declare a pointer to object I cant again declare as an array becuase I
dont know the size

if I use a container to hold the object pointers I believe this is not the
standard way of using containers

If I declare as an instance to be in a container (as global) isnt this bad
programming as memory might run short.

I think this is a basic problem
eg Class A {
}

I want to declare an amount of Objects of Class A to be Global and to grow
in size an amount only known at runtime
Jul 23 '05 #4

P: n/a
john townsley wrote:
I want to declare an amount of Objects of Class A to be Global and to grow
in size an amount only known at runtime


std::vector does that. Many other container classes do that. This is
exactly what containers are for.

The only other way is to create a pointer to class A objects and use new
or malloc to keep track of the items yourself. But I figure the stdlib
writers probably got it optimized for most situations and have all the
bugs that I would introduce worked out...I just use the wheel that is
already there.

std::vector< A > global_a_array;

int main(void)
{
A a;
global_a_array.push_back(a);
...

return 0;
}

You don't even have to deal with size at all...unless you want to, then
you can give it size to start with, and make it grow when you think it
should.
Jul 23 '05 #5

P: n/a

"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:11*************@corp.supernews.com...
john townsley wrote:
I want to declare an amount of Objects of Class A to be Global and to
grow in size an amount only known at runtime


std::vector does that. Many other container classes do that. This is
exactly what containers are for.

The only other way is to create a pointer to class A objects and use new
or malloc to keep track of the items yourself. But I figure the stdlib
writers probably got it optimized for most situations and have all the
bugs that I would introduce worked out...I just use the wheel that is
already there.

std::vector< A > global_a_array;

int main(void)
{
A a;
global_a_array.push_back(a);
...

return 0;
}

You don't even have to deal with size at all...unless you want to, then
you can give it size to start with, and make it grow when you think it
should.


what about memoery considerations for this, isnt global variables of Object
instances risky
Jul 23 '05 #6

P: n/a
john townsley wrote:

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
whats the best way to declare Global objects (holding say 10 member
variables and functions) of an unknown size in c++, memory efficient as
possible

thanks

global objects of an a unknown size cant be defined as an array since we
dont know the size until run time


right

if I declare a pointer to object I cant again declare as an array becuase I
dont know the size
Just a moment. That sentence doesn't make sense. You declare a pointer.
At the moment the program knows how many, the program allocates an array
of that size and let the pointer point to it.

I think your sentence expresses a common misconception:
<Quote> An array and a pointer are the same thing. </Quote>
This is not true!
An array is *not* a pointer!

if I use a container to hold the object pointers I believe this is not the
standard way of using containers
Why not?
Indeed it is the standard way. Even if you need polymorphism (which requires
the container to hold pointers) it is much better to wrap that pointer up
in a class (a so called smart pointer) and let the container hold those
smart pointer objects.

If I declare as an instance to be in a container (as global) isnt this bad
programming as memory might run short.
Depends on your environment.
In an embedded system you might be right: memory constraints might force you
to accept some cludges. In todays desktop environments this is no longer
an issue. A typical PC has 512 MByte of memory and you really are scared of
using 4 or 8 bytes which aren't absolutely neccessary?

I think this is a basic problem

eg Class A {
}

I want to declare an amount of Objects of Class A to be Global and to grow
in size an amount only known at runtime


vector< A > Globals;

int main()
{
// somehow come up with the number of global
// objects needed
...

Globals.push_back( A_Object_1 );
Globals.push_back( A_Object_2 );
...

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7

P: n/a
john townsley wrote:

std::vector< A > global_a_array;

int main(void)
{
A a;
global_a_array.push_back(a);
...

return 0;
}

You don't even have to deal with size at all...unless you want to, then
you can give it size to start with, and make it grow when you think it
should.
what about memoery considerations for this,


In which environment are you programming?
isnt global variables of Object
instances risky


Global objects are always a problem, no matter how many
or how they are stored.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

P: n/a

john townsley wrote:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:11*************@corp.supernews.com...
john townsley wrote:
I want to declare an amount of Objects of Class A to be Global and to grow in size an amount only known at runtime
std::vector does that. Many other container classes do that. This is exactly what containers are for. std::vector< A > global_a_array; You don't even have to deal with size at all...unless you want to, then you can give it size to start with, and make it grow when you think it should.


what about memoery considerations for this, isnt global variables of

Object instances risky


What Karl said.

I will add that using std::vector is a lot safer that doing it by hand.
Really the only memory considerations I can think of is having some
set of functions growing the vector for no reason, or adding copies of
items that are already added by other functions. I think the real way
to deal with that kind of thing, if it is happening, is to use the
Singleton pattern and only allow it to manipulate an internal vector.

Really, depending on the actual needs of the OP, which were not
specific, there are many 'answers' to his problem. Why he is using
globals is something we don't know. It is very possible that a
different approach would be better.

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.