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(s z);
ifstream file;
try {
file.open(fname .c_str(), ios_base::in);
// first line should be the equity ticker identifiers
file.getline(&v buff[0], sz);
Thanks
Gary 2 5246
Gary Kuehn wrote in news:Q4******** **********@fe10 .lga 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(s z);
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(&v buff[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/
Thanks for the critique.
I'm reviewing all your input, thanks again.
Gary
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@216 .196.109.145... Gary Kuehn wrote in news:Q4******** **********@fe10 .lga 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(s z);
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(&v buff[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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stefan Höhne |
last post by:
Hi,
as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to
MS' DOT.NET - compiler.
In the 6.0 version the capacity() of the vector did not change with the
call to clear(), in DOT.NET the capacity() is reduced to 0.
|
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.
Version 1 (crashes on deallocating)
#include <iostream>
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
by: Bob |
last post by:
Here's what I have:
void miniVector<T>::insertOrder(miniVector<T>& v,const T& item)
{
int i, j;
T target;
vSize += 1;
T newVector;
newVector=new T;
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value types or
std::vector<int>.
So where I would use an int* and reallocate it from time to time in C,
and randomly access it via , then I figure to copy the capacity and
reserve methods, because I just need a growable array.
I get to considering...
| |
by: Dilip |
last post by:
If you reserve a certain amount of memory for a std::vector, what
happens when a reallocation is necessary because I overshot the limit?
I mean, say I reserve for 500 elements, the insertion of 501st element
is going to cause some more allocation -- for arguments sake if the
vector re-grows to accomodate 1000 elements, I play around with it and
completely erase everything in it after I am done. Now how much does
the vector hold? Do I...
|
by: wenmang |
last post by:
hi,
I like to preallocate a vector of an element X, my question is after
calling reserve(), is there a guarantee that memory is contiguous?
|
by: T. Crane |
last post by:
Hi,
I'm struggling with how to initialize a vector<vector<double>>
object. I'm pulling data out of a file and storing it in the
vector<vector<double>object. Because any given file will have a
large amount of data, that I read off using an ifstream object, I
don't want to use the push_back method because this grows the
vector<vector<double>dynamically, and that will kill my execution
time. So, I want to reserve space first, using, of...
|
by: Mike -- Email Ignored |
last post by:
In std::vector, is reserve or resize required?
On:
Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU/Linux
Using:
g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
The program below fails, but if the reserve(en)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |