Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using STL Vector [modified]

using STL Vector [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    act_x
    wrote on last edited by
    #1

    I hope i could get a view on this . if i have an STL vector vector<BYTE> vec[45] ; Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; some operations .. p++; is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ? -- modified at 15:44 Wednesday 11th April, 2007 -- modified at 16:32 Wednesday 11th April, 2007

    Engineering is the effort !

    M L M S 4 Replies Last reply
    0
    • A act_x

      I hope i could get a view on this . if i have an STL vector vector<BYTE> vec[45] ; Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; some operations .. p++; is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ? -- modified at 15:44 Wednesday 11th April, 2007 -- modified at 16:32 Wednesday 11th April, 2007

      Engineering is the effort !

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      It can be if you intend on accesing bytes of the vector objects. It doesn't point to the elements contained in the vectors. *edited*

      "If you can dodge a wrench, you can dodge a ball."

      L A 2 Replies Last reply
      0
      • M Mark Salsbery

        It can be if you intend on accesing bytes of the vector objects. It doesn't point to the elements contained in the vectors. *edited*

        "If you can dodge a wrench, you can dodge a ball."

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        It can be if you intend on accesing bytes of the vector object.

        :laugh::laugh::laugh: You win a free Fish Filet! :)

        led mike

        M 1 Reply Last reply
        0
        • A act_x

          I hope i could get a view on this . if i have an STL vector vector<BYTE> vec[45] ; Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; some operations .. p++; is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ? -- modified at 15:44 Wednesday 11th April, 2007 -- modified at 16:32 Wednesday 11th April, 2007

          Engineering is the effort !

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          you might want to investigate Iterators

          led mike

          1 Reply Last reply
          0
          • L led mike

            Mark Salsbery wrote:

            It can be if you intend on accesing bytes of the vector object.

            :laugh::laugh::laugh: You win a free Fish Filet! :)

            led mike

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Thanks man! I'm HUNGRY! Looking at it again... &vec is the address of the address of the first vector in the array? :wtf: Help :laugh:

            "If you can dodge a wrench, you can dodge a ball."

            1 Reply Last reply
            0
            • M Mark Salsbery

              It can be if you intend on accesing bytes of the vector objects. It doesn't point to the elements contained in the vectors. *edited*

              "If you can dodge a wrench, you can dodge a ball."

              A Offline
              A Offline
              act_x
              wrote on last edited by
              #6

              Sorry I intended to use Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; This is meant to point to the first byte . however is it safe to do operations such as p++ and expect it to move to the next vector element( next byte in this case) . I am asking this since someone gave me this as a solution of creating a block of memory dynamically and not worrying about calling delete ( as one would with doing new) .

              Engineering is the effort !

              M N 3 Replies Last reply
              0
              • A act_x

                Sorry I intended to use Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; This is meant to point to the first byte . however is it safe to do operations such as p++ and expect it to move to the next vector element( next byte in this case) . I am asking this since someone gave me this as a solution of creating a block of memory dynamically and not worrying about calling delete ( as one would with doing new) .

                Engineering is the effort !

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                act_x wrote:

                Sorry I intended to use Now BYTE * p = reinterpret_cast&vec[0] ;

                Cool. That syntax reads better to me :) Just reinterpret_cast<BYTE*>(vec) "works" too.

                act_x wrote:

                however is it safe to do operations such as p++ and expect it to move to the next vector element( next byte in this case) .

                p points to the first byte of the first vector object, NOT to the elements of the first vector. So, no. Mark

                "If you can dodge a wrench, you can dodge a ball."

                1 Reply Last reply
                0
                • A act_x

                  Sorry I intended to use Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; This is meant to point to the first byte . however is it safe to do operations such as p++ and expect it to move to the next vector element( next byte in this case) . I am asking this since someone gave me this as a solution of creating a block of memory dynamically and not worrying about calling delete ( as one would with doing new) .

                  Engineering is the effort !

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  act_x wrote:

                  I am asking this since someone gave me this as a solution of creating a block of memory dynamically and not worrying about calling delete ( as one would with doing new) .

                  A "smart pointer" or similar may be a better solution.

                  "If you can dodge a wrench, you can dodge a ball."

                  1 Reply Last reply
                  0
                  • A act_x

                    I hope i could get a view on this . if i have an STL vector vector<BYTE> vec[45] ; Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; some operations .. p++; is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ? -- modified at 15:44 Wednesday 11th April, 2007 -- modified at 16:32 Wednesday 11th April, 2007

                    Engineering is the effort !

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    This line:

                    vector vec[45];

                    does not declare a vector. It declares an array of vectors. From your description, I don't think that's what you intended. But if you have a vector, its storage is guaranteed to be contiguous, so taking the address of vec[0] is fine.


                    Last modified: 7hrs 19mins after originally posted --

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                    1 Reply Last reply
                    0
                    • A act_x

                      Sorry I intended to use Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; This is meant to point to the first byte . however is it safe to do operations such as p++ and expect it to move to the next vector element( next byte in this case) . I am asking this since someone gave me this as a solution of creating a block of memory dynamically and not worrying about calling delete ( as one would with doing new) .

                      Engineering is the effort !

                      N Offline
                      N Offline
                      nde_plume
                      wrote on last edited by
                      #10

                      The answer is basically no it is not safe, except in certain very resrictive circumstances. In particular if you add elements to the vector, your pointer could very readily be invalidated. A better way to do it is to use itertors. However, if your concern is simple block of memory, I'd suggest what you need is a change in perspective. A vector is a block of memory, albeit with a type structure imposed on it. Why pretend it is a BYTE*, why not just use a vector?

                      1 Reply Last reply
                      0
                      • A act_x

                        I hope i could get a view on this . if i have an STL vector vector<BYTE> vec[45] ; Now BYTE * p = reinterpret_cast<BYTE*>&vec[0] ; some operations .. p++; is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ? -- modified at 15:44 Wednesday 11th April, 2007 -- modified at 16:32 Wednesday 11th April, 2007

                        Engineering is the effort !

                        S Offline
                        S Offline
                        Stephen Hewitt
                        wrote on last edited by
                        #11

                        act_x wrote:

                        if i have an STL vector vector<BYTE> vec[45] ;

                        No you don't: you have an array of 45 vectors!

                        act_x wrote:

                        BYTE * p = reinterpret_cast<BYTE*>(&vec[0]);

                        NOTE: Underlined brackets were added by me. They are needed. This reinterpret_cast is not needed as the type of &vec[0] is already a BYTE*.

                        act_x wrote:

                        is it safe to assume that the pointer p can be used safely even though its pointing to the address of a vector ?

                        It isn't pointing to a vector but rather an element contained in the vector.

                        Steve

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups