Solved

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

Posted on 2002-04-04
9
370 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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