Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Is this the right way to "cast" a vector to an array?

I have a code which had a local array whose size was not known at compile time.  g++ accepts this, but Solaris CC does not.  So, I would like to switch this to a vector but I want to make sure that I am using vector appropriately.

The code was:
   int numentries = NumEntries() ;
   int MyArray[ numentries ];
   .
   .
   .
   sub( ..., MyArray, ... ) ;

I have converted this to:
   vector <int>MyVec( numentries ) ;
   .
   .
   .
   sub( ..., MyVec.begin(), ... ) ;


This works, but it is portable?  Is there a better way?

Thanks,
  Ken
0
klopter
Asked:
klopter
  • 3
  • 2
  • 2
  • +1
1 Solution
 
IainHereCommented:
Depends on the code in sub(...).  If you're assuming the return of MyVec.begin() is a pointer, then chances are you'll have undefined behaviour, since begin() returns an iterator, rather than a pointer.

However, the iterator will be implemented as a pointer on every system that I know of, so it will work.  It is certainly not portable, and it should be thrown out at a code review.

If you didn't already know this, look up iterators, and come back with any subsequent questions.  Incidentally, the original code was wrong, and gcc was in error compiling it.  A portable way of doing this would be to replace your original lines with

int numentries = NumEntries();
int *myArray;
myArray = new int[numentries];

The advantage is that you will not have to change any other code, the disadvantages are that you don't get the safety of using a vector.
0
 
jkrCommented:
>>This works, but it is portable?  

Yes, it is - it conforms with the ANSI C++ standard. Th eonly trouble I see is when you would use a compiler that does not support the standard, but this would be a pretty outdated one...

>>Is there a better way?

Not that I'd know of :o)
0
 
klopterAuthor Commented:
IainHere
  I know that begin() returns an iterator and not a pointer.  Indeed that is why I asked the question.  I also realize that I could use new() and delete [] here, but I prefer to stay away from new/delete because, to my mind, they are only slightly better than malloc/free.  On the other hand, if my vector code is non-standard and/or not portable, that is a good enough reason to use new/delete.

The code in sub() expects an integer array.

IainHere and jkr's answers appear to be nearly opposite.  I hope that others will weigh in on this.

Thanks,
  Ken
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
IainHere is correct that it depends on what "sub()" takes as an argument. Be aware the you can use the index operator "[]" on a vector, too...
0
 
AxterCommented:
I believe the poratble method to replace begin() is the following:

sub( ..., &MyVec[0], ... ) ;
0
 
AxterCommented:
jkr,
You beat me to it.  You're too fast.
0
 
klopterAuthor Commented:
jkr,
  I could easily have given the points to you instead.  Perhaps your answers are more deserving, but I chose to give them to Axter's response because it gives exactly the syntax that I should use.

Thanks to both of you.

Ken
0
 
IainHereCommented:
OK, sorry to be the dense one here, but to my mind using

&MyVec[0];

as the pointer to the start of an array will be valid when and only when MyVec.begin() is also valid?  That is, when the vector of int is arranged the same way as an array?

So, although all current implementations of std::vector will conform, they don't _have_ to?
0
 
AxterCommented:
>>So, although all current implementations of std::vector
>>will conform, they don't _have_ to?
Not according to Scott Meyers.  In Effective STL (by Scott Meyers), he states that &MyVec[0] is the correct method for passing a vector as a C-type array, and that the begin() method doesn't have to work in all implementation, but the index operator[] method is required to work according to the standards.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now