Variable across instances of class.

A

Amit Bhatia

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.

--
 
I

Ian Collins

Amit said:
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.
 
S

Scott McPhillips [MVP]

Amit said:
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.
 
P

pibru

Amit said:
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.
 
T

Thomas J. Gritzan

Amit said:
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;
};
 
H

Howard

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
 
H

Howard

Thomas J. Gritzan said:
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
 
H

Howard

Amit Bhatia said:
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
 
F

Frederick Gotham

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top