Accessing vector content even though size = 0

H

Hansen

Hi group,

I'm writting a test at the moment where I want to inspect the content of a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0). So when I try to inspect the vector afterwards, it appears empty.

I there a way to get a pointer to the beginning of the vector, since the
data is still there. Or is it possible to perform a resize(n) that doens't
zero out the content?

I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

Any ideas?

(I've tried using the #define protected public hack, but since the code
being tested is located in another compilation unit, I get a dllimport
error, since some of the methods being used are defined as protected)
 
T

Triple-DES

Hi group,

I'm writting a test at the moment where I want to inspect the content of a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0). So when I try to inspect the vector afterwards, it appears empty.

I there a way to get a pointer to the beginning of the vector, since the
data is still there. Or is it possible to perform a resize(n) that doens't
zero out the content?

I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

Any ideas?

(I've tried using the #define protected public hack, but since the code
being tested is located in another compilation unit, I get a dllimport
error, since some of the methods being used are defined as protected)

For a vector v, &*v.begin() and &v[0] yields a pointer to the first
element.
 
H

Hansen

I'm writting a test at the moment where I want to inspect the content of
a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0). So when I try to inspect the vector afterwards, it appears
empty.

I there a way to get a pointer to the beginning of the vector, since the
data is still there. Or is it possible to perform a resize(n) that
doens't
zero out the content?

I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

Any ideas?

(I've tried using the #define protected public hack, but since the code
being tested is located in another compilation unit, I get a dllimport
error, since some of the methods being used are defined as protected)

For a vector v, &*v.begin() and &v[0] yields a pointer to the first
element.

But when size=0 (due to the "hidden" resize(0) call) this causes a runtime
error. My problem is get the address of the vector even though size = 0.
 
T

Triple-DES

For a vector v, &*v.begin() and &v[0] yields a pointer to the first
element.

But when size=0 (due to the "hidden" resize(0) call) this causes a runtime
error. My problem is get the address of the vector even though size = 0.

If you are using VC++,
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0

should disable those "nasty" runtime checks =)
 
H

Hansen

[snip]
For a vector v, &*v.begin() and &v[0] yields a pointer to the first
element.

But when size=0 (due to the "hidden" resize(0) call) this causes a
runtime
error. My problem is get the address of the vector even though size = 0.

If you are using VC++,
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0

should disable those "nasty" runtime checks =)

Problem is that I'm building for both win32 using VC++ and for StrongArm
using a Diab compiler.
But I'll note that little define trick into the "book of trick" :eek:)
 
R

RedNax

Hi group,

I'm writting a test at the moment where I want to inspect the content of a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0). So when I try to inspect the vector afterwards, it appears empty.

I there a way to get a pointer to the beginning of the vector, since the
data is still there. Or is it possible to perform a resize(n) that doens't
zero out the content?

I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

Any ideas?

(I've tried using the #define protected public hack, but since the code
being tested is located in another compilation unit, I get a dllimport
error, since some of the methods being used are defined as protected)

The easiest way for this problem is create a copy of the vector and
use the copy instead of the original vector as the parameter, like
this:

FuncProcessAndEmpty( vector<typeA>(vecA) );

instead of :

FuncProcessAndEmpty( vecA );

do not rely on resize() or something else - it dangerous!
 
R

Richard Herring

Hansen said:
Hi group,

I'm writting a test at the moment where I want to inspect the content of a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0).

Start with the real problem. *Why* does a function called "send"
actually perform "clear"?
So when I try to inspect the vector afterwards, it appears empty.

It _is_ empty, by any standard-conforming interpretation.
I there a way to get a pointer to the beginning of the vector, since the
data is still there.

For some definition of "still there" involving the words "undefined
behaviour", maybe.
Or is it possible to perform a resize(n) that doens't
zero out the content?
No.

I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

Because it's an implementation detail.
Any ideas?

Solve the real problem, which is in the algorithm, not the low-level
details.
 
A

Andrew Koenig

I'm writting a test at the moment where I want to inspect the content of a
vector that I uses as a buffer. The problem is that the function which
populates the vector also sends the vector and the send method performs a
resize(0). So when I try to inspect the vector afterwards, it appears
empty.

Right. That's because it is empty.
I there a way to get a pointer to the beginning of the vector, since the
data is still there. Or is it possible to perform a resize(n) that doens't
zero out the content?

Calling resize(0) sets the size to zero. After that, the vector has no
content.
I now that the _Myfirst member of vector points to the beginning of the
vector, but thats a protected member and hence inaccessible.

If the size is zero, the vector has no beginning.
Any ideas?

If you are trying to inspect the content of vector, don't throw the content
away until after you've inspected it.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top