Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Accessing raw values in a vector

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 22 Mar 2005 22:07 GMT
Hi all,

In unmanaged C++, I need to allocate an "array" of int values, put values
into the array, then call a function and pass it the address and length of
the array.  The length of the array is only known at run-time, so I assume
that my best course of action is to use a std::vector object for the
"array".  My question is, am I guaranteed that the vector stores its data in
contiguous memory, so that I can use vector.begin() as a pointer to the
memory containing my contiguous array of data?
Carl Daniel [VC++ MVP] - 22 Mar 2005 22:36 GMT
> Hi all,
>
[quoted text clipped - 6 lines]
> contiguous memory, so that I can use vector.begin() as a pointer to the
> memory containing my contiguous array of data?

Yes, you are.

-cd
Arnaud Debaene - 23 Mar 2005 01:34 GMT
> Hi all,

<snip>
>  My question is, am I guaranteed
> that the vector stores its data in contiguous memory,
Practically speaking yes (although it is not yet in the norm, it will soon
be).

> so that I can
> use vector.begin() as a pointer to the memory containing my
> contiguous array of data?
That, on the other hand, is wrong : there is no guarantee than a vector
iterator can be used as a pointer : You can use &MyVect[0] to get a pointer
to the begnning of the vector buffer.

Arnaud
MVP - VC
Tom Widmer - 23 Mar 2005 11:25 GMT
>>Hi all,
>
[quoted text clipped - 5 lines]
> Practically speaking yes (although it is not yet in the norm, it will soon
> be).

The 2003 standard says it explicitly: "The elements of a vector are
stored contiguously, meaning that if v is a vector<T, Allocator> where T
is some type other than bool, then it obeys the identity &v[n] == &v[0]
+ n for all 0 <= n < v.size()."

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.