Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is Vector Reserve guaranteed to allocate contiguous memory?

Reply
Thread Tools

Is Vector Reserve guaranteed to allocate contiguous memory?

 
 
Gary Kuehn
Guest
Posts: n/a
 
      07-18-2005

Is Reserve guaranteed to allocate contiguous memory?

How safe is the following:



vector<char> vbuff;

int sz = numeric_limits<short int>::max();

// reserve contiguous memory IS THIS ALWAYS TRUE ?

vbuff.reserve(sz);

ifstream file;

try {

file.open(fname.c_str(), ios_base::in);

// first line should be the equity ticker identifiers

file.getline(&vbuff[0], sz);



Thanks
Gary


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-18-2005
Gary Kuehn wrote in news:Q4WCe.16914$(E-Mail Removed) in comp.lang.c++:

>
> Is Reserve guaranteed to allocate contiguous memory?


Yes, but its implementation defined if that memory will
actually be there when you need it.

Some OS's only bprovide real memory when you use it, if some other
process (ay) comes along and uses up all the real memory and
swap file between when you "reserve" memory an actaly use it
then ou're out of luck.

>
> How safe is the following:
>


Not at all, you need resize, not reserve.

reserve only allocates memory, its not available until
you actually populate the vector.

>
>
> vector<char> vbuff;
>
> int sz = numeric_limits<short int>::max();
>
> // reserve contiguous memory IS THIS ALWAYS TRUE ?
>
> vbuff.reserve(sz);
>


vbuff.resize( sz );

Even better (for paranoid people anyway):

vbuff.resize( sz, '\0' );

as this will copy a 0 char to every location

It "uses" the memory, so you know its real not virtual.

> ifstream file;
>
> try {
>
> file.open(fname.c_str(), ios_base::in);
>
> // first line should be the equity ticker identifiers
>
> file.getline(&vbuff[0], sz);
>


How about:

std:string s;
std::getline( file, s );

I.e. let the standard library do all the memory allocation.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Gary Kuehn
Guest
Posts: n/a
 
      07-19-2005
Thanks for the critique.
I'm reviewing all your input, thanks again.

Gary

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns9697F297A8094rtwfreenetREMOVEcouk@216.196. 109.145...
> Gary Kuehn wrote in news:Q4WCe.16914$(E-Mail Removed) in comp.lang.c++:
>
>>
>> Is Reserve guaranteed to allocate contiguous memory?

>
> Yes, but its implementation defined if that memory will
> actually be there when you need it.
>
> Some OS's only bprovide real memory when you use it, if some other
> process (ay) comes along and uses up all the real memory and
> swap file between when you "reserve" memory an actaly use it
> then ou're out of luck.
>
>>
>> How safe is the following:
>>

>
> Not at all, you need resize, not reserve.
>
> reserve only allocates memory, its not available until
> you actually populate the vector.
>
>>
>>
>> vector<char> vbuff;
>>
>> int sz = numeric_limits<short int>::max();
>>
>> // reserve contiguous memory IS THIS ALWAYS TRUE ?
>>
>> vbuff.reserve(sz);
>>

>
> vbuff.resize( sz );
>
> Even better (for paranoid people anyway):
>
> vbuff.resize( sz, '\0' );
>
> as this will copy a 0 char to every location
>
> It "uses" the memory, so you know its real not virtual.
>
>> ifstream file;
>>
>> try {
>>
>> file.open(fname.c_str(), ios_base::in);
>>
>> // first line should be the equity ticker identifiers
>>
>> file.getline(&vbuff[0], sz);
>>

>
> How about:
>
> std:string s;
> std::getline( file, s );
>
> I.e. let the standard library do all the memory allocation.
>
> Rob.
> --
> http://www.victim-prime.dsl.pipex.com/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-contiguous port vector ranges??? Mark McDougall VHDL 2 11-02-2006 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Are array members guaranteed to be contiguous in physical memory? Olumide C Programming 9 11-30-2004 02:54 PM
Is std::vector contiguous? Ravi C++ 8 03-06-2004 12:47 AM
Is the storage for a std::vector<T> guaranteed to be contiguous? (from FAQ) Newsgroup - Ann C++ 0 08-15-2003 01:42 AM



Advertisments