c++ recursion with object as function parameter - c++

I'm trying to find all paths through a given network of activities and thought that I could do so using recursion. It is much like traversing through a network drive with different folders and sub-folders.
I have defined an activity class (for storing each activity's properties) and a network class (for storing the network's activities). I'm passing the network object as a parameter to the recursive function call. The recursive function does not work as expected. It gives a segmentation fault and seems not to be able to access the network object properly.
Can someone point me in the right direction on how to solve this (and how I could also store the found paths in a 2 dimensional array).
Here is the code:
#include <iostream>
using namespace std;
class Activity {
public:
int id;
double duration;
int successors[10];
};
class Network {
public:
Activity activities[];
};
int traverse(int, Network);
int main()
{
// define the set of successors for each activity
// a value of -1 indicates that no more successors are present
const int successorset[][5] = {
{ 1,2,3,-1 },
{ 5,-1 },
{ 4,-1 },
{ 4,-1 },
{ 5,-1 },
{ -1},
};
// two dimensional array where for each path the activity ids are stored
int pathset [10][10];
// declare activities
Activity Activity0, Activity1, Activity2, Activity3, Activity4, Activity5;
// declare network
Network NetworkMain;
// define activity 0
Activity0.id = 0;
Activity0.duration = 0;
Activity0.successors[0] = successorset[0][0];
Activity0.successors[1] = successorset[0][1];
Activity0.successors[2] = successorset[0][2];
Activity0.successors[3] = successorset[0][3];
NetworkMain.activities[0] = Activity0;
// define activity 1
Activity1.id = 1;
Activity1.duration = 3;
Activity1.successors[0] = successorset[1][0];
Activity1.successors[1] = successorset[1][1];
NetworkMain.activities[1] = Activity1;
// define activity 2
Activity2.id = 2;
Activity2.duration = 1;
Activity2.successors[0] = successorset[2][0];
Activity2.successors[1] = successorset[2][1];
NetworkMain.activities[2] = Activity2;
// define activity 3
Activity3.id = 3;
Activity3.duration = 2;
Activity3.successors[0] = successorset[3][0];
Activity3.successors[1] = successorset[3][1];
NetworkMain.activities[3] = Activity3;
// define activity 4
Activity4.id = 4;
Activity4.duration = 4;
Activity4.successors[0] = successorset[4][0];
Activity4.successors[1] = successorset[4][1];
NetworkMain.activities[4] = Activity4;
// define activity 5
Activity5.id = 5;
Activity5.duration = 0;
Activity5.successors[0] = successorset[5][0];
NetworkMain.activities[5] = Activity5;
// print info on all activities to check whether they are defined correctly
for( int a = 0; a < 6; a++ ) {
cout << "id of activity = " << NetworkMain.activities[a].id << endl;
cout << "duration of activity = " << NetworkMain.activities[a].duration << endl;
int s = 0;
while (NetworkMain.activities[a].successors[s]!=-1) {
cout << "successor of activity " << a << " = " << NetworkMain.activities[a].successors[s] << endl;
s++;
}
}
// call recursive function to traverse through all paths of the network
traverse(0, NetworkMain);
return 0;
}
int traverse(int id, Network net)
{
if (net.activities[id].successors[0]==-1) // reached finish activity
{
cout << "reached finish " << endl;
return 1;
}
else
{
cout << "id of activity under investigation " << net.activities[id].id << endl;
int t = 0;
while (net.activities[id].successors[t]!=-1) {
cout << "going to investigate successor " << t << endl;
traverse(net.activities[id].successors[t], net);
t++;
}
}
}
Here is the output:
id of activity = 0
duration of activity = 0
successor of activity 0 = 1
successor of activity 0 = 2
successor of activity 0 = 3
id of activity = 1
duration of activity = 3
successor of activity 1 = 5
id of activity = 2
duration of activity = 1
successor of activity 2 = 4
id of activity = 3
duration of activity = 2
successor of activity 3 = 4
id of activity = 4
duration of activity = 4
successor of activity 4 = 5
id of activity = 5
duration of activity = 0
id of activity under investigation -578861888
going to investigate successor 0
id of activity under investigation 0
going to investigate successor 0
id of activity under investigation -581127744
going to investigate successor 0
Segmentation fault (core dumped)
Process returned 139 (0x8B) execution time : 0.087 s
Press ENTER to continue.
Edit: should have added the newbie alert, below is the debugger output.
Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: /home/home/Desktop/recursion/
Adding source dir: /home/home/Desktop/recursion/
Adding file: /home/home/Desktop/recursion/bin/Debug/recursion
Changing directory to: /home/home/Desktop/recursion/.
Set variable: LD_LIBRARY_PATH=.:
[debug]Command-line: /usr/bin/gdb -nx -fullname -quiet -args /home/home/Desktop/recursion/bin/Debug/recursion
[debug]Working dir : /home/home/Desktop/recursion
Starting debugger: /usr/bin/gdb -nx -fullname -quiet -args /home/home/Desktop/recursion/bin/Debug/recursion
done
[debug]Reading symbols from /home/home/Desktop/recursion/bin/Debug/recursion...done.
[debug](gdb)
[debug]> set prompt >>>>>>cb_gdb:
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
[debug]>>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
[debug]Copyright (C) 2016 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law. Type "show copying"
[debug]and "show warranty" for details.
[debug]This GDB was configured as "x86_64-linux-gnu".
[debug]Type "show configuration" for configuration details.
[debug]For bug reporting instructions, please see:
[debug]<http://www.gnu.org/software/gdb/bugs/>.
[debug]Find the GDB manual and other documentation resources online at:
[debug]<http://www.gnu.org/software/gdb/documentation/>.
[debug]For help, type "help".
[debug]Type "apropos word" to search for commands related to "word".
[debug]>>>>>>cb_gdb:
[debug]> set confirm off
Debugger name and version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 0
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor intel
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source /usr/share/codeblocks/scripts/stl-views-1.0.3.gdb
[debug]>>>>>>cb_gdb:
[debug]> directory /home/home/Desktop/recursion/
[debug]Source directories searched: /home/home/Desktop/recursion:$cdir:$cwd
[debug]>>>>>>cb_gdb:
[debug]Using terminal's PID as console PID 13148, TTY /dev/pts/1
[debug]> tty /dev/pts/1
[debug]Queued:[tty /dev/pts/1]
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: /home/home/Desktop/recursion/bin/Debug/recursion
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
[debug]/home/home/Desktop/recursion/main.cpp:103:2808:beg:0x400ee9
[debug]>>>>>>cb_gdb:
Program received signal SIGSEGV, Segmentation fault.
At /home/home/Desktop/recursion/main.cpp:103
[debug]> bt 30
[debug]#0 0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
[debug]#1 0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#2 0x0000000000400ff1 in traverse (id=6, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#3 0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
[debug]#4 0x0000000000400e91 in main () at /home/home/Desktop/recursion/main.cpp:96
[debug]>>>>>>cb_gdb:

Seemingly a newbie mistake, I had to declare the size of the activities array to make room for it in the computer's memory.
Incorrect:
class Network {
public:
Activity activities[];
};
Correct:
class Network {
public:
Activity activities[6]; // given 6 activities
};
Thanks all for your help.

Related

C++ and ANTLR4 : Handling "include" directives in lexer gives segfault in C++ runtime library

I need to handle include directives, similar to standard C/C++ preprocessor.
I already found this solution (in C#), based on changing lexer input stream: C# and ANTLR4: Handling "include" directives when parsing a file so I did the same in C++, but I get a segfault in antlr4 CPP runtime library.
Here is the very basic reproducer, based on CPP runtime demo provided example:
Modify parsed string to embbed an include directive:
diff --git a/runtime/Cpp/demo/Linux/main.cpp b/runtime/Cpp/demo/Linux/main.cpp
index 672ce2a3b..4e44347fb 100644
--- a/runtime/Cpp/demo/Linux/main.cpp
+++ b/runtime/Cpp/demo/Linux/main.cpp
## -20,7 +20,7 ## using namespace antlrcpptest;
using namespace antlr4;
int main(int , const char **) {
- ANTLRInputStream input(u8"๐Ÿด = ๐Ÿ + \"๐Ÿ˜Ž\";(((x * ฯ€))) * ยต + โˆฐ; a + (x * (y ? 0 : 1) + z);");
+ ANTLRInputStream input(u8"๐Ÿด = ๐Ÿ + \"๐Ÿ˜Ž\"; #include \"test.txt\"");
TLexer lexer(&input);
CommonTokenStream tokens(&lexer);
Create the included file that contains the rest of the string:
$ cat test.txt
(((x * ฯ€))) * ยต + โˆฐ; a + (x * (y ? 0 : 1) + z);
Add support for include directive in the provided lexer:
diff --git a/runtime/Cpp/demo/TLexer.g4 b/runtime/Cpp/demo/TLexer.g4
index ac2128c8d..26d70e4ea 100644
--- a/runtime/Cpp/demo/TLexer.g4
+++ b/runtime/Cpp/demo/TLexer.g4
## -3,7 +3,10 ## lexer grammar TLexer;
// These are all supported lexer sections:
// Lexer file header. Appears at the top of h + cpp files. Use e.g. for copyrights.
-#lexer::header {/* lexer header section */}
+#lexer::header {/* lexer header section */
+ #include <iostream>
+ #include <stack>
+}
// Appears before any #include in h + cpp files.
#lexer::preinclude {/* lexer precinclude section */}
## -21,6 +24,16 ## lexer grammar TLexer;
// Appears in the public part of the lexer in the h file.
#lexer::members {/* public lexer declarations section */
+std::stack<antlr4::CharStream *> input_stack;
+virtual antlr4::Token *emitEOF() override {
+ if (input_stack.empty()) {
+ return Lexer::emitEOF();
+ };
+ hitEOF = false;
+ setInputStream(input_stack.top());
+ input_stack.pop();
+ return nextToken().get();
+}
bool canTestFoo() { return true; }
bool isItFoo() { return true; }
bool isItBar() { return true; }
## -69,6 +82,12 ## Comma: ',' -> skip;
Dollar: '$' -> more, mode(Mode1);
Ampersand: '&' -> type(DUMMY);
+fragment SPACES : [ \t]+ ;
+INCLUDE : '#include' SPACES '"' {
+ std::cerr << "Got include directive " << getSourceName() << "\n";
+ std::cerr << "Current mode = " << mode << "\n";
+ } -> skip, pushMode(INCLUDEHANLDING);
+
String: '"' .*? '"';
Foo: {canTestFoo()}? 'foo' {isItFoo()}? { myFooLexerAction(); };
Bar: 'bar' {isItBar()}? { myBarLexerAction(); };
## -84,3 +103,36 ## Dot: '.';
mode Mode2;
DotDot: '..';
+
+mode INCLUDEHANLDING;
+// Skipped to hide FILE token to parser
+FILE : ~["]+ {
+ {
+ // Create new input stream from the file mentioned
+ std::ifstream stream(getText());
+ if (stream.fail()) {
+ std::cerr << "Config error: " << std::strerror(errno) << " for "<< getText() << "\n";
+ } else {
+ // Push the old stream to stack
+ input_stack.push(getInputStream());
+ std::cerr << "Handling open file. mode = " << mode << "\n";
+ // This new stream will be popped and used right after, on DQUOTE.
+ input_stack.push(new ANTLRInputStream(stream));
+ }
+ }
+} -> skip;
+
+// Skipped to hide DQUOTE token to parser
+DQUOTE: '"' {
+ // Injecting the newly generated Stream.
+ std::cerr << "Current mode = " << mode << "\n";
+ setInputStream(input_stack.top());
+ input_stack.pop();
+ std::cerr << "Injected stream. Now reading from " << getSourceName() << "\n";
+ std::cerr << "Current mode = " << mode << "\n";
+} -> skip;
+
+NL : ('\r'? '\n' | '\r')+ -> skip;
Recompile the demo example
make
Run it and you get a segfault:
Got include directive <unknown>
Current mode = 0
Handling open file. mode = 3
Current mode = 3
Injected stream. Now reading from <unknown>
Current mode = 0
Segmentation fault (core dumped)
Debugging with gdb:
I recompiled the runtime library with debug support.
gdb demo/antlr4-demo
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from demo/antlr4-demo...
(gdb) r
Starting program: /home/adrpes01/work/antlr/antlr4/runtime/Cpp/build/demo/antlr4-demo
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Got include directive <unknown>
Current mode = 0
Handling open file. mode = 3
Current mode = 3
Injected stream. Now reading from <unknown>
Current mode = 0
Program received signal SIGSEGV, Segmentation fault.
antlr4::atn::LexerATNSimulator::failOrAccept (this=0x55555570c7b0, input=0x7fffffffd9b0, reach=0x555555712fc0, t=18446744073709551615)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp:213
213 return _prevAccept.dfaState->prediction;
(gdb) where
#0 antlr4::atn::LexerATNSimulator::failOrAccept (this=0x55555570c7b0, input=0x7fffffffd9b0, reach=0x555555712fc0, t=18446744073709551615)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp:213
#1 0x00005555555ca9d9 in antlr4::atn::LexerATNSimulator::execATN (this=0x55555570c7b0, input=0x7fffffffd9b0, ds0=0x555555712ab0)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp:167
#2 0x00005555555ca58c in antlr4::atn::LexerATNSimulator::match (this=0x55555570c7b0, input=0x7fffffffd9b0, mode=3)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp:76
#3 0x000055555558ea88 in antlr4::Lexer::nextToken (this=0x7fffffffdb00) at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/Lexer.cpp:80
#4 0x00005555555887d4 in antlr4::BufferedTokenStream::fetch (this=0x7fffffffd950, n=1000)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/BufferedTokenStream.cpp:96
#5 0x000055555558a812 in antlr4::BufferedTokenStream::fill (this=0x7fffffffd950)
at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/BufferedTokenStream.cpp:404
#6 0x000055555556a400 in main () at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/demo/Linux/main.cpp:27
(gdb) l
208 }
209
210 size_t LexerATNSimulator::failOrAccept(CharStream *input, ATNConfigSet *reach, size_t t) {
211 if (_prevAccept.dfaState != nullptr) {
212 accept(input, _prevAccept.dfaState->lexerActionExecutor, _startIndex, _prevAccept.index, _prevAccept.line, _prevAccept.charPos);
213 return _prevAccept.dfaState->prediction;
214 } else {
215 // if no accept and EOF is first char, return EOF
216 if (t == Token::EOF && input->index() == _startIndex) {
217 return Token::EOF;
(gdb) p _prevAccept
$1 = {index = 18446744073709551615, line = 0, charPos = 18446744073709551615, dfaState = 0x0}
(gdb)
Crash occurs at /home/adrpes01/work/antlr/antlr4/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp:213
213 return _prevAccept.dfaState->prediction;
... whish is weird, since _prevAccept.dfaState effectively seems null:
(gdb) p _prevAccept
$1 = {index = 18446744073709551615, line = 0, charPos = 18446744073709551615, dfaState = 0x0}
From what I understood, Lexer::reset() calls getInterpreteratn::LexerATNSimulator()->reset(), that itself call _prevAccept.reset();
Question is:
Am I doing something wrong that mess up ANTLR internal ?
Am I not doing soemthing that I should do ?
Or is it a real bug from CPP runtime ?
Thanks for your help

I am doing Unit Testing in QT test for c++, I am getting Fatal error?

The detailed problem is given here
void HostManagerTesting::TestAstmIsrGet()
{
cout<<"\n//*** "<<__func__<<" func called ***//\n"<<endl;
int iReturn;
cout<<"\n________________Case_1________________\n"<<endl;
Astm_obj.ut_set_AstmIsrGet(100);
iReturn=Astm_obj.AstmIsrGet(10,EOT);
QCOMPARE(iReturn,0);
cout<<"\n//*** "<<__func__<<" func Done ***//\n"<<endl;
}
I am testing this function , Astm_obj is an class object ,ut_set_AstmIsrGet(100) is function created by me for seting a global variable
void Astm::ut_set_AstmIsrGet(int m)
{
IsrStatus=m;
}
The Actual function I am testing is this
int Astm::AstmIsrGet ( int a, UCHAR uc )
{
SHORT sReport;
sReport = 0;
if (IsrStatus == ISR_IDLE)
{
if (uc == EOT || uc == ACK || uc == NAK || uc == ENQ)
{
IsrBufIn [0] = uc;
IsrBufInPos = 1;
sReport = 1;
cout<<"uc=eot";
goto EXIT;
}
}
EXIT:
if (sReport)
{
IsrBufInData = IsrBufInPos;
IsrStatus = ISR_WAIT;
/* Warns the ASTM automaton of the end of the rx */
sem_post ( &AstmSemRxEnded );
}
return( 0);
}
It should return 0 , But all I get is fatal error .
This is the error I am getting
**________________Case_1________________
=== Received signal at function time: 1ms, total time: 24ms, dumping stack ===
GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 24482
(gdb) QFATAL : HostManagerTesting::TestAstmIsrGet() Received signal 11
Function time: 0ms Total time: 24ms
FAIL! : HostManagerTesting::TestAstmIsrGet() Received a fatal error.
Loc: [Unknown file(0)]
Totals: 1 passed, 1 failed, 0 skipped, 0 blacklisted, 152ms
********* Finished testing of HostManagerTesting *********
=== End of stack trace ===
16:29:54: /home/chaithanya/Documents/QT/build-HostManagerTesting-Desktop-Debug/HostManagerTesting crashed.**
I feel that the problem exists in passing the arguments, am I passed it correctly , is there any other way to pass arguments ?

How to fix "A debugging check in this application failed" codeblocks?

I was working on a project in Codeblocks, but at one point when I wanted to Run it, the Debug window just remained empty.I thought that it was just an infinite thing, but while I was looking at it, it showed this, sayng that "A debugging check in this application failed".If I pressed "Continue" it would let me go and it would give me the result, but when I tried to debug the code, it said the same thing, and afterwards it wouldn't let me debug.
Here is the code:
#include <fstream>
using namespace std;
ifstream fin("forta.in");
ofstream fout("forta.out");
int main()
{
int p,n,maxx=0;
long long x,minn=2000000000;
fin>>p>>n;
if(p==1)
{
for(int i=0; i<n; i++)
{
fin>>x;
long long divt=1,p,cx=x,m=0;
long long frp[x+1], prim[x+1];
frp[0]=0;
frp[1]=0;
for(int i=2; i<=x; i++)
frp[i]=1;
for(int i=2; i<=x; i++)
{
if(frp[i]==1)
{
prim[m++] =i; ///stochez nr prime
for(int j=i+i; j<=x; j=j+i)
frp[j]=0;
}
}
for(int i=0; i<m ; i++) /// sirul de nr prime
{
p=0;
while(x%prim[i]==0)
{
p++;
x=x/prim[i];
}
divt=divt*(p+1);
}
if(divt>maxx)
{
maxx=divt;
minn=cx;
}
else if(divt==maxx)
{
if(cx<minn)
minn=cx;
}
}
fout<<minn;
}
if(p==2)
{
int fr[20000]= {0}, maxx=0;
for(int i=0; i<n; i++)
{
fin>>x;
long long divt=1,p,cx=x,m=0;
long long frp[x+1], prim[x+1];
frp[0]=0;
frp[1]=0;
for(int i=2; i<=x; i++)
frp[i]=1;
for(int i=2; i<=x; i++)
{
if(frp[i]==1)
{
prim[++m] =i; ///stochez nr prime
for(int j=i+i; j<=x; j=j+i)
frp[j]=0;
}
}
for(int i=0; i<m ; i++) /// sirul de nr prime
{
p=0;
while(x%prim[i]==0)
{
p++;
x=x/prim[i];
}
divt=divt*(p+1);
}
fr[divt]++; /// frecventa pe nr de div
if(maxx<fr[divt])
maxx=fr[divt];
}
fout<<maxx;
}
return 0;
}
It all goes well until "fout<<minn;"(line 54)
I wonder what caused that error and how to fix it.
P.S.
Here is the debug log:
Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Users\Tudor\CodeBlocks\OJI_2020_A6-A_forta\
Adding source dir: C:\Users\Tudor\CodeBlocks\OJI_2020_A6-A_forta\
Adding file: C:\Users\Tudor\CodeBlocks\OJI_2020_A6-A_forta\bin\Debug\OJI_2020_A6-A_forta.exe
Changing directory to: C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/.
Set variable: PATH=.;C:\Program Files\CodeBlocks\MinGW\bin;C:\Program Files\CodeBlocks\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Users\TT\AppData\Local\Programs\Python\Python37\Scripts;C:\Users\TT\AppData\Local\Programs\Python\Python37
[debug]Command-line: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/bin/Debug/OJI_2020_A6-A_forta.exe
[debug]Working dir : C:\Users\TT\CodeBlocks\OJI_2020_A6-A_forta
Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/bin/Debug/OJI_2020_A6-A_forta.exe
done
[debug]Reading symbols from C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/bin/Debug/OJI_2020_A6-A_forta.exe...
[debug]done.
[debug](gdb)
[debug]> set prompt >>>>>>cb_gdb:
Setting breakpoints
[debug]>>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (GDB) 8.1
[debug]Copyright (C) 2018 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law. Type "show copying"
[debug]and "show warranty" for details.
[debug]This GDB was configured as "x86_64-w64-mingw32".
[debug]Type "show configuration" for configuration details.
[debug]For bug reporting instructions, please see:
[debug]<http://www.gnu.org/software/gdb/bugs/>.
[debug]Find the GDB manual and other documentation resources online at:
[debug]<http://www.gnu.org/software/gdb/documentation/>.
[debug]For help, type "help".
[debug]Type "apropos word" to search for commands related to "word".
[debug]>>>>>>cb_gdb:
[debug]> set confirm off
Debugger name and version: GNU gdb (GDB) 8.1
[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 200
[debug]>>>>>>cb_gdb:
[debug]> set new-console on
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> directory C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/
[debug]Source directories searched: C:/Users/Tudor/CodeBlocks/OJI_2020_A6-A_forta;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> break "C:/Users/TT/CodeBlocks/OJI_2020_A6-A_forta/main2.cpp:54"
[debug]Breakpoint 2 at 0x401925: file C:\Users\TT\CodeBlocks\OJI_2020_A6-A_forta\main2.cpp, line 54.
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: C:\Users\TT\CodeBlocks\OJI_2020_A6-A_forta\bin\Debug\OJI_2020_A6-A_forta.exe
Child process PID: 6060
[debug][New Thread 6060.0x1624]

cygwin exception when assigning value to vector of strings

I am having following exception during the course of the run of program:
0 [main] myFunction 5560 cygwin_exception::open_stackdumpfile: Dumping stack trace to myFunction.exe.stackdump
The contents of stackdump file are as follows:
Stack trace:
Frame Function Args
00000223800 0018006FB93 (0060007AE38, 00600083EC8, 00600083EF8, 00600083F28)
00000000006 0018007105A (0060007BB78, 00600000000, 0000000014C, 00000000000)
000002239E0 0018011C6A7 (00600083048, 00600083078, 006000830A8, 006000830D8)
00000000041 001801198DE (0060007DCB8, 0060007DCE8, 00000000000, 0060007DD48)
0060008F2B0 00180119DAB (0060007E1F8, 0060007E228, 0060007E258, 00000000006)
0060008F2B0 00180119F7C (0060007CB38, 0060007CB68, 0060007CB98, 0060007CBC8)
0060008F2B0 0018011A23F (00180115A0B, 0060007CCE8, 006000885B0, 00000000000)
0060008F2B0 00180148A65 (003FC4AA93D, 00600083900, 00100439102, 0060007B080)
0060008F2B0 001800C1DB3 (00000000000, 00000223EE0, 0010042A2BC, 00000223E90)
0060008F2B0 00180115A0B (00000223EE0, 0010042A2BC, 00000223E90, 00000000017)
0060008F2B0 00600000001 (00000223EE0, 0010042A2BC, 00000223E90, 00000000017)
End of stack trace
Let me describe in detail the peculiar problem which happens at runtime. I am not able to describe the problem with just words, so I am listing scenario when the program works and when it fails.
I have created a vector of string in my header file and initialised them in the constructor as follows :
std::vector <std::string> symbolMap,localSymbolMap;
for(int i=0;i<100;i++){
symbolMap.push_back(" ");
localSymbolMap.push_back(" ");
}
I have defined a function to assign appropriate value to these variables later in the program as follows :
void TestClient::setTickerMap(int j, std::string symbol, std::string localSymbol){
symbolMap[j] = symbol;
localSymbolMap[j]=localSymbol;
}
Now, in the main program, I call this function as follows:
TestClient client;
for(int j=0;j<27;j++){
std::cout<<j<<" "<<realTimeSymbols[j]<<" "<<getLocalSymbol(realTimeSymbols[j],date)<<std::endl;
client.setTickerMap(j,realTimeSymbols[j],getLocalSymbol(realTimeSymbols[j],date));
}
// Here, I have checked for each j, that values of realTimeSymbols and getLocalSymbol are proper.
When I run the program, I get the error described above. The program always crashed when j is equal to 24.
Now the following workaround is working as of now:
void TestClient ::setTickerMap(int j, std::string symbol, std::string localSymbol){
if(j==24){
// symbolMap[j]="SYNDIBANK";
// localSymbolMap[j]="SYNDIBANK15MARFUT";
}
else{
symbolMap[j] = symbol;
localSymbolMap[j]=localSymbol;
}
if(j==1){
symbolMap[24]="SYNDIBANK";
localSymbolMap[24]="SYNDIBANK15MARFUT";
}
}
Following 3 variations of the code are above workaround are not working and they result in the original error:
Variation 1:
void TestClient ::setTickerMap(int j, std::string symbol, std::string localSymbol){
if(j==24){
// symbolMap[j]="SYNDIBANK";
// localSymbolMap[j]="SYNDIBANK15MARFUT";
}
else{
symbolMap[j] = symbol;
localSymbolMap[j]=localSymbol;
}
if(j==25){
symbolMap[24]="SYNDIBANK";
localSymbolMap[24]="SYNDIBANK15MARFUT";
}
}
Variation 2:
void TestClient ::setTickerMap(int j, std::string symbol, std::string localSymbol){
if(j==24){
symbolMap[j]="SYNDIBANK";
localSymbolMap[j]="SYNDIBANK15MARFUT";
}
else{
symbolMap[j] = symbol;
localSymbolMap[j]=localSymbol;
}
}
Variation 3:
void TestClient ::setTickerMap(int j, std::string symbol, std::string localSymbol){
if(j==24){
symbolMap[j]="AB";
localSymbolMap[j]="SYNDIBANK15MARFUT";
}
else{
symbolMap[j] = symbol;
localSymbolMap[j]=localSymbol;
}
}
Now, if I assign a single character to symbolMap in variation 3 as follows :
symbolMap[j]="A";
then the code is able to run(although is the result is not correct).
I am not able to figure what exactly is causing this runtime error. I have checked the related question (Cygwin Exception : open stack dump file) and I do not have a separate session of cygwin running. I have restarted my PC just be extra sure. Still the problem persists. Any suggestions as to why this behaviour is seen on my PC.
UPDATE:
To be sure that the error is not related to out-of-index, the following call from main program works fine:
TestClient client;
for(int j=25;j<27;j++){
std::cout<<j<<" "<<realTimeSymbols[j]<<" "<<getLocalSymbol(realTimeSymbols[j],date)<<std::endl;
client.setTickerMap(j,realTimeSymbols[j],getLocalSymbol(realTimeSymbols[j],date));
}
The program also works fine when j is iterated from 24 to 27. But fails when the loop is iterated from any number before 24 to 27.
GDB OUTPUT
I do not have much experience with gdb but following is the output of the gdb if it helps:
GNU gdb (GDB) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from order_trading2632_limit.exe...done.
(gdb) run
Starting program: /cygdrive/e/eclipse_workspace/testClient/Debug/testClient.exe
[New Thread 4832.0x11e4]
[New Thread 4832.0x1798]
Attempt 1 of 10000
[New Thread 4832.0x1020]
Connection successful
Program received signal SIGABRT, Aborted.
0x00000003fc4ab0e3 in cygstdc++-6!_ZNSs6assignERKSs () from /usr/bin/cygstdc++-6.dll
(gdb) bt
#0 0x00000003fc4ab0e3 in cygstdc++-6!_ZNSs6assignERKSs () from /usr/bin/cygstdc++-6.dll
#1 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) set $pc=*(void **)$rsp
(gdb) set $rsp=$rsp+8
(gdb) bt
#0 0x000007fefd3110ac in WaitForSingleObjectEx () from /cygdrive/c/Windows/system32/KERNELBASE.dll
#1 0x000000018011c639 in sig_send(_pinfo*, siginfo_t&, _cygtls*) () from /usr/bin/cygwin1.dll
#2 0x00000001801198de in _pinfo::kill(siginfo_t&) () from /usr/bin/cygwin1.dll
#3 0x0000000180119dab in kill0(int, siginfo_t&) () from /usr/bin/cygwin1.dll
#4 0x0000000180119f7c in raise () from /usr/bin/cygwin1.dll
#5 0x000000018011a23f in abort () from /usr/bin/cygwin1.dll
#6 0x0000000180148a65 in dlfree () from /usr/bin/cygwin1.dll
#7 0x00000001800c1db3 in free () from /usr/bin/cygwin1.dll
#8 0x0000000180115a0b in _sigfe () from /usr/bin/cygwin1.dll
#9 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
Note that stack trace is corrupted and I have used trick from following question to print the stacktrace (GDB corrupted stack frame - How to debug?). Please help me in debugging the program further.
UPDATE
It is not the case that the error happens only when index is 24. Before calling the said loop, I initialize various arrays of int, double and string. Changing the number of initialization affects the index when this error happens. Today, I initialised vectors of length 24 before running this loop, this time the error happened at index 3.
This is really frustrating to implement the workaround. I do not that if there are some other memory issues I am overlooking because of this. Please offer suggestions.
CODE
int main(int argc, char** argv) {
unsigned int port = 7900;
const char* host = "";
int clientId = 6;
int attempt = 0;
int MAX_ATTEMPTS=10000;
int NUMREALTIMESYMBOLS=37;
std::string realTimeSymbolsArr[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk"};
std::vector <std::string> realTimeSymbols(realTimeSymbolsArr,realTimeSymbolsArr+NUMREALTIMESYMBOLS);
int isTradeable[]={1,0,0,0,1,0,1,1,1,1,1,0,0,1,0,1,1,0,0,0,0,1,0,0,0,0,0,1,1,1,0,1,0,1,1,0,1};
int numSubscriptions[]={2,2,1,4,1,1,2,6,3,1,1,1,2,1,3,1,1,2,1,3,1,1,1,10,4,1,6,1,1,9,4,2,1,3,1,1,2};
int subscriptionList[NUMREALTIMESYMBOLS][100];
int subscriptionIndex[NUMREALTIMESYMBOLS][100];
subscriptionList[0][0]=0;subscriptionIndex[0][0]=0;
subscriptionList[0][1]=2;subscriptionIndex[0][1]=2;
subscriptionList[1][0]=2;subscriptionIndex[1][0]=1;
subscriptionList[1][1]=0;subscriptionIndex[1][1]=3;
subscriptionList[2][0]=2;subscriptionIndex[2][0]=0;
subscriptionList[3][0]=4;subscriptionIndex[3][0]=2;
subscriptionList[3][1]=31;subscriptionIndex[3][1]=2;
subscriptionList[3][2]=13;subscriptionIndex[3][2]=3;
subscriptionList[3][3]=34;subscriptionIndex[3][3]=3;
subscriptionList[4][0]=4;subscriptionIndex[4][0]=0;
subscriptionList[5][0]=9;subscriptionIndex[5][0]=2;
subscriptionList[6][0]=6;subscriptionIndex[6][0]=0;
subscriptionList[6][1]=8;subscriptionIndex[6][1]=2;
subscriptionList[7][0]=7;subscriptionIndex[7][0]=0;
subscriptionList[7][1]=8;subscriptionIndex[7][1]=1;
subscriptionList[7][2]=31;subscriptionIndex[7][2]=1;
subscriptionList[7][3]=36;subscriptionIndex[7][3]=1;
subscriptionList[7][4]=13;subscriptionIndex[7][4]=2;
subscriptionList[7][5]=34;subscriptionIndex[7][5]=2;
subscriptionList[8][0]=8;subscriptionIndex[8][0]=0;
subscriptionList[8][1]=7;subscriptionIndex[8][1]=1;
subscriptionList[8][2]=21;subscriptionIndex[8][2]=3;
subscriptionList[9][0]=9;subscriptionIndex[9][0]=0;
subscriptionList[10][0]=10;subscriptionIndex[10][0]=0;
subscriptionList[11][0]=11;subscriptionIndex[11][0]=0;
subscriptionList[12][0]=28;subscriptionIndex[12][0]=3;
subscriptionList[12][1]=33;subscriptionIndex[12][1]=3;
subscriptionList[13][0]=13;subscriptionIndex[13][0]=0;
subscriptionList[14][0]=33;subscriptionIndex[14][0]=1;
subscriptionList[14][1]=28;subscriptionIndex[14][1]=2;
subscriptionList[14][2]=15;subscriptionIndex[14][2]=3;
subscriptionList[15][0]=15;subscriptionIndex[15][0]=0;
subscriptionList[16][0]=16;subscriptionIndex[16][0]=0;
subscriptionList[17][0]=0;subscriptionIndex[17][0]=1;
subscriptionList[17][1]=11;subscriptionIndex[17][1]=2;
subscriptionList[18][0]=7;subscriptionIndex[18][0]=2;
subscriptionList[19][0]=6;subscriptionIndex[19][0]=3;
subscriptionList[19][1]=8;subscriptionIndex[19][1]=3;
subscriptionList[19][2]=16;subscriptionIndex[19][2]=3;
subscriptionList[20][0]=9;subscriptionIndex[20][0]=1;
subscriptionList[21][0]=21;subscriptionIndex[21][0]=0;
subscriptionList[22][0]=9;subscriptionIndex[22][0]=3;
subscriptionList[23][0]=6;subscriptionIndex[23][0]=1;
subscriptionList[23][1]=10;subscriptionIndex[23][1]=1;
subscriptionList[23][2]=27;subscriptionIndex[23][2]=1;
subscriptionList[23][3]=29;subscriptionIndex[23][3]=1;
subscriptionList[23][4]=16;subscriptionIndex[23][4]=2;
subscriptionList[23][5]=21;subscriptionIndex[23][5]=2;
subscriptionList[23][6]=2;subscriptionIndex[23][6]=3;
subscriptionList[23][7]=4;subscriptionIndex[23][7]=3;
subscriptionList[23][8]=7;subscriptionIndex[23][8]=3;
subscriptionList[23][9]=36;subscriptionIndex[23][9]=3;
subscriptionList[24][0]=24;subscriptionIndex[24][0]=0;
subscriptionList[24][1]=24;subscriptionIndex[24][1]=1;
subscriptionList[24][2]=24;subscriptionIndex[24][2]=2;
subscriptionList[24][3]=24;subscriptionIndex[24][3]=3;
subscriptionList[25][0]=29;subscriptionIndex[25][0]=3;
subscriptionList[26][0]=21;subscriptionIndex[26][0]=1;
subscriptionList[26][1]=0;subscriptionIndex[26][1]=2;
subscriptionList[26][2]=10;subscriptionIndex[26][2]=2;
subscriptionList[26][3]=15;subscriptionIndex[26][3]=2;
subscriptionList[26][4]=27;subscriptionIndex[26][4]=2;
subscriptionList[26][5]=33;subscriptionIndex[26][5]=2;
subscriptionList[27][0]=27;subscriptionIndex[27][0]=0;
subscriptionList[28][0]=28;subscriptionIndex[28][0]=0;
subscriptionList[29][0]=29;subscriptionIndex[29][0]=0;
subscriptionList[29][1]=4;subscriptionIndex[29][1]=1;
subscriptionList[29][2]=13;subscriptionIndex[29][2]=1;
subscriptionList[29][3]=16;subscriptionIndex[29][3]=1;
subscriptionList[29][4]=34;subscriptionIndex[29][4]=1;
subscriptionList[29][5]=6;subscriptionIndex[29][5]=2;
subscriptionList[29][6]=36;subscriptionIndex[29][6]=2;
subscriptionList[29][7]=27;subscriptionIndex[29][7]=3;
subscriptionList[29][8]=31;subscriptionIndex[29][8]=3;
subscriptionList[30][0]=30;subscriptionIndex[30][0]=0;
subscriptionList[30][1]=30;subscriptionIndex[30][1]=1;
subscriptionList[30][2]=30;subscriptionIndex[30][2]=2;
subscriptionList[30][3]=30;subscriptionIndex[30][3]=3;
subscriptionList[31][0]=31;subscriptionIndex[31][0]=0;
subscriptionList[31][1]=29;subscriptionIndex[31][1]=2;
subscriptionList[32][0]=11;subscriptionIndex[32][0]=3;
subscriptionList[33][0]=33;subscriptionIndex[33][0]=0;
subscriptionList[33][1]=15;subscriptionIndex[33][1]=1;
subscriptionList[33][2]=28;subscriptionIndex[33][2]=1;
subscriptionList[34][0]=34;subscriptionIndex[34][0]=0;
subscriptionList[35][0]=11;subscriptionIndex[35][0]=1;
subscriptionList[36][0]=36;subscriptionIndex[36][0]=0;
subscriptionList[36][1]=10;subscriptionIndex[36][1]=3;
double a1[]={720,0.0,750,0.0,900,0.0,760,360,120,390,600,360,0.0,760,0.0,140,660,0.0,0.0,0.0,0.0,720,0.0,0.0,100,0.0,0.0,120,320,40,100,500,0.0,630,570,0.0,100};
double a2[]={0.5,0.0,1.3,0.0,0.6,0.0,0.45,0.15,0.45,0.4,0.25,1.4,0.0,0.55,0.0,0.2,0.8,0.0,0.0,0.0,0.0,0.6,0.0,0.0,0.4,0.0,0.0,0.25,0.4,0.25,0.4,0.35,0.0,0.4,0.5,0.0,0.4};
double a3[]={1350,0.0,1250,0.0,300,0.0,1150,1400,900,1200,850,900,0.0,600,0.0,1450,1450,0.0,0.0,0.0,0.0,1000,0.0,0.0,1200,0.0,0.0,1150,350,1400,1200,1350,0.0,1500,300,0.0,1200};
double a4[]={0.6,0.0,0.7,0.0,0.2,0.0,0.3,0.55,0.4,0.8,0.25,0.7,0.0,0.25,0.0,0.55,0.5,0.0,0.0,0.0,0.0,0.4,0.0,0.0,0.7,0.0,0.0,0.65,0.55,0.45,0.7,0.6,0.0,0.4,0.4,0.0,0.7};
double a5[]={300,0.0,1300,0.0,1350,0.0,200,1100,1200,650,1500,1350,0.0,1050,0.0,1300,550,0.0,0.0,0.0,0.0,250,0.0,0.0,150,0.0,0.0,1250,700,1150,150,1250,0.0,1500,1500,0.0,150};
double a6[]={0.3,0.0,0.8,0.0,0.6,0.0,0.5,0.6,0.6,0.3,0.35,0.7,0.0,0.55,0.0,0.45,0.35,0.0,0.0,0.0,0.0,0.3,0.0,0.0,0.5,0.0,0.0,0.55,0.3,0.35,0.5,0.75,0.0,0.2,0.5,0.0,0.5};
double a7[]={1500,0.0,1500,0.0,1050,0.0,750,1100,1350,1350,100,1350,0.0,550,0.0,1400,1000,0.0,0.0,0.0,0.0,1000,0.0,0.0,1350,0.0,0.0,350,550,350,1350,500,0.0,1350,1250,0.0,1350};
double a8[]={0.9,0.0,0.9,0.0,0.8,0.0,0.6,0.35,0.7,0.2,0.15,0.7,0.0,0.3,0.0,0.55,0.5,0.0,0.0,0.0,0.0,0,0.0,0.0,0.3,0.0,0.0,0.4,0.3,0.5,0.3,0.35,0.0,0.5,0.5,0.0,0.3};
double a9[]={0.008,0.0,0.009,0.0,0.01,0.0,0.01,0.009,0.009,0.007,0.009,0.01,0.0,0.009,0.0,0.01,0.008,0.0,0.0,0.0,0.0,0.01,0.0,0.0,0.006,0.0,0.0,0.008,0.009,0.01,0.006,0.009,0.0,0.008,0.009,0.0,0.006};
double a10[]={0.008,0.0,0.009,0.0,0.008,0.0,0.008,0.008,0.009,0.008,0.008,0.006,0.0,0.009,0.0,0.01,0.008,0.0,0.0,0.0,0.0,0.005,0.0,0.0,0.009,0.0,0.0,0.01,0.008,0.009,0.009,0.009,0.0,0.008,0.01,0.0,0.009};
double a11[]={0.4,0.0,0.2,0.0,0.1,0.0,0.3,0.4,0.2,0.1,0.7,0.2,0.0,0,0.0,0.1,0,0.0,0.0,0.0,0.0,0.1,0.0,0.0,0.2,0.0,0.0,0.3,0,0.2,0.2,0,0.0,0.7,0.1,0.0,0.2};
int a12[]={500,1000,8000,2000,4000,1000,1250,1000,1000,500,1000,125,2000,4000,1000,250,2000,250,1000,1250,500,2000,1000,500,0,250,500,4000,4000,1250,0,2000,500,500,4000,125,1000};
double a13[]={0.0013406,0.0020022,0.0018709,0.0018948,0.0017975,0.0014687,0.0011068,0.001355,0.0010891,0.00088151,0.0014294,0.0012989,0.0014205,0.0019711,0.0015365,0.0020505,0.0018961,0.00078672,0.0023114,0.0012203,0.0012849,0.0015674,0.0012844,0.0014197,0.0,0.00074657,0.00096164,0.0017109,0.0015385,0.00068178,0.0,0.0021815,0.00087359,0.00074349,0.0021645,0.001595,0.0014573};
int a14[]={14850,0,16500,0,13740,0,13740,24750,13740,14100,13740,30750,0,14400,0,13740,13740,0,0,0,0,14100,0,0,13500,0,0,13740,13740,25200,13500,13740,0,13740,13740,0,13740};
int a15[]={30750,0,35900,0,34950,0,35900,35900,35900,35900,30000,34950,0,26250,0,34000,35900,0,0,0,0,34500,0,0,13500,0,0,35900,34650,35900,13500,32700,0,35900,35900,0,33300};
Client client;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
client.connect(host, port, clientId);
++attempt;
std::cout << "Attempt " << attempt << " of " << MAX_ATTEMPTS<< std::endl;
for (int j=0;j<NUMREALTIMESYMBOLS;j++){
if(j==24 || j==30)
continue;
std::cout<<j<<" "<<realTimeSymbols[j]<<" "<<getLocalSymbol(realTimeSymbols[j],date)<<std::endl;
client.setTickerMap(j,realTimeSymbols[j],getLocalSymbol(realTimeSymbols[j],date));
}
}
}
Constructor of Client:
Client::Client(){
for(int i=0;i<50;i++){
symbolMap.push_back(" ");
localSymbolMap.push_back(" ");
}
}
The above code fails at 24 and 30. Hence, the loop to continue when j is 24 or 30 as workaround.
std::string realTimeSymbolsArr[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk"};
subscriptionList[24][1]=24;subscriptionIndex[24][1]=1;
subscriptionList[24][2]=24;subscriptionIndex[24][2]=2;
subscriptionList[24][3]=24;subscriptionIndex[24][3]=3;
subscriptionList[30][1]=30;subscriptionIndex[30][1]=1;
subscriptionList[30][2]=30;subscriptionIndex[30][2]=2;
subscriptionList[30][3]=30;subscriptionIndex[30][3]=3;
You have not posted the source for getLocalSymbol, but I must assume that it also uses the same data and has a flow of the following form:
getLocalSymbol(a, b) {
int i, j, old_i, old_j;
std::string value;
// Derive i and j from the parameters
// ...
// And build the String
do {
old_i = i;
old_j = j;
i = subscriptionList[old_i][old_j];
j = subscriptionIndex[old_i][old_j];
value += realTimeSymbolsArr[i];
} while(j > 0);
return value;
}
Got it right? That control flow, or something equivalent, appears to be part of it, either way.
This goes well for almost all values of i and j - except for the aforementioned values of 24 and 30 for i, and 1 to 3 for j.
With these values, i and j remain the same in every iteration and value becomes longer and longer, until eventually something breaks on the stack which overwrites both j (and thereby causes the loop to terminate) and corrupts value.
Either way, the std::string you returned is now corrupted as you exceeded some limit during that endless loop.
As for how to solve it, fix that infinite loop and fix your data.
For fixing the loop, limit the iteration count.
For fixing your data, well, now that you know why the data is causing the bug, you should be able to figure yourself how to fix it.
Remember, you have to fix BOTH. If you don't fix the data, you will get an unreasonable long return value. And if you don't fix the iteration limit, it will crash again as soon as someone repeats a similar mistake when updating the data.

lldb assertion failure when attempting to print vector

I get the error
lldb: /home/hannes/.llvm/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2271: uint64_t ::RecordLayoutBuilder::updateExternalFieldOffset(const clang::FieldDecl *, uint64_t): Assertion `ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && "Field does not have an external offset"' failed.
Aborted (core dumped)
when I try to print a vector<string>. Does anyone know why this happens, and how to fix it? The equivalent works just fine in gdb (there are a number of reason why I'd rather use / have to use lldb over gdb).
I'm running Ubuntu 12.10 with llvm, clang and lldb trunk.
The program, build instructions and lldb command sequence:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using std::for_each;
using std::begin;
using std::end;
int main() {
std::vector<std::string> vec{"Hello","World","!"};
for_each(begin(vec),end(vec),[](const std::string& s) {
std::cout << s << " ";
});
std::cout << std::endl;
return 0;
}
clang++ -g -c -std=c++11 main.cpp
clang++ -std=c++11 main.o -o test
lldb test
Current executable set to 'test' (x86_64).
(lldb) break -n main
invalid command 'breakpoint -n'
(lldb) breat set -n main
error: 'breat' is not a valid command.
(lldb) break set -n main
Breakpoint 1: where = test`main + 26 at main.cpp:5, address = 0x0000000000400aea
(lldb) run
Process 24489 launched: '/home/hannes/Documents/Programming/CXX/test/test' (x86_64)
Process 24489 stopped
* thread #1: tid = 0x5fa9, 0x0000000000400aea test`main + 26 at main.cpp:5, stop reason = breakpoint 1.1
frame #0: 0x0000000000400aea test`main + 26 at main.cpp:5
2 #include <string>
3
4 int main() {
-> 5 std::vector<std::string> vec{"Hello","World","!"};
6 return 0;
7 }
n
Process 24489 stopped
* thread #1: tid = 0x5fa9, 0x0000000000400c72 test`main + 418 at main.cpp:6, stop reason = step over
frame #0: 0x0000000000400c72 test`main + 418 at main.cpp:6
3
4 int main() {
5 std::vector<std::string> vec{"Hello","World","!"};
-> 6 return 0;
7 }
frame variable
lldb: /home/hannes/.llvm/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2271: uint64_t <anonymous namespace>::RecordLayoutBuilder::updateExternalFieldOffset(const clang::FieldDecl *, uint64_t): Assertion `ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && "Field does not have an external offset"' failed.
Aborted (core dumped)
Log output with debug level 10:
Logging from function (<frame object at 0x3172f20>, '/usr/lib/python2.7/dist-packages/lldb/formatters/cpp/gnu_libstdcpp.py', 141, '__init__', ['\t\tlogger = lldb.formatters.Logger.Logger()\n'], 0)
Providing synthetic children for a map named vec
Logging from function (<frame object at 0x3170d10>, '/usr/lib/python2.7/dist-packages/lldb/formatters/cpp/gnu_libstdcpp.py', 214, 'update', ['\t\tlogger = lldb.formatters.Logger.Logger()\n'], 0)
Does the same thing happen if you say frame variable --raw?
This command is meant to dump your vector disabling data formatters. In the specific case you will get (assuming no crashes) the in-memory layout of the vector instead of the printout of the strings you stored there.
I am mostly trying to figure out if the data formatters are or not a part in this issue.