I have the following coredump.
Program terminated with signal 11, Segmentation fault.
#0 0xb5b1c2f8 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
from /arm-none-linux-gnueabi/libc_m/usr/lib/libstdc++.so.6
#0 0xb5b1c2f8 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
from /arm-none-linux-gnueabi/libc_m/usr/lib/libstdc++.so.6
#1 0x00964f5c in Qct::OamClientUtil::getRsp (this=<value optimized out>)
at /Agent/include/oamClientUtil.hpp:92
#2 0x00902118 in MapPM::sendCollect (this=0xb4af9928) at /gent/src/mapPM.cc:557
#3 0x00921dc0 in Agent::handlePMCollectReq (this=<value optimized out>, buf=0xb335ccb0)
at /Agent/src/Agent.cc:5671
#4 0x0095deb4 in AgentRxHandler (handle=<value optimized out>, buf=0xb335ccb0)
at /Agent/src/uslAgent.cc:398
#5 0x0080f364 in uslDCTEntry (dct=20401048) at /usl/src/lcid/uslDCT.cc:253
#6 0x009c867c in TASK::taskEntryPoint (params=0x1375098) at /emvxworks/taskLib.cpp:224
#7 0x009c7bbc in pthreadw_taskentry (arg=<value optimized out>) at /emvxworks/pthread_wrapper.cpp:786
#8 0xb59a6120 in start_thread (arg=0xb4aff460) at pthread_create.c:307
#9 0xb592e9f8 in nfsservctl ()
from /CodeSourcery/4.6.3-2012.03-57/arm-none-linux-gnueabi/libc_m/lib/libc.so.6
Backtrace stopped: frame did not save the PC
I am quite confused how a simple string is causing the crash.
Since the code is pretty big I will paste the relevant classes for now. Will add more if needed.
/Agent/include/oamClientUtil.hpp:92
37 class OamClientUtil
38 {
39 public:
40 OamClientUtil();
41
42 virtual
43 ~OamClientUtil();
44
45
46 void
47 initOamProxyConn();
48
49 void
50 setBatch(
51 OamClients& batch
52 );
53
54 /// load oam transactions from a file
55 /// and convert them to a list of oam client transactions
56 bool
57 loadTrxFromFile(
58 std::string& batchfilename
59 );
60
61 /// actual processing the list of transactions
62 MgmtResult
63 run();
64
65 bool
66 isGood();
67
68 /// return the dryrun flag
69 bool
70 isDryrun();
71
72 /// set the dryrun flag
73 bool
74 isDryrun(bool flag);
75
76 const OamErrorString&
77 errorString() const;
78
79
80 void
81 initRsp() {
82 getRsp_ = "";
83 }
84 void
85 initKeyRsp() {
86 getKeysRsp_ = "";
87 }
88
89
90 std::string
91 getRsp() {
92 return getRsp_;
93 }
94 std::string
95 getKeysRsp() {
96 return getKeysRsp_;
97 }
98 bool
99 getKeysLast() {
100 return getKeysLast_;
101 }
102 private:
103 static const unsigned REQ_TIMEOUT_SECS = 10;
104
105 /// send get request
106 MgmtResult
107 sendGetReq(
108 OamClientTrxPtr trx
109 );
110
111 /// send getKeys request
112 MgmtResult
113 sendGetKeysReq(
114 OamClientTrxPtr trx
115 );
116 /// send set request
117 MgmtResult
118 sendSetReq(
119 OamClientTrxPtr trx
120 );
121
122 /// send clear request
123 MgmtResult
124 sendClearReq(
125 OamClientTrxPtr trx
126 );
127
128 /// process the list of transactions
129 MgmtResult
130 processTransactionList(
131 OamClientTrxList& trxlist ///< The list of transaction to be processed
132 );
133
134 /// process one transaction
135 MgmtResult
136 processTransaction(
137 OamClientTrxPtr trx ///< The transaction to be processed
138 );
139
140 /// print content of one transaction
141 void
142 printRequests(
143 OamClientTrxPtr trx
144 );
145
146 void
147 indCallback(
148 unsigned int msgId,
149 QctUint8_t* indMsgBuffer,
150 unsigned int indMsgBufferLength);
151
152
153 static ProvisioningPtr
154 initProvisioningPtr();
155
156
157 static MgmtXmlPtr
158 initMgmtXmlPtr();
159
160 QmiClient qmiClient_;
161 MgmtXmlPtr mgmtXmlPtr_;
162 OamClients batch_;
163 SerialStreamFixedBuf<QMI_FSM_OAM_CLIENT_MAX_LENGTH_V04> sstream_;
164 QctUint16_t trxid_;
165
166 std::string batchfilename_;
167 bool isDryrun_;
168 bool isGood_;
169 QctUint32_t oamSessionId_;
170 OamErrorString errorMsg_;
171 std::string getRsp_;
172 std::string getKeysRsp_;
173 bool getKeysLast_;
174 ProvisioningPtr provisioningPtr_;
175 };
176
#2 0x00902118 in MapPM::sendCollect (this=0xb4af9928) at /Agent/src/mapPM.cc:557
549Qct::MgmtResult MapPM::sendCollect()
550{
551 clUtil_.initRsp();
552 clUtil_.setBatch(oamClients_);
553 Qct::MgmtResult mr = clUtil_.run(); // process every transaction in oamClients_
554 if (mr == Qct::MGMT_RESULT_FAIL) {
555 //return Qct::MGMT_RESULT_FAIL;
556 }
557 return makePMFile(clUtil_.getRsp());
558
}
clUtil_ is object of Qct::OamClientUtil
Qct::OamClientUtil clUtil_;
/Agent/src/Agent.cc:5671
5671 Qct::MgmtResult result = mapPM.sendCollect();
/Agent/src/uslAgent.cc:398
400 case OAM_AGENT_PMSETUP:
401 oamAgent.handlePMSetupReq(buf);
402 break;
I don't think there should be any valid reason for your program to crash at this point -- that is assuming that;
All the code have been compiled with the same compiler
The header files are not compiled differently in different files (i.e. you didn't forget to recompile something after you changed a header file).
I think you most likely have a memory corruption somewhere else and it shows up (by bad luck) when you accesses this class. Try to use a tool like valgrind or similar to track down anything bad being done by the rest of your code.
Related
I am working on a program where I need to read ints from a file into an array and use the array to do work. not super complicated and shouldtm take next to no time at all.
i am still a student learning c++ and i've exhausted all my options trying to get this to function as I understand it.
The text file has 200 lines each line contains a int followed by a '\n' newline character.
When I read the file in the main function into an int it works as expected. when I pass the ifstream by reference into the function that does the exact same for loop it breaks giving me an uninitialized in as the only value.
#include<iostream> // required
#include<fstream>
#include<random>
#include<string>
using namespace std; // using standard namespace as for this entire program
void readNumbers(ifstream&, int[], int); // passing a input filestream by reference, c style array, and an integer for the array length
int totalInts(ifstream&); // this will read how many lines the file has
int main()
{
ifstream is("numbers.txt"); // this is the text file that we are reading from
int ar[200]; // the array of length 200 that everything will be read into
int temp; // temporary holding for our ints
if (!is) // error checking
cout << "somethings <expletive deleted>";
else {
for (int i = 0; i < 200; i++) // iterating through the file 200 times and inputting the int into the temp holding, this is being used to test if this method works as expected.
{
is >> temp;
cout << i << ' ' << temp << '\n'; // prints it out for troubleshooting
}
// cout << totalInts(is);
readNumbers(is, ar, 200);
}
return 0;
}
int totalInts(ifstream& file)
{
string i;
int intNum;
intNum = 0;
while (getline(file, i))
{
intNum++;
}
return intNum;
}
void readNumbers(ifstream &reading, int papi[], int rayLength) // where we really want to read the file
{
int temp; // for temp holding while we troubleshoot
if (!reading)
cout << "somethings fucked";
else
{
for (int i = 0; i < rayLength;i++) // reads the file for as many time as we have space in the array.
{
reading >> temp; // reads into temp
cout << temp; // outputs for troubleshooting
}
}
}
the program outputs below
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 12832) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
as you can see the output for the first for loop is exactly as expected, however the function which has all the same functionality is not working and is showing an uninitialized int for temp.
this means that it is not reading from the ifstream which I passed to the function. so wrote another function that reads the file using a newly created ifstream as well as the passed ifstream and prints what is read from both streams.
totalInts(ifstream &file)
{
ifstream tester("numbers.txt");
string l,i;
int intNum;
intNum = 0;
for (int j = 0; j < 200; j++)
{
file >> l;
tester >> i;
intNum++;
cout << intNum << ' ' << i << ' ' << l << '\n';
}
file.close();
return intNum;
}
when this is used the output is as expected, so passing the ifstream as a param using reference is the issue, as when I make a new ifstream the program runs fine using that ifstream and reading the file as expected where as the passed ifstream shows no values read.
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
1 41
2 485
3 340
4 526
5 188
6 739
7 489
8 387
9 988
10 488
11 710
12 173
13 304
14 843
15 970
16 491
17 997
18 953
19 831
20 441
21 423
22 618
23 905
24 153
25 292
26 394
27 438
28 734
29 737
30 914
31 452
32 747
33 785
34 549
35 870
36 931
37 692
38 325
39 52
40 903
41 731
42 834
43 353
44 363
45 690
46 668
47 156
48 718
49 281
50 874
51 572
52 671
53 694
54 789
55 57
56 871
57 731
58 750
59 556
60 778
61 328
62 38
63 212
64 843
65 288
66 136
67 49
68 950
69 283
70 670
71 473
72 828
73 905
74 735
75 394
76 365
77 21
78 132
79 417
80 551
81 648
82 635
83 108
84 973
85 774
86 851
87 970
88 383
89 944
90 334
91 960
92 471
93 650
94 334
95 542
96 559
97 134
98 84
99 951
100 557
101 837
102 146
103 643
104 687
105 726
106 939
107 990
108 308
109 704
110 408
111 26
112 773
113 950
114 91
115 276
116 834
117 803
118 588
119 102
120 528
121 10
122 303
123 170
124 654
125 377
126 791
127 678
128 589
129 35
130 64
131 377
132 151
133 957
134 745
135 979
136 433
137 138
138 221
139 25
140 348
141 472
142 299
143 780
144 393
145 959
146 917
147 241
148 767
149 245
150 606
151 428
152 970
153 533
154 43
155 429
156 197
157 900
158 623
159 780
160 656
161 427
162 365
163 651
164 557
165 569
166 489
167 622
168 45
169 605
170 374
171 301
172 866
173 383
174 31
175 600
176 45
177 375
178 222
179 687
180 508
181 289
182 738
183 53
184 1
185 444
186 965
187 906
188 791
189 145
190 467
191 731
192 907
193 672
194 505
195 824
196 423
197 324
198 623
199 835
200 523
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 24600) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
If this was up to me I would just be passing the filename through to the functions as a string param and making new ifstreams in these functions to get the thing to work, however the requirement for this is to have ad least one function that passes an ifstream through reference and utilize it. however after lots of troubleshooting and reading stackexange posts on the topic I cannot get it to function as expected.
what am I doing wrong?
any help would be appreciated.
I am getting error while executing my C++ code
Error is:
The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning : Segmentation fault
IDE : QT Creator
I know there is something wrong with the way I have declared the 2 dimentional array of structs in header file
But don't know exactly what!
Can someone please explain..
Please note, I plan to create multiple instances of Datamatrix class as part of my work in a recursive or iterative manner.
Each instance containing 2 dimensional array of structs..
So Any help explanation is welcome.
There are many areas of C++ which I have not explored! still learning :)
Please see the code below
main.cpp
----------
#include <iostream>
#include "datamatrix.h"
#include "utils.h"
using namespace std;
void printArray(int items[],int size)
{
for(int j=0;j<size;j++)
{
cout<<items[j]<<" ";
}
cout<<endl;
}
int main()
{
cout << "Hello there" << endl;
int a[250];
for(int i=0;i<250;i++)
{
a[i] = i+1;
}
printArray(a,250);
DataMatrix *matrix = new DataMatrix();
Utils u;
u.myFunction(matrix); **<-- All goes well till here**
cout<<"End of Main method"; **<-- This line of code is never reached**
return 0;
}
datamatrix.h
------------
#ifndef DATAMATRIX_H
#define DATAMATRIX_H
#include <string>
using namespace std;
struct KVPair { **<--- This is the line where issue comes**
string key;
double value;
};
class DataMatrix
{
public:
DataMatrix();
KVPair matrix[250][130];
};
#endif // DATAMATRIX_H
datamatrix.cpp
--------------
#include "datamatrix.h"
DataMatrix::DataMatrix()
{
for(int i=0;i<250;i++)
{
for(int j=0;j<130;j++)
{
string k = "f"+i+1;
matrix[i][j].key = k;
matrix[i][j].value = i+5;
}
}
}
utils.h
--------
#ifndef UTILS_H
#define UTILS_H
#include "datamatrix.h"
class Utils
{
public:
Utils();
DataMatrix myFunction(DataMatrix* dm);
};
#endif // UTILS_H
utils.cpp
---------
#include "utils.h"
#include <algorithm>
#include <time.h>
#include <math.h>
#include "datamatrix.h"
Utils::Utils()
{
}
DataMatrix Utils::myFunction(DataMatrix* dm)
{
printf("inside myFunction ");
}
Just thought to add console output as well(please see console output below)
Hello there
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
248 249 250
inside myFunction
Thanks in advance
The following line:
string k = "f"+i+1;
Causes the issue. It takes the address far beyond the string literal "f", which is an invalid address, and passes that to std::string constructor, which reads from that address to determine the length of the string and causes the crash.
One way to fix would be:
string k = "f" + std::to_string(i + 1);
Eliminating the one warning from gcc -Wall prevents the crash. One way to do that is to add a return value in Utils::myFunction, as follows.
DataMatrix Utils::myFunction(DataMatrix* dm)
{
printf("inside myFunction ");
return *dm;
}
It appears that main allocates space for a DataMatrix on the stack with the expectation that the call to Utils::myFunction will populate that space. The following gdb session excerpt on a binary with the added return statement in Utils::myFunction reinforces that claim.
(gdb) break main.cpp:30
Breakpoint 1 at 0xfaa: file main.cpp, line 30.
(gdb) run > /dev/null
Starting program: main > /dev/null
Breakpoint 1, main () at main.cpp:30
30 u.myFunction(matrix);
(gdb) print matrix
$1 = (DataMatrix *) 0x7ffff7e95010
(gdb) set $matrix = (DataMatrix *) ($rbp - 0x13d640)
(gdb) print matrix.matrix[249][129].key
$2 = "\030J\017\vw\b\200"
(gdb) print $matrix.matrix[249][129].key
$3 = <incomplete sequence \303>
(gdb) next
31 cout<<"End of Main method";
(gdb) print $matrix.matrix[249][129].key
$4 = "\030J\017\vw\b\200"
(gdb)
In my testing, the crash occurs during an attempt to deallocate an uninitialized KVPair consisting of arbitrary stack contents. The details of that are not revealed in the preceding gdb session excerpt, but the point is that after Utils::myFunction returns a value other than void, the two copies of the DataMatrix agree about the contents of the last array element (the first one processed in the destructor, in my testing). The following excerpt from gdb disassembly explains the $rbp offset used for $matrix.
0x0000555555554fc7 <+183>: lea -0x13d640(%rbp),%rax
0x0000555555554fce <+190>: mov %rax,%rdi
0x0000555555554fd1 <+193>: callq 0x555555555092 <DataMatrix::~DataMatrix()>
Another way to prevent the crash is to declare Utils::myFunction as returning void, in which case main does not allocate space on the stack, and there is no call to the DataMatrix destructor within main.
I have two similar C++ implementations of Prim's algorithm for a minimum spanning tree, but one of them works in every situation, while the other fails in some. The difference is the method of comparison used within the priority queue.
This is the one that fails in a few situations (it uses a struct function in order to compare the cost of given arches):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>``
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue<int, vector <int>, comp> pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push(1);
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top();
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] >= c)
{
C[v] = c;
pq.push(v);
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
And this is the one that succeeds every time (it uses the greater within the priority queue and stores both the cost associated with a node and the node itself within the queue):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
typedef pair<int, int> iPair;
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] < C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue< pair<int, int>, vector <pair<int, int> > , greater<pair<int, int> > > pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push((make_pair(0, 1)));
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top().second;
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] > c)
{
C[v] = c;
pq.push(make_pair(C[v], v));
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
Why doesn't the first one work as intended every time and how would I go about changing it while still using the struct function and the code basis I already have? I tried a similar implementation to the one I use for Dijkstra's algorithm.
The program reads the number of nodes and arches and all the given arches from a file and it's supposed to show the minimum cost of the spanning tree, the number of arches and the arches in any order.
The examples I have that fail for the 1st implementation are of pretty abnormal sizes, I don't know if they will be of any help:
apm.in:
164 531
155 74 113
73 15 817
38 87 -153
92 100 699
125 114 -210
5 50 -29
1 4 335
11 39 846
112 59 -745
157 86 -146
92 47 397
146 48 -614
81 123 539
8 36 -799
39 17 175
55 36 -133
14 129 809
134 107 948
153 97 -428
144 73 -975
31 41 293
76 95 -550
111 96 -906
132 54 35
19 36 -627
142 31 -949
127 52 -363
44 17 -913
141 69 36
163 2 -610
17 12 -187
65 41 941
160 39 931
69 127 966
98 128 528
144 145 354
49 86 -201
7 102 569
113 31 -151
155 139 752
13 94 -659
140 69 672
32 8 -945
72 19 -592
104 68 -631
5 87 -760
37 58 -211
32 74 134
88 60 622
128 77 -480
4 20 151
38 66 823
133 41 886
128 58 -522
115 68 403
62 128 379
24 156 3
152 22 -630
126 160 322
61 16 852
57 69 -650
126 99 371
87 54 -659
101 93 -44
4 22 899
128 16 222
96 132 -523
67 74 993
143 110 -346
128 9 -892
146 86 -773
14 8 306
114 156 116
91 77 -581
44 95 978
89 38 609
147 139 -486
67 34 -952
119 36 829
44 90 653
1 164 -197
77 157 -273
7 130 606
3 143 701
37 91 363
44 18 -814
53 34 -170
124 39 -384
38 80 -51
72 4 30
133 59 -53
121 10 676
20 142 967
159 84 -456
118 110 75
68 75 828
154 91 -83
94 128 -740
22 72 269
1 81 -371
58 154 98
73 146 -240
11 6 -254
80 62 -70
72 156 611
119 118 990
109 23 -67
103 97 -281
6 135 388
116 159 -948
1 50 -551
31 60 -577
52 47 -514
164 91 562
113 112 178
45 106 -667
160 22 952
17 156 748
125 34 36
94 93 440
139 117 890
43 144 -179
91 60 346
85 96 853
118 156 -991
67 107 718
60 89 649
17 28 -105
64 47 666
26 136 -174
31 147 835
79 115 -235
106 66 693
8 74 107
162 136 712
83 116 506
82 139 489
110 114 -299
96 69 -94
19 129 -361
30 129 612
122 91 -571
62 2 -318
38 143 662
117 142 -34
91 124 385
112 50 -460
14 81 405
129 145 227
71 106 -774
27 15 -339
157 85 251
146 84 726
127 46 -711
95 134 175
134 51 103
31 154 661
95 150 56
164 27 -845
31 127 -452
46 29 -264
74 102 -477
72 39 320
89 23 -811
12 83 -672
68 136 194
82 90 -326
52 81 819
109 47 -204
148 24 870
146 141 148
83 158 -198
77 9 -351
64 25 575
114 28 -992
29 139 630
21 130 -979
153 104 611
80 162 217
155 74 768
2 51 -643
75 97 -706
124 62 -69
36 135 -181
100 43 -163
146 129 54
126 106 799
15 155 907
125 153 658
85 51 702
112 95 80
149 84 -782
46 105 859
69 73 280
98 150 11
60 93 -997
82 134 -786
20 40 -166
134 90 -810
93 76 893
100 51 -161
43 92 314
162 28 525
121 141 -953
30 58 991
97 100 -890
54 23 314
63 92 -480
97 155 -736
29 136 -817
64 86 -424
86 53 613
96 38 -4
57 65 -789
8 6 -586
18 145 -959
136 19 -497
74 86 -895
7 155 -281
43 6 -814
43 69 361
134 141 257
67 46 -326
111 73 -249
11 13 -581
98 143 -909
133 78 -551
83 25 325
93 162 -503
138 31 -386
161 92 135
84 79 -80
12 108 41
86 158 109
71 3 334
27 62 -517
59 15 613
46 63 -316
74 39 996
37 11 -438
38 102 369
15 129 313
131 6 -841
152 109 -529
124 53 878
35 78 -994
116 117 -312
47 109 -12
47 36 534
84 63 -626
152 142 667
87 79 -201
52 22 -518
99 103 -434
139 53 262
162 1 -524
120 69 836
131 93 368
155 135 5
133 117 -818
59 15 854
37 139 931
111 8 -15
92 58 411
126 19 789
111 12 763
66 147 605
20 142 -446
156 163 -566
137 36 -350
127 156 -307
49 22 376
124 24 -863
36 161 175
63 27 -687
5 29 -375
27 69 -260
24 149 447
141 117 32
58 64 239
87 149 -919
65 101 -807
127 117 237
145 44 -731
32 74 535
142 13 -869
49 5 315
152 105 -747
10 133 -232
69 98 -253
134 86 458
129 48 -322
121 132 46
64 15 -216
103 38 -530
63 17 -871
69 38 -577
55 106 141
111 90 583
138 100 -478
67 43 -608
141 88 921
45 55 596
155 91 338
128 3 532
28 124 -839
134 101 240
36 41 -888
3 156 -139
79 151 446
41 150 978
160 3 70
5 45 -597
89 103 -849
123 56 -820
16 123 779
114 89 -534
160 147 -359
52 140 239
142 80 66
124 66 -18
89 23 945
162 108 -353
151 143 -912
68 121 656
23 122 730
17 8 -81
87 130 44
116 158 -234
16 123 430
66 130 -398
29 65 -844
105 43 -854
84 99 -730
107 162 729
128 153 175
146 13 538
135 43 -753
93 123 996
23 3 -436
45 133 -203
151 132 931
49 159 -358
147 120 -274
132 131 -192
94 141 -758
19 153 946
155 33 284
85 18 646
69 148 -720
142 125 965
80 63 -96
29 140 -129
116 50 -111
38 124 -750
156 102 -674
39 67 -459
50 150 -261
110 29 904
11 83 -520
58 65 449
34 144 -362
103 76 567
97 85 322
151 76 118
61 58 -636
7 143 535
61 26 40
10 57 -155
120 33 -871
28 53 -176
57 86 -602
161 92 -96
69 151 555
49 17 876
43 71 -91
47 118 -191
70 49 576
102 139 -920
60 153 873
80 124 222
30 20 -147
37 158 587
65 9 314
46 69 957
117 16 -831
74 106 -756
95 92 -222
52 147 310
2 61 -427
138 21 -256
113 94 273
162 105 -53
40 80 572
21 143 386
154 115 229
97 126 362
106 40 -164
60 100 -405
29 109 -506
133 35 -867
114 70 -169
132 46 -145
161 133 434
94 47 -939
86 91 543
110 155 -277
20 162 -683
32 145 304
135 124 -23
5 123 283
11 65 -858
31 128 223
54 97 585
8 93 688
91 58 -974
123 97 931
140 61 822
19 13 69
161 162 976
126 115 887
47 84 -502
12 157 -824
135 98 420
114 6 -605
66 138 -354
70 138 -596
56 87 145
37 159 -207
78 136 -573
49 57 271
154 14 859
103 139 -848
32 105 -927
163 126 -935
49 19 262
14 108 408
164 145 -612
37 135 -454
68 20 892
78 164 470
24 109 -598
70 53 -403
71 148 -983
92 46 851
26 34 -602
88 47 -667
66 59 750
142 99 319
43 57 967
109 17 654
85 108 -947
74 33 694
150 83 -845
129 7 -37
65 70 -668
4 102 771
158 69 -615
85 148 225
12 55 172
7 41 -83
123 103 937
110 137 -36
42 131 -392
153 18 -844
86 94 -404
77 44 -307
18 114 -372
75 115 99
53 4 -472
140 159 -844
46 67 -991
84 162 599
91 106 406
149 108 -811
142 22 -653
136 44 496
131 43 -2
122 37 375
103 109 -692
26 58 -822
84 128 -967
71 53 -162
74 18 -123
145 103 722
65 99 -961
56 15 -908
44 147 176
41 133 -145
96 63 915
35 20 -106
122 73 -845
11 150 917
5 105 -361
119 44 -261
27 76 215
129 11 -102
157 140 338
160 120 958
28 131 -194
36 106 -468
130 52 679
16 76 -514
151 156 236
2 137 -335
112 80 724
35 78 -535
44 121 119
62 59 -379
110 141 780
52 37 342
29 33 -49
89 7 828
87 131 171
44 130 419
86 45 438
103 128 558
24 129 -565
86 99 -758
51 110 -571
117 94 476
61 78 -875
127 101 -359
146 40 -871
87 48 -600
97 149 -165
3 58 507
88 37 -764
51 43 918
41 76 10
13 88 -835
apm.ok:
-105854
163
60 93
35 78
114 28
46 67
118 156
71 148
21 130
144 73
91 58
84 128
65 99
18 145
121 141
67 34
142 31
116 159
85 108
32 8
94 47
163 126
32 105
102 139
87 149
44 17
151 143
98 143
56 15
111 96
74 86
128 9
97 100
36 41
61 78
120 33
63 17
146 40
142 13
133 35
124 24
11 65
105 43
89 103
103 139
150 83
164 27
122 73
140 159
153 18
29 65
131 6
28 124
13 88
117 16
12 157
26 58
123 56
133 117
29 136
43 6
44 18
89 23
149 108
134 90
65 101
8 36
57 65
82 134
149 84
71 106
146 86
88 37
5 87
94 141
86 99
74 106
135 43
38 124
152 105
112 59
94 128
97 155
84 99
69 148
127 46
75 97
103 109
63 27
20 162
156 102
12 83
65 70
45 106
88 47
87 54
142 22
2 51
61 58
104 68
152 22
19 36
84 63
158 69
146 48
163 2
67 43
114 6
26 34
24 109
70 138
72 19
91 77
31 60
122 91
51 110
156 163
24 129
1 50
76 95
162 1
96 132
11 83
52 22
27 62
16 76
93 162
147 139
63 92
138 100
53 4
112 50
39 67
159 84
23 3
64 86
70 53
66 130
42 131
1 81
160 147
49 159
66 138
137 36
143 110
27 15
7 155
147 120
119 44
111 73
79 115
10 133
125 114
87 79
113 31
30 20
55 36
80 63
161 92
154 91
134 51
68 136
14 8
83 25
67 107
apm.out:
-105439
163
2 163
3 23
4 53
5 87
6 43
7 155
8 32
9 128
10 133
11 13
12 83
13 142
14 8
15 27
16 117
17 63
18 44
19 36
20 162
21 130
22 142
23 89
24 124
25 83
26 58
27 63
28 114
29 65
30 20
31 60
32 105
33 120
34 67
35 78
36 8
37 88
38 69
39 67
40 146
41 36
42 131
43 105
44 17
45 106
46 67
47 88
48 146
49 159
50 1
51 2
52 22
53 70
54 87
55 36
56 15
57 65
58 61
59 112
60 93
61 78
62 27
63 84
64 86
65 11
66 138
67 43
68 136
69 148
70 65
71 106
72 19
73 122
74 86
75 97
76 16
77 91
78 136
79 87
80 63
81 1
82 134
83 11
84 128
85 108
86 99
87 149
88 13
89 114
90 134
91 58
92 63
93 162
94 47
95 76
96 111
97 100
98 143
99 65
100 138
101 65
102 139
103 89
104 68
105 152
106 74
107 67
108 149
109 103
110 51
111 73
112 50
113 31
114 6
115 79
116 159
117 133
118 156
119 44
120 147
121 141
122 91
123 56
124 28
125 114
126 163
127 46
128 94
129 24
130 66
131 6
132 96
133 35
134 51
135 43
136 29
137 36
138 70
139 103
140 159
141 94
142 31
143 110
144 73
145 18
146 86
147 139
148 71
149 84
150 83
151 143
152 22
153 18
154 91
155 97
156 102
157 12
158 69
159 84
160 147
161 92
162 1
163 156
164 27
Every times you pop a node u from priority_queue, you need to update C[v] with v is adjacent node with u. These v may be already in the queue, so technically, you indirectly update the value of some elements in priority_queue (since value of an element is expressed via C[]). It will break the structure of the queue and lead to incorrect answer.
In the correct version, you push 2 int to queue, and these values never change. Therefore you do not get the above problem.
Looking through the exposed sample codes, I got a suspicion about an essential difference of comparison behaviors. To prove that suspicion, I put both versions of the sample codes into WinMerge and compared them. They look really similar. So, I'm quite sure I'm on the right track:
The first version uses this for comparison:
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
The second version uses this instead:
std::greater<pair<int, int> >
The big difference: std::greater considers both members of std::pair in comparison but the comparison function in first sample only C[x] and C[y] (but not x and y).
According to cppreference.com
template< class T1, class T2 >
constexpr bool operator>(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
is defined as rhs < lhs and
template< class T1, class T2 >
constexpr bool operator<(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
does
If lhs.first<rhs.first, returns true. Otherwise, if rhs.first<lhs.first, returns false. Otherwise, if lhs.second<rhs.second, returns true. Otherwise, returns false.
So, if the first comparison shall resemble the second, the comparison functor of first sample has to be changed to e.g.:
struct comp{
bool operator()(int x, int y)
{
return x == y ? C[x] > C[y] : x > y;
}
};
That looks a bit stupid as x == y surely results in C[x] == C[y]. However, it should return the exact same results as the std::greater<std::pair<int, int> > for the second sample code.
Concerning the original problem of the OP, the first version may actually be the correct one (and there is yet another mistake or a wrong expectation about output). In this case, the comparison of second sample had to be adjusted, e.g. introducing a resp. functor for std::pair:
struct comp{
bool operator()(
const std::pair<int, int> &xCx, const std::pair<int, int> &yCy) const
{
return xCx.second > yCy.second;
}
};
As I do not know Prim's algorithm, I cannot decide which one is the actually correct solution. The OP might leave a note regarding this (and I'm would be glad to edit this in).
I have an input in format as shown in below Image.I am using the vector graph[200] to read input. Let's suppose program read first number of first row and column (as shown in Image) . Now I want to add all vertices which is connected to first node like graph[1].push_back(next integer in same row) and so on . But when should I stop reading input for particular Node(graph[i]). Because after reading first row I need to add vertices into another graph[i+1] to node's corresponding list . If you don't understand my questions plz have a look to my code.
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[201];
int main()
{
int n=2,i,node,temp;
for(i=1;i<=n;i++)
{
cin>>node;
while(scanf("%d",&temp)!=EOF/*What is the correct conditon to stop loop*/)
{
graph[node].push_back(temp);
}
}
return 0;
}
Input format :
1 37 79 164 155 32 87 39 113 15 18 78 175 140 200 4 160 97 191 100 91 20 69 198 196
20 123 134 10 141 13 12 43 47 3 177 101 179 77 182 117 116 36 103 51 154 162 128 30
3 48 123 134 109 41 17 159 49 136 16 130 141 29 176 2 190 66 153 157 70 114 65 173 104 194 54
14 91 171 118 125 158 76 107 18 73 140 42 193 127 100 84 121 60 81 99 80 150 55 1 35 23 93
5 193 156 102 118 175 39 124 119 19 99 160 75 20 112 37 23 145 135 146 73 35
60 155 56 52 120 131 160 124 119 14 196 144 25 75 76 166 35 87 26 20
7 156 185 178 79 27 52 144 107 78 22 71 26 31 15 56 76 112 39 8 113 93
8 185 155 171 178 108 64 164 53 140 25 100 133 9 52 191 46 20 150 144 39 62 131 42 119 127 31 7
9 91 155 8 160 107 132 195 26 20 133 39 76 100 78 122 127 38 156 191 196
10 190 184 154 49 2 182 173 170 161 47 189 101 153 50 30 109 177 148 179 16 163 116 13 90 185
111 123 134 163 41 12 28 130 13 101 83 77 109 114 21 82 88 74 24 94 48 33
12 161 109 169 21 24 36 65 50 2 101 159 148 54 192 88 47 11 142 43 70 182 177 179 189 194 33
13 161 141 157 44 83 90 181 41 2 176 10 29 116 134 182 170 165 173 190 159 47 82 111 142 72 154 110 21 103 130 11 33 138 152
and so on...
Here is the screenshot of my input format
Assuming you want to read line-by-line basis, you could do following:
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[201];
int main(){
string line;
int n=2,i,node,temp;
for(i=1;i<=n;i++){
getline(cin, line);
istringstream in( line );
in>>node;
while(in>>temp){
graph[node].push_back(temp);
}
}
return 0;
}
When "text" is larger than about 280 numbers the program waits ...
With any "text" of 280 numbers, it works fine.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text;
getline (cin, text);
cout << text;
}
eg:
167 214 280 265 278 292 196 249 242 297 7 125 151 4 25 172 293 157 290 277 240 155 201 90 44 230 94 185 184 65 189 159 74 30 59 279 169 136 142 80 46 124 66 203 138 182 171 241 267 294 32 233 165 39 149 181 156 170 137 96 130 238 239 37 298 48 288 6 100 174 268 144 272 109 275 190 160 154 57 15 83 16 183 236 95 97 147 215 77 34 219 91 68 81 52 207 187 105 229 153 243 20 71 53 3 102 259 13 115 123 98 193 87 208 120 221 113 261 126 178 111 133 255 36 287 93 228 263 47 227 188 191 295 205 28 82 244 152 281 166 58 192 162 60 256 76 50 179 235 247 282 118 88 212 112 21 273 141 222 56 209 134 237 2 121 104 23 150 194 146 24 300 64 92 78 79 116 108 286 223 70 61 67 284 19 33 173 216 42 164 29 199 63 69 140 132 211 101 103 119 106 198 296 168 224 158 232 27 254 246 262 110 250 225 135 86 26 51 180 231 114 257 75 202 217 251 218 18 89 213 85 220 117 266 206 127 234 197 291 248 14 258 129 226 148 260 84 204 73 299 31 264 276 107 11 145 1 54 200 49 72 177 62 45 163 271 274 270 195 186 252 139 99 55 41 38 253 285 5 176 283 22 122 161 17 175 131 43 289 269 9 40 245 12 10 143 35 210 128 8
The only reason I could imagine your code is pausing is if you were overfilling the space allocated for a string. This seems extremely unlikely. There is a limit to the maximum size for a string based on the size of size_t but typically 32 bits are allocated for size_t... This means that the max number of characters for a string is typically 2^32 - 1 characters. Obviously, you are nowhere near reaching that length.
In normal circumstances, you are far more likely to be bound by the amount of space that your system can allocate for the string. This is dependent on the amount of memory available and how your system breaks it up.
Both of these situations seem extremely unlikely.
In this situation, the issue is most likely an issue with your compiler or platform...
What's your reason for taking in such a long string? Have you considered other approaches like reading in the integers from a text file? You could use a vector to store the integers in a more organized way by reading them in individually from the text file, just a thought.