Solved

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

Posted on 2002-04-04
9
340 Views
Last Modified: 2013-12-14
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
Comment
Question by:klopter
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6917902
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
 
LVL 86

Expert Comment

by:jkr
ID: 6917906
>>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
 

Author Comment

by:klopter
ID: 6917964
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
 
LVL 86

Expert Comment

by:jkr
ID: 6918013
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6918025
I believe the poratble method to replace begin() is the following:

sub( ..., &MyVec[0], ... ) ;
0
 
LVL 30

Expert Comment

by:Axter
ID: 6918026
jkr,
You beat me to it.  You're too fast.
0
 

Author Comment

by:klopter
ID: 6918152
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
 
LVL 4

Expert Comment

by:IainHere
ID: 6918227
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
 
LVL 30

Expert Comment

by:Axter
ID: 6918255
>>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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now