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

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

P: n/a
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)
Aug 16 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a

Peter Olcott wrote:
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

Aug 16 '06 #2

P: n/a
Peter Olcott wrote:
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<intGeorge;
} ;

S s;

s.Fred.push_back(2);
Aug 16 '06 #3

P: n/a

Peter Olcott wrote:
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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& alias() const { return m_vec; }
};

HTH,
Michiel Salters

Aug 16 '06 #4

P: n/a

<Mi*************@tomtom.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
Peter Olcott wrote:
>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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& 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.
Aug 16 '06 #5

P: n/a

"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
Peter Olcott wrote:
>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
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.
Aug 16 '06 #6

P: n/a

Peter Olcott wrote:
<Mi*************@tomtom.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

Peter Olcott wrote:
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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& 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
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
>
The problem with this is that it does not compile.
Aug 16 '06 #7

P: n/a
Peter Olcott wrote:
>
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>>
Peter Olcott wrote:
>>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

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 intby 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;
typedef std::vector< unsigned int second_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
Aug 16 '06 #8

P: n/a
Peter Olcott wrote:
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<intv;
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

Aug 16 '06 #9

P: n/a
"Peter Olcott" <ol****@att.netwrote in message
news:IVGEg.815$Tl4.453@dukeread06...
>
<Mi*************@tomtom.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

Peter Olcott wrote:
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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& alias() const { return m_vec; }
};

HTH,
Michiel Salters

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.

--
Philip Potter

Aug 16 '06 #10

P: n/a

Thomas Tutone wrote:
Peter Olcott wrote:
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<intv;
vector<int>& a;
The above line should be:

vector<int>& a = v;

Sorry about that.
Best regards,

Tom

Aug 16 '06 #11

P: n/a

"Peter Olcott" <ol****@att.netwrote in message
news:AYGEg.817$Tl4.758@dukeread06...
>
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>>
Peter Olcott wrote:
>>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

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

Aug 16 '06 #12

P: n/a

"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
Peter Olcott wrote:
><Mi*************@tomtom.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
>
Peter Olcott wrote:
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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& 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

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.
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.
>
/Peter
Why
>>
The problem with this is that it does not compile.

Aug 16 '06 #13

P: n/a

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eb**********@murdoch.acc.Virginia.EDU...
Peter Olcott wrote:
>>
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
>>>
Peter Olcott wrote:
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

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 intby 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;
typedef std::vector< unsigned int second_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.
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.
>
Best

Kai-Uwe Bux

Aug 16 '06 #14

P: n/a

"Philip Potter" <ph***********@xilinx.comwrote in message
news:eb*********@cliff.xsj.xilinx.com...
"Peter Olcott" <ol****@att.netwrote in message
news:IVGEg.815$Tl4.453@dukeread06...
>>
<Mi*************@tomtom.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
>
Peter Olcott wrote:
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<intm_vec;
std::vector<int>& alias() { return m_vec; }
std::vector<intconst& alias() const { return m_vec; }
};

HTH,
Michiel Salters

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.

--
Philip Potter
My code has been correct for more than a year. Now I am optimizing it.
Aug 16 '06 #15

P: n/a

"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Peter Olcott wrote:
>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.
But sometimes implements them as pointers that take up space.
>
#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<intv;
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.
>
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

Aug 16 '06 #16

P: n/a

"Howard" <al*****@hotmail.comwrote in message
news:fD*********************@bgtnsc04-news.ops.worldnet.att.net...
>
"Peter Olcott" <ol****@att.netwrote in message
news:AYGEg.817$Tl4.758@dukeread06...
>>
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
>>>
Peter Olcott wrote:
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

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

Aug 16 '06 #17

P: n/a

"Peter Olcott" <ol****@att.netwrote in message
news:T2IEg.830$Tl4.476@dukeread06...
>
"Howard" <al*****@hotmail.comwrote in message
news:fD*********************@bgtnsc04-news.ops.worldnet.att.net...
>>
"Peter Olcott" <ol****@att.netwrote in message
news:AYGEg.817$Tl4.758@dukeread06...
>>>
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googleg roups.com...

Peter Olcott wrote:
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
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?

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
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
Aug 16 '06 #18

P: n/a

"Howard" <al*****@hotmail.comwrote in message
news:Hb*********************@bgtnsc05-news.ops.worldnet.att.net...
>
"Peter Olcott" <ol****@att.netwrote in message
news:T2IEg.830$Tl4.476@dukeread06...
>>
"Howard" <al*****@hotmail.comwrote in message
news:fD*********************@bgtnsc04-news.ops.worldnet.att.net...
>>>
"Peter Olcott" <ol****@att.netwrote in message
news:AYGEg.817$Tl4.758@dukeread06...

"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@i3g2000cwc.google groups.com...
>
Peter Olcott wrote:
>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
>

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?

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

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.
Aug 16 '06 #19

P: n/a
Peter Olcott wrote:
"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Peter Olcott wrote:
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.

But sometimes implements them as pointers that take up space.
True. But in the example I gave, most compilers would optimize it
away.
#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<intv;
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.
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

Aug 16 '06 #20

P: n/a

"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.com...
Peter Olcott wrote:
>"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11**********************@75g2000cwc.googlegr oups.com...
Peter Olcott wrote:
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.

But sometimes implements them as pointers that take up space.

True. But in the example I gave, most compilers would optimize it
away.
#include <vector>
#include <cassert>

int main()
{
using std::vector;
vector<intv;
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.

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
16, 16, 4 with maximum space optimization turned on.
Aug 16 '06 #21

P: n/a

Peter Olcott wrote:
"Howard" <al*****@hotmail.comwrote in message
news:Hb*********************@bgtnsc05-news.ops.worldnet.att.net...

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.
Knock yourself out...

Aug 16 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.