Hi all,
I've got a simple question regarding stl containers. Consider this
code:
std::vector<flo at> foo;
foo.resize(100) ;
How do I know if memory allocation was successful? The resize method
returns nothing. I understand that resize does not always allocate
memory. But even if I use the reserve method it doesn't return boolean
or something to validate.
Should I do something like this:
if(foo.capacity ()<foo.size())
cout << "Memory allocation fail!" << endl;
Is there any standard way of doing this.
Thanks all
Kwijibo 6 13843
"kwijibo28" <kw*******@hotm ail.com> wrote... I've got a simple question regarding stl containers. Consider this code:
std::vector<flo at> foo; foo.resize(100) ;
How do I know if memory allocation was successful? The resize method returns nothing. I understand that resize does not always allocate memory.
If it does, and there isn't enough of it, std::bad_alloc is thrown.
But even if I use the reserve method it doesn't return boolean or something to validate.
Should I do something like this:
if(foo.capacity ()<foo.size()) cout << "Memory allocation fail!" << endl;
Is there any standard way of doing this.
Yes,
try {
foo.resize(100) ;
}
catch (std::bad_alloc const&) {
cout << "Memory allocation fail!" << endl;
}
Victor
kwijibo28 wrote: Hi all,
I've got a simple question regarding stl containers. Consider this code:
std::vector<flo at> foo; foo.resize(100) ;
How do I know if memory allocation was successful? The resize method returns nothing. I understand that resize does not always allocate memory. But even if I use the reserve method it doesn't return boolean or something to validate.
Should I do something like this:
if(foo.capacity ()<foo.size()) cout << "Memory allocation fail!" << endl;
Is there any standard way of doing this.
Research the 'std::bad_alloc ' exception class. The implementation (i.e.,
the standard C++ library) throws a std::bad_alloc object to report
storage allocation failures.
<example>
try {
std::vector<flo at> foo;
...
foo.resize(100) ;
...
}
catch ( const std::bad_alloc & error) {
// whoops... if control reaches here, a memory allocation
// failure occurred somewhere within the 'try{}' block
}
</example>
--
Jim
To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link58 09{at}now.here. com
"kwijibo28" wrote: Hi all,
I've got a simple question regarding stl containers. Consider this code:
std::vector<flo at> foo; foo.resize(100) ;
How do I know if memory allocation was successful? The resize method returns nothing. I understand that resize does not always allocate memory. But even if I use the reserve method it doesn't return boolean or something to validate.
Memory allocation requests don't fail. They succeed or throw an exception
(std::bad_alloc ).
HTH,
Patrick
kwijibo28 wrote: Thanks for the reply guys. I've tried all this but it still doesn't work. So I'll be a little more specific about what I am trying do. I'm writing an MFC aplication using VC++ 6.0.
When I write something like:
try { foo.resize(powe r(2,32)-1); } catch (std::bad_alloc const&) { ::MessageBox(NU LL,"Memory allocation fail!","Error", MB_OK); }
I've got a message box that pop up with a "Out of memory" message when foo.resize is called. I don't know where this message box come from.
Deep from the internals of your runtime system.
MS has yet to learn that emitting such messages is just a way to
support lazy programmers. The number of programmers trying to get
rid of the floppy-disk-failed message in DOS is legion. MS has not
learned from that.
And even worst, my message box with "Memory allocation fail!" don't appear.
VC++ 6.0 does not throw an exception when a memory allocation failes.
Instead NULL is returned from new. Don't know what resize does with
that information.
But honestly: 2 to the power of 32 is a lot of memory. If we assume
that foo holds unsigned characters, that would be 4 GB. Tracing your
post back, I see that foo holds floats, so that would sum up to
16 GB in VC++. That's a lot. Are you sure you need that much memory?
--
Karl Heinz Buchegger kb******@gascad .at
Karl Heinz Buchegger <kb******@gasca d.at> wrote in message When I write something like:
try { foo.resize(powe r(2,32)-1); } catch (std::bad_alloc const&) { ::MessageBox(NU LL,"Memory allocation fail!","Error", MB_OK); }
But honestly: 2 to the power of 32 is a lot of memory. If we assume that foo holds unsigned characters, that would be 4 GB. Tracing your post back, I see that foo holds floats, so that would sum up to 16 GB in VC++. That's a lot. Are you sure you need that much memory?
I usually don't need that much memory but that was just a test to be
sure that memory allocation would fail, because I was trying to catch
the exception. I don't think it's good practice not to check for
memory allocation problem just because it's a small array.
kwijibo28
On 11 Aug 2003 04:14:09 -0700, kw*******@hotma il.com (kwijibo28)
wrote: Karl Heinz Buchegger <kb******@gasca d.at> wrote in message > > When I write something like: > > try { > foo.resize(powe r(2,32)-1); > } > catch (std::bad_alloc const&) { > ::MessageBox(NU LL,"Memory allocation fail!","Error", MB_OK); > } > But honestly: 2 to the power of 32 is a lot of memory. If we assume that foo holds unsigned characters, that would be 4 GB. Tracing your post back, I see that foo holds floats, so that would sum up to 16 GB in VC++. That's a lot. Are you sure you need that much memory?
I usually don't need that much memory but that was just a test to be sure that memory allocation would fail, because I was trying to catch the exception. I don't think it's good practice not to check for memory allocation problem just because it's a small array.
In your case you've passed such a huge number that it will throw a
std::length_err or, since the memory allocator doesn't support
allocating that much memory even if your computer has it physically.
A better test would be:
try
{
while(1)
{
int* i = new int[10000000];
//leak!
}
}
catch(std::bad_ alloc const&)
{
}
but VC6 won't throw here anyway since it doesn't follow the standard
in this respect, but VC7.1 (and maybe 7.0) catches the exception.
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: Steve |
last post by:
Hi,
I'm using a std::vector to store a list of user defined objects. The vector
may have well over 1000 elements, and I'm suffering a performance hit. If I
use push_back I get a much worse perfomance than if I first define the
vector of a given size, then write to the elements with myvec =
However, I'm currently thinking that it isn't...
|
by: Rakesh |
last post by:
Hi All,
Assume I declared a vector of char * .
vector<string> x
How the memory is allocated ?
If I push a string of 100 bytes .
|
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...
|
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...
|
by: Johan Tibell |
last post by:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socket, args) == -1) {
perror("function");
exit(EXIT_FAILURE);
}
I feel that the ifs destroy the readability of my code. Would it be
|
by: nw |
last post by:
Hi,
We've been having a discussion at work and I'm wondering if anyone
here would care to offer an opinion or alternative solution. Aparently
in the C programming HPC community it is common to allocate
multidimentional arrays like so:
int *v_base = (int *) malloc(1000000*sizeof(int));
int **v = (int **) malloc(1000*sizeof(int *));
|
by: cham |
last post by:
Hi,
I am working on c++ in a linux system ( Fedora core 4 ),
kernel version - 2.6.11-1.1369_FC4
gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )
In my code i am creating a vector to store pointers of type structure
"SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
an instance of structure "SAMPLE_TABLE_STRUCT" using...
|
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...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |