Accessing vector content even though size = 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hansen

    Accessing vector content even though size = 0

    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)


  • Triple-DES

    #2
    Re: Accessing vector content even though size = 0

    On 19 Feb, 10:08, "Hansen" <bluesb...@remo ve.the.spam.hot mail.com>
    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.

    Comment

    • Hansen

      #3
      Re: Accessing vector content even though size = 0

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


      Comment

      • Triple-DES

        #4
        Re: Accessing vector content even though size = 0

        On 19 Feb, 10:20, "Hansen" <bluesb...@remo ve.the.spam.hot mail.com>
        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_D EBUGGING 0

        should disable those "nasty" runtime checks =)

        Comment

        • Hansen

          #5
          Re: Accessing vector content even though size = 0

          [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_D EBUGGING 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" :o)


          Comment

          • RedNax

            #6
            Re: Accessing vector content even though size = 0

            On 2ÔÂ19ÈÕ, ÏÂÎç5ʱ08·Ö, "Hansen" <bluesb...@remo ve.the.spam.hot mail.com>
            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:

            FuncProcessAndE mpty( vector<typeA>(v ecA) );

            instead of :

            FuncProcessAndE mpty( vecA );

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

            Comment

            • Richard Herring

              #7
              Re: Accessing vector content even though size = 0

              In message <fpe69t$qmr$1@n ews.net.uni-c.dk>, Hansen
              <bluesboys@remo ve.the.spam.hot mail.comwrites
              >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

              Comment

              • Andrew Koenig

                #8
                Re: Accessing vector content even though size = 0

                "Hansen" <bluesboys@remo ve.the.spam.hot mail.comwrote in message
                news:fpe69t$qmr $1@news.net.uni-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.


                Comment

                Working...