Solved

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

Posted on 2002-04-04
9
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 378
Arduino EDI - Programming language 3 128
max float value 3 75
difference between sorce folder and folder in eclipise 3 65
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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

752 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