Alias for a std::vector that does not take space

P

Peter Olcott

I got the previous alias to std::vector working, and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this? (I tried #define Name1
Name2 and it didn't compile)
 
P

peter koch

Peter said:
I got the previous alias to std::vector working, and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this? (I tried #define Name1
Name2 and it didn't compile)
No there is not. Perhaps you should try to explain your problem
instead.

/Peter
 
R

Ron Natalie

Peter said:
I got the previous alias to std::vector working, and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this? (I tried #define Name1
Name2 and it didn't compile)
The #define should have worked... but it's a blunt hammer:


#define Fred George

struct S {
vector<int> George;
} ;

S s;

s.Fred.push_back(2);
 
M

Michiel.Salters

Peter said:
I got the previous alias to std::vector working, and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this?

Yes: use a method. E.g.

struct {
std::vector<int> m_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<int> const& alias() const { return m_vec; }
};

HTH,
Michiel Salters
 
P

Peter Olcott

Yes: use a method. E.g.

struct {
std::vector<int> m_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<int> const& alias() const { return m_vec; }
};

HTH,
Michiel Salters

That would take up the space of two more std::vectors. I want something that has
the zero addtional run-time overhead of #define Charlie Fred

The problem with this is that it does not compile.
 
P

Peter Olcott

peter koch said:
No there is not. Perhaps you should try to explain your problem
instead.

/Peter

I am using the std::vector for two different purposes. I want to refer to it
using a meaningful name for each purpose. It must hold unsigned int in both
cases, yet, the data represents two different kinds of things. I want to refer
to this date by the kind of thing that it refers to. I could simply pick one of
these names, but, that makes for code that is less self-documenting.
 
P

peter koch

Peter said:
That would take up the space of two more std::vectors. I want something that has
the zero addtional run-time overhead of #define Charlie Fred

It does not take up any space: there is only one vector in the struct.
Also, I do not see why there should be any run-time overhead at all.
From your response to me (other place in this thread), I gather that
you have an object where you at one time use on vector and at another
time you use another vector of the same type. The two vectors are not
used at the same time and thus would like to reuse the space occupied.
To me this smells of a design mistake.

/Peter
Why
 
K

Kai-Uwe Bux

Peter said:
I am using the std::vector for two different purposes. I want to refer to
it using a meaningful name for each purpose. It must hold unsigned int in
both cases, yet, the data represents two different kinds of things.

Huh, as of now, I thought you wanted to refer to the same *object* of type
std::vector<unsigne int> by two different names. Now you talk about two
cases and data representing different things. Maybe you just want to use
the *type* std::vector< unsigned int > twice. If so, just use typedefs:

typedef std::vector< unsigned int > first_vector_kind;
I want
to refer to this date by the kind of thing that it refers to. I could
simply pick one of these names, but, that makes for code that is less
self-documenting.

Using two names for the same *object* would rather be confusing.


Best

Kai-Uwe Bux
 
P

Philip Potter

Peter Olcott said:
That would take up the space of two more std::vectors.

No. That struct has exactly one data member: m_vec. It is of type
std::vector<int>. The next two lines define functions (not data members)
which return references to m_vec.

In addition, why can't you spare "the space of a pointer" used by the
previous solution? Is that pointer *really* pushing your program over the
edge from being prudent with memory to being a complete hog?

Get your code correct first. Only then, if it isn't fast enough or if it
takes up too much space, should you consider such bean-counting
optimizations.
 
T

Thomas Tutone

Peter said:
I got the previous alias to std::vector working,

What previous alias? Who are you talking to?
and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this? (I tried #define Name1
Name2 and it didn't compile)


C++ uses references as aliases.

#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<int> v;
vector<int>& a;
a.push_back(1);
assert(&v==&a && v.size()==a.size());
}

In the above example, a is an alias for v. For most compilers, it
takes up no space and should have no run-time overhead.

If this isn't what you want, you're going to need to be more clear what
it is you're looking for.

Best regards,

Tom
 
T

Thomas Tutone

Thomas said:
What previous alias? Who are you talking to?



C++ uses references as aliases.

#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<int> v;
vector<int>& a;

The above line should be:

vector<int>& a = v;

Sorry about that.


Best regards,

Tom
 
H

Howard

Peter Olcott said:
I am using the std::vector for two different purposes. I want to refer to
it using a meaningful name for each purpose. It must hold unsigned int in
both cases, yet, the data represents two different kinds of things. I want
to refer to this date by the kind of thing that it refers to. I could
simply pick one of these names, but, that makes for code that is less
self-documenting.

There seems to be some confusion here. Do you want two names for one
vector, or two vectors, with only one of them "taking up space" at one time?
If it's the latter, then how about using two pointers (std::vector*), and
dynamically allocating the vector you want to use? Or, if for some totally
weird reason you can't afford one extra pointer, then use one pointer, and
then use the reference or method technique to get an alias to that one
pointer. Why on earth you need to do something like that is beyond me,
however. Why is space so critical that you can't afford one extra pointer
or reference?

-Howard
 
P

Peter Olcott

peter koch said:
It does not take up any space: there is only one vector in the struct.
Also, I do not see why there should be any run-time overhead at all.

you have an object where you at one time use on vector and at another
time you use another vector of the same type. The two vectors are not
used at the same time and thus would like to reuse the space occupied.
To me this smells of a design mistake.

No. I am using the same vector in the same class for the same purpose, in each
case it holds the same underlying data. There are two different meanings for
this data that I want to make self-evident by the self documentation of using
two different names.
 
P

Peter Olcott

Kai-Uwe Bux said:
Huh, as of now, I thought you wanted to refer to the same *object* of type
std::vector<unsigne int> by two different names. Now you talk about two
cases and data representing different things. Maybe you just want to use
the *type* std::vector< unsigned int > twice. If so, just use typedefs:

typedef std::vector< unsigned int > first_vector_kind;


Using two names for the same *object* would rather be confusing.

My whole purpose is to provide better self-documentation to make it less
confusing. In the case at had being able to refer to the same std::vector by two
different names is the best way.
 
P

Peter Olcott

Philip Potter said:
No. That struct has exactly one data member: m_vec. It is of type
std::vector<int>. The next two lines define functions (not data members)
which return references to m_vec.

In addition, why can't you spare "the space of a pointer" used by the
previous solution? Is that pointer *really* pushing your program over the
edge from being prudent with memory to being a complete hog?

Get your code correct first. Only then, if it isn't fast enough or if it
takes up too much space, should you consider such bean-counting
optimizations.
My code has been correct for more than a year. Now I am optimizing it.
 
P

Peter Olcott

Thomas Tutone said:
What previous alias? Who are you talking to?



C++ uses references as aliases.

But sometimes implements them as pointers that take up space.
#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<int> v;
vector<int>& a;
a.push_back(1);
assert(&v==&a && v.size()==a.size());
}

In the above example, a is an alias for v. For most compilers, it
takes up no space and should have no run-time overhead.

I ran a sizeof() of my compiler, and it takes the space of a pointer.
 
P

Peter Olcott

Howard said:
There seems to be some confusion here. Do you want two names for one vector,
or two vectors, with only one of them "taking up space" at one time?

Either way works.
If it's the latter, then how about using two pointers (std::vector*), and
dynamically allocating the vector you want to use? Or, if for some totally

I want to get away from this extra four bytes. The pointer takes four more
bytes.
weird reason you can't afford one extra pointer, then use one pointer, and

One extra pointer * 500,000,000
 
H

Howard

Peter Olcott said:
Either way works.


I want to get away from this extra four bytes. The pointer takes four more
bytes.


One extra pointer * 500,000,000

You have 500 million instances of your class? Geez, Louise!! I think I'd
just leave it alone and deal with the one generic name, then.

-Howard
 
P

Peter Olcott

Howard said:
You have 500 million instances of your class? Geez, Louise!! I think I'd
just leave it alone and deal with the one generic name, then.

-Howard
Unless someone comes up with a better way, that is what I will do.
 
T

Thomas Tutone

Peter said:
But sometimes implements them as pointers that take up space.

True. But in the example I gave, most compilers would optimize it
away.
I ran a sizeof() of my compiler, and it takes the space of a pointer.

You've jumped to an erroneous conclusion. When sizeof is applied to a
reference, it returns the size of the aliased object. Apparently, on
your implementation sizeof(std:vector<int>) is the same as
sizeof(void*). That's just coincidence. Try the following:

#include <iostream>

struct Foo {
double x;
long y;
void* z;
};

int main()
{
using std::cout; using std::endl;
Foo foo;
Foo& bar = foo;
cout << "sizeof foo: " << sizeof(foo) << endl;
cout << "sizeof bar: " << sizeof(bar) << endl;
cout << "sizeof void* " << sizeof(void*) << endl;
}

Please run this program on your system, then report back on the
results. Do you still believe that sizeof a reference must be the same
as sizeof a pointer?

Best regards,

Tom
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top