For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2006 > is this way of using a std::vector safe?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author is this way of using a std::vector safe?
alanw

2006-02-08, 10:12 am

Hi,

I was wondering if anyone know whether the following code
is safe to use:

int aStaticArray[ 20 ];
for ( int i = 0; i < 20; i++ )
aStaticArray[ i ] = 20 - i;

vector<int> vIntVector;

// copy data pointers into vector
vIntVector._Myfirst = aStaticArray;
vIntVector._Mylast = vIntVector._Myfirst + 20;
vIntVector._Myend = vIntVector._Mylast;

// sort static array
sort( vIntVector.begin(), vIntVector.end() );

// reset pointers to avoid static array corruption
vIntVector._Myfirst = NULL;
vIntVector._Mylast = NULL;
vIntVector._Myend = NULL;


I've tried this in VS 2003 and it seems ok - the static
array is correctly sorted. However, I'm well aware that
the code looks a little dodgy!

Does anyone know any reason why this shouldn't work, or is
there a more elegant method of inserting your own data
pointer into a vector.

Please note, that I know the above example seems a little odd,
since I could have simply push_back'ed 20 integers and
sorted them as normal (or used qsort on the static array) - I'd
just like to know whether this is ok.

Thanks,
Alan.


Igor Tandetnik

2006-02-08, 10:12 am

alanw <a@hotmail.com> wrote:
> I was wondering if anyone know whether the following code
> is safe to use:
>
> int aStaticArray[ 20 ];
> for ( int i = 0; i < 20; i++ )
> aStaticArray[ i ] = 20 - i;
>
> vector<int> vIntVector;
>
> // copy data pointers into vector
> vIntVector._Myfirst = aStaticArray;
> vIntVector._Mylast = vIntVector._Myfirst + 20;
> vIntVector._Myend = vIntVector._Mylast;


This is a very bad idea. It relies on internal undocumented details of
this particular implementation of the class. It's completely
non-portable, possibly even between different versions of the same
compiler.

> // sort static array
> sort( vIntVector.begin(), vIntVector.end() );


Why not just

sort(aStaticArray, aStaticArray + 20);

?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


alanw

2006-02-08, 10:12 am


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eV53GeNKGHA.1288@TK2MSFTNGP09.phx.gbl...
> alanw <a@hotmail.com> wrote:
>
> This is a very bad idea. It relies on internal undocumented details of
> this particular implementation of the class. It's completely non-portable,
> possibly even between different versions of the same compiler.
>
>
> Why not just
>
> sort(aStaticArray, aStaticArray + 20);
>
> ?
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>


Many thanks for your advice Igor - I knew it looked hacked, and rest
assured I won't be using that code!

I was under the impression that std::sort required randon access iterators
to be implemented, however trying your suggestion and looking at the
internals it looks like a very elegant solution.

Thanks again,
Alan.


Doug Harrison [MVP]

2006-02-08, 10:12 am

On Fri, 03 Feb 2006 17:48:31 GMT, "alanw" <a@hotmail.com> wrote:

>
>I was under the impression that std::sort required randon access iterators
>to be implemented, however trying your suggestion and looking at the
>internals it looks like a very elegant solution.


Pointers are random access iterators. That is, they satisfy the
requirements that define what a "random access iterator" is.

--
Doug Harrison
Visual C++ MVP
Sean Connery

2006-02-08, 7:08 pm

"alanw" wrote:
> Many thanks for your advice Igor - I knew it looked hacked, and rest
> assured I won't be using that code!
>
> I was under the impression that std::sort required randon access iterators
> to be implemented, however trying your suggestion and looking at the
> internals it looks like a very elegant solution.


Additionally, I'd suggest you buy Josuttis' STL book. It will explain
everything in painful detail. Its a good reference, but as all reference
books should be read, you should read it once so you know whats in there.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com