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

global objects

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
8 8386
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
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

"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
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

"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
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
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

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

Similar topics

1
by: Oystein Haare | last post by:
I'm thinking about sort of a factory-system where the factories get instanciated on program start (through a global object in the .cpp file), and registers themselves with a singelton. Is it a...
1
by: Oystein Haare | last post by:
Note: This might be a bit off topic.. I want to create some global objects that register themselves with another "manager"-class upon creation: class SomeClass { ... }; // cpp:
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
10
by: parez | last post by:
Hi ALL! I am feeling kinda lost here.How do i make global objects available to a class. e.g i want to make some objects available like the httpcontext object available if you run a...
6
by: MechSoft | last post by:
Hi, I am new to C++ from C, I am a bit confused about using global objects in C++. I have a program that need to share some data(held in classes) between files, and of course I thought about using...
0
by: MechSoft | last post by:
Hi, there, I have an issue about globally access data. My multi-threaded application has some resources that need to be accessed by multiple threads and have the lifetime of the application,...
5
by: Chris Hughes | last post by:
I have an environment with many thousands of client machines uploading data files several times each day to a web server via HTTP PUT. To avoid disk I/O (for performance), I am implementing a...
4
by: Shadowboxer | last post by:
What happened to global objects? I want to make the main form accessible throughout all the classes as a single instance, right now I have to make a cludgy pass back through the class structure...
2
by: Ben Collver | last post by:
What is the JavaScript equivalent of the forth words word, or the TCL command? I am using OSSP SpiderMonkey as my JavaScript host and I would like to list the global objects such as Number,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.