?
Solved

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

Posted on 2002-04-04
9
Medium Priority
?
375 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

718 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