using STL Vector [modified]
-
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 !
-
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 !
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."
-
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."
-
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 !
-
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
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."
-
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."
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 !
-
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 !
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."
-
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 !
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."
-
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 !
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?
-
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 !
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?
-
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 !
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 aBYTE*
.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