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

Variable across instances of class.

P: n/a
User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:817840
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:

int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space..Another practical thing to do could be to supply this as an argument in
functions that need it?I have presented a very simplified example above, but in cases where
there are bunch of such integers/doubles etc it becomes important.
thanks,
amit.

--
Aug 22 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Amit Bhatia wrote:
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:
Could you make A a template and the int value a template parameter?
int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space..Another practical thing to do could be to supply this as an argument in
functions that need it?I have presented a very simplified example above, but in cases where
there are bunch of such integers/doubles etc it becomes important.
One step on from the int template parameter, use a struct containing the
constants you require.

--
Ian Collins.
Aug 22 '06 #2

P: n/a
Amit Bhatia wrote:
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.
You can't save any memory by using a reference or pointer to shared
values: They use as much or more memory than the int. You can change
from int to a smaller type such as char. Depending on the presence of
other member variables and compiler padding, using char may or may not
save memory.

--
Scott McPhillips [VC++ MVP]

Aug 22 '06 #3

P: n/a
Amit Bhatia wrote:
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:
a should be a singleton object ... so that you will have only one real
object for all the instances.
Aug 22 '06 #4

P: n/a
Amit Bhatia schrieb:
As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:

int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space..Another practical thing to do could be to supply this as an argument in
functions that need it?I have presented a very simplified example above, but in cases where
there are bunch of such integers/doubles etc it becomes important.
If you have more than one integer, you could put the shared stuff in an
extra struct/class and let other objects contain a pointer or reference to
one of the shared struct objects, like this:

struct shared_data
{
int shared_a, shared_b, shared_c;

double shared_double;
};

class A
{
shared_data* data;
};

--
Thomas
Aug 22 '06 #5

P: n/a

"pi***@tin.it" <pi**********@tin.itwrote in message
news:44***********************@reader3.news.tin.it ...
Amit Bhatia wrote:
>hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:
a should be a singleton object ... so that you will have only one real
object for all the instances.
That might save memory if a was a user-defined object, but not for a single
int. Not even for multiple doubles and ints, unless they all took specific
values based on just one of them having a given value. (And even then, you
could save more memory by storing just that one value the others depend on,
and looking up or calculating the others at run-time.)

-Howard


Aug 22 '06 #6

P: n/a

"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:ec**********@newsreader2.netcologne.de...
Amit Bhatia schrieb:
>As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:

int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space..Another
practical thing to do could be to supply this as an argument in
functions that need it?I have presented a very simplified example above,
but in cases where
there are bunch of such integers/doubles etc it becomes important.

If you have more than one integer, you could put the shared stuff in an
extra struct/class and let other objects contain a pointer or reference to
one of the shared struct objects, like this:

struct shared_data
{
int shared_a, shared_b, shared_c;

double shared_double;
};

class A
{
shared_data* data;
};
This would only save memory if the values for shared_a, shared_b and
shared_c were the same whenever one of them was the same. Otherwise, you'd
have a potentially large number of shared_data objects where, for instance,
shared_a was the same, but shared_b and shared_c were different.

(And if shared_b and shared_c were the same for a given value of shared_a,
then you may not need to store them in memory at all. You might be able to
compute them at run-time.)

-Howard

Aug 22 '06 #7

P: n/a

"Amit Bhatia" <am****************@nospam.gmail.comwrote in message
news:ec**********@zinnia.noc.ucla.edu...
>
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5). As
a result I would like to save memory, and let a bunch of instantiations
share the same copy of "a" and a bunch of other ones another copy and so
on. So "static" won't help here. However, as the number of instances is
huge, this saving might be substantial.I can think of one way to do this:

int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space..Another
practical thing to do could be to supply this as an argument in
functions that need it?I have presented a very simplified example above,
but in cases where
there are bunch of such integers/doubles etc it becomes important.
thanks,
amit.
Passing the value as a parameter to various functions instead of storing it
should indeed save you memory, (provided that you aren't just pushing the
storage of the values to an equally large number of other locations). It
might cost you some small amount of time (to compute the value to pass) as
the trade-off for space, but only you could tell that (since it's your
code).

-Howard


Aug 22 '06 #8

P: n/a
Amit Bhatia posted:
>
hi,
I was wondering how to do the following. I have a class A,

class A{
//;
int a;
//;
};

I wish to instantiate this class, but across all instantiations of the
class, "a" will be given only one of the finitely many values(say 5).

class A {
int static a[5];
};

As a result I would like to save memory, and let a bunch of
instantiations share the same copy of "a" and a bunch of other ones
another copy and so on. So "static" won't help here. However, as the
number of instances is huge, this saving might be substantial.I can
think of one way to do this:

int &a;

But will this result really in saving any memory? because "a" is just an
integer, and saving just the address will also take some space.

You're correct -- a pointer might even take more space than an int. Seems
like you've got two choices:

1) Store an actual in within each object.

2) Keep an external record (a linked list perhaps) of the address of all
the current objects in existance. This will make each individual object
smaller, but it won't ultimately save memory because you'll have to
allocate memory for the list.

Another practical thing to do could be to supply this as an argument in
functions that need it?

Yes, but still you'll need to keep a record of which value corresponds to
which object -- and this will require memory.

--

Frederick Gotham
Aug 22 '06 #9

P: n/a
Frederick Gotham posted:
1) Store an actual in within each object.
TYPO: Store an actual int within each object.

--

Frederick Gotham
Aug 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.