473,386 Members | 1,860 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,386 software developers and data experts.

bad_alloc error...

terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted


could anyone help me in this?
Expand|Select|Wrap|Line Numbers
  1. //mst.cpp
  2. #include <iostream>
  3. #include <fstream>
  4. #include "Graph.h"
  5. #include "LList.h"
  6. #include "Edge.h"
  7. //#include "Prim.h"
  8. #include "Heap.h"
  9. #include "Kruskal.h"
  10. using std::cout;
  11. using std::cerr;
  12. using std::endl;
  13. using std::exit;
  14. using std::ifstream;
  15. using std::cin;
  16.  
  17. main (int argc, char *argv[])
  18. {
  19.  
  20.     int FNum;
  21. // Check usage.
  22.     if ( argc < 2 ) 
  23.     {
  24.         cerr << "Usage:\n\t" << argv[0] << " file_name" << endl;
  25.         exit (-1);
  26.     }
  27.     for ( FNum = 1; FNum < argc; FNum++ )
  28.     {
  29. // Open an input file
  30.         ifstream GraphData (argv[FNum]);
  31.         if ( ! GraphData.good () ) 
  32.         {
  33.             cerr << "Could not open GraphData file\n";
  34.             exit (-1);
  35.         }
  36.         cout << "\n\nOpened file " << argv[FNum] << " for processing\n";
  37.         int GSize;
  38.         GraphData >> GSize;
  39.         if ( GraphData.bad () ) 
  40.         {
  41.             cerr << "Bad read\n";
  42.             exit (-1);
  43.         }
  44.         Graph G(GSize); // Declare a graph.
  45. // ...
  46. }
  47.  
  48. class Graph
  49. {
  50.  
  51. public:
  52.  
  53. Graph ( const int &);     // Constructor
  54. void Print ();         // Print a rectangular adjacency matrix - not used
  55. void PrintEdge ( const int &, const int & );     // Print one edge.        
  56. Edge GetEdge ( const int &, const int & );     // Get an Edge object
  57. void SetEdge ( const Edge & );             // Set an edge weight.
  58. float GetWeight ( const int &, const int & ) ;  // Get an edge weight from
  59.                         // the graph.
  60. int GetSize ();                 // Return number of nodes
  61.  
  62. private:
  63.  
  64. int NumVert;             // Number of nodes.
  65. float *AdjMat;             // Adjacency matrix.
  66.  
  67. };
  68.  
  69. // Parameter: GraphSize - number of nodes.
  70. // Graph constructor. Create an empty
  71. // adjacency matrix of size ( GraphSize ^2 ).
  72.  
  73. Graph::Graph ( const int &GraphSize )
  74. {
  75.  
  76. // allocate memory for the adjacency matrix
  77.     AdjMat = new float[GraphSize * GraphSize];
  78.     NumVert = GraphSize;     // Initialize NumVert private member
  79.     for ( int i=0; i < NumVert; i++ ) // Initalize all edges to ENOEDGE
  80.        for ( int j=0; j < NumVert; j++ )
  81.         AdjMat[(i * GraphSize) + j] = ENOEDGE;
  82. }
  83.  
  84.  
thanks
regards,
usha
May 2 '07 #1
3 4024
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. AdjMat = new float[GraphSize * GraphSize];
  2.  
How big is this array?? One float is 4 bytes.

A bad_alloc exception is thrown by new when it runs out of heap memory.
May 2 '07 #2
Ganon11
3,652 Expert 2GB
Chenged the thread title.
May 2 '07 #3
AdrianH
1,251 Expert 1GB
Hi jyothiusha,

I apologise for erasing most of your code, but if you look at the posting guidelines, it states to post only relevant sections. If you are new to C++, this may be a little difficult, but here are some tips:
  • When you compile, you will be given file names and line numbers where the errors/warnings occur. Post these along with the line where the problem occurred. Actually, don’t just post the one line, but with a bit of context. The amount of context could be up to the beginning of the function with supporting class(es).
  • It is a good idea to break the problem down in to smaller pieces so that you can post something that is compliable, but contains only what is necessary to replicate the error that you are having. In fact, by doing this, you will be more than halfway there to figuring out what the problem is yourself.
  • Keep your functions small to make debugging it easier. The less irrelevant information in the function the better. In fact, a function should contain no irrelevant information, as that adds to the noise, making your function harder to read and debug (what you have done is right on the money there ;)).
  • If it becomes necessary for someone to ask you for the entire code base, please Private Message (PM) it to the party asking for it.
All of these points will actually help you in learning how to debug your code yourself.

Now as for your problem, weaknessforcats is probably correct. Check out what the value of GraphSize is when it is read in from the file. If you know that the file is correct and that you are parsing it correctly, you may be parsing it incorrectly. Print out the value you are reading in and that may give you an idea of what is going wrong.

Hope this helps, come back if you are still having trouble.


Adrian
May 2 '07 #4

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

Similar topics

5
by: Asfand Yar Qazi | last post by:
Hi, Just wondering, if I do a: std::set_new_handler(std::terminate) I won't have to worry about a std::bad_alloc being thrown when I do a 'new ...' or a 'new(nothrow) ...', right? Its...
5
by: Gary Wessle | last post by:
Hi I am getting this error when running a very similar code like the below, it is made for illustration only. thanks *************************************** **************** error...
14
by: Mohsen | last post by:
Hello everyone, In my program, I have to define many pointers and when I want to compile my program (in UNIX), it gives me the following error: terminate called after throwing an instance of...
3
by: schizoid_man | last post by:
Hi, I have the following code snippets and I get a std::bad_alloc error where I think there should be none. I've attached the relevant bits of the base class, derived class and the .cpp file...
1
by: michigaki | last post by:
Hi, we're currently working on a video streaming app. Here is the problem: int ConvertRGBtoYUV(int w, int h, unsigned char *rgb, unsigned int *yuv) { unsigned int *uu, *vv, *u, *v; unsigned...
4
by: Alerion | last post by:
Hello everyone, I've been a regular of this forum but this is my first post, generally I can find the answer to my question already, but this time I'm having a somewhat specific problem. For...
3
by: George2 | last post by:
Hello everyone, Please help to comment whether my following understanding is correct, 1. whether or not we are using auto_ptr to allocate new object on heap (using new), there may be...
6
by: George2 | last post by:
Hello everyone, I usually check whether there is bad_alloc thrown to identify whether the allocation is success or not. My question is, Is there a way to disable bad_alloc and just to...
16
upadhyad
by: upadhyad | last post by:
Hi everyone !! i have an interesting problem...hope you people can help. I am coding in C. the code is posted below: here I have a structure with two character vectors. I have to characters....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
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,...
0
jinu1996
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...

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.