Accessing vector content even though size = 0

Discussion in 'C++' started by Hansen, Feb 19, 2008.

  1. Hansen

    Hansen Guest

    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)
    Hansen, Feb 19, 2008
    #1
    1. Advertising

  2. Hansen

    Triple-DES Guest

    On 19 Feb, 10:08, "Hansen" <>
    wrote:
    > 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.
    Triple-DES, Feb 19, 2008
    #2
    1. Advertising

  3. Hansen

    Hansen Guest

    >> 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.
    Hansen, Feb 19, 2008
    #3
  4. Hansen

    Triple-DES Guest

    On 19 Feb, 10:20, "Hansen" <>
    wrote:
    > >> 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.


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

    should disable those "nasty" runtime checks =)
    Triple-DES, Feb 19, 2008
    #4
  5. Hansen

    Hansen Guest

    [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:)
    Hansen, Feb 19, 2008
    #5
  6. Hansen

    RedNax Guest

    On 2ÔÂ19ÈÕ, ÏÂÎç5ʱ08·Ö, "Hansen" <>
    wrote:
    > 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!
    RedNax, Feb 19, 2008
    #6
  7. In message <fpe69t$qmr$-c.dk>, Hansen
    <> writes
    >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.
    >
    > (I've tried using the #define protected public hack,


    UB.

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



    --
    Richard Herring
    Richard Herring, Feb 19, 2008
    #7
  8. "Hansen" <> wrote in message
    news:fpe69t$qmr$-c.dk...

    > 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.
    Andrew Koenig, Feb 19, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGh1cndpdHo=?=
    Replies:
    1
    Views:
    457
    Teemu Keiski
    Apr 10, 2004
  2. Joergen Bech
    Replies:
    2
    Views:
    3,288
    Joergen Bech
    Jul 2, 2005
  3. craig dicker
    Replies:
    9
    Views:
    675
    Juan T. Llibre
    Jul 7, 2005
  4. Replies:
    0
    Views:
    703
  5. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
Loading...

Share This Page