473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Add an exception about bad_alloc in a vector of vectors

I have a .csv file that i have to read and create a matrix through vector of vectors.
I have a problem about adding a try,catch if the matrix is too big to be stored in memory.
I tried to insert try,catch in different part of the program but noone worked.

Expand|Select|Wrap|Line Numbers
  1.  try {
  2. stringstream convertor(line);
  3. colonne=0;
  4. while (getline(convertor, token,','))
  5. {
  6.    tmp.push_back(stoi(token));
  7.    if (token=="0"){
  8.       zero.push_back(make_triplet(righe,colonne,0));
  9.       contzero++;
  10.                }
  11.       colonne++;
  12. }
  13.   matrix.push_back((tmp));
  14. }
  15.   catch (std::exception& ba)
  16. {
  17. cerr << "bad_alloc caught: " << ba.what() << '\n';
Dec 21 '15 #1
1 1235
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. catch (std::exception& ba)
  2. {
  3.     cerr << "bad_alloc caught: " << ba.what() << '\n';
  4. }
  5.  
is catching a bad_alloc but it is also catching any exception thrown by any object in the try block.

Not good.

You need to try just the push_back of the matrix.

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    matrix.push_back((tmp));
  4. }
  5. catch (std::bad_alloc& ba)
  6. {
  7.       ... bad_alloc caught here
  8. }
  9. catch (...)
  10. {
  11.      ..all other exceptions caught here
  12. }
  13.  
That said, catching a bad_alloc is pointless because by the time this gets thrown there is no free memory. If you are on Windows you wuld have been getting "windows running low on resources" message boxes for several minutes plus the entire computer will be processing vvvvvveeeeerrrrrryyyyy ssssslllllloooooowwwwwllllyyyyyyy.....
Dec 21 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
2
by: sd2004 | last post by:
Coudl someone make the following code more elegant ? #include<iostream> #include <string> #include<vector> #include<sstream> #include<fstream> using namespace std; struct astruct {
2
by: Priya Mishra | last post by:
Hi All It was very nice to intract with this group, While in my previous post, I was suggested to reffer the link, in order to learn C++, Well I was going thoruigh the link in which i had some...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
3
by: Vince C. | last post by:
Hi. I'm writing a C++ application that uses GNU getopt_long() to parse command line argument syntaxes. http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html I'd like to...
3
by: Aarti | last post by:
I was reading about exception safety in vectors and came across a staement that says The clear operation of vector guarantees a nothrow provided the copy and assignment operator do not throw an...
4
by: demonbunny666 | last post by:
I'm trying to get this program to work, but I am running into a problem and as hard as I try I can't figure it out. It compiles fine( VS Studio 6.0) but when it executes a problem occurs, something...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.