my code is going out of bounds

I am really unsure how to keep within the boundries and still perform the function I need.
My code functions normal when I have both categories 'buy' and 'sell' in the queue which is my main goal and I should be clocked out on this function BUT,,,
If the queue is missing all 'sell' data, it segment faults.
I don't want to change any of the functionality, just get rid of the segment fault error.
It appears b < buydat.size() and buydat[b+1] are in conflict.
The purpose of the algorithm is capture the record sets in groups of 7 from data coming in from the www as strings. In that group/set, I pattern match for the string 'Buy' and if true, insert record into vector for processing. I also need the price (y = 3)

How do I capture buydat[2] and buydat[3] in groups of 7 without a segment fault?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void buymngr(){
        //vector defs
        vector<std::string> buydat;
        vector<std::string> markdat;
        vector<std::string> pricedat;
        vector<std::string> qworkcoin;

        buydat = getmyData();
        markdat = getmarketbuyData();
        cout << "Bork!" << endl;
		//This if statement was put here to try and prevent running code if missing data (seg fault patch)
		//It does not work and if category 'sell' is missing, it will fail to 'True' and run anyway then segment fault
        //if(!buydat.empty()){
        if(buydat[2] == std::string("Buy")){
                cout << "Bork2!" << endl;
                for(int b = 2; b < buydat.size(); b+=7){
                        int y = 3;  y+=7;
                        if ( buydat[b] == "Buy" ) {
                                pricedat.push_back(buydat[b+1]);
								//segment fault here
                                qworkcoin.push_back(buydat[y+1]);
                        }
				}
		}




gentoo-mini # cat getmyData.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
using namespace std;

vector<string> getmyData()
{
        FILE *mydfp = popen("php getMyorders.php 155", "r");
        if (mydfp == NULL) perror ("Error opening file");
        char mydbuff[BUFSIZ];
        vector<string> vrecords;
        while(fgets(mydbuff, sizeof(mydbuff), mydfp) != NULL){
                size_t n = strlen(mydbuff);
                //clean '\0' from fgets
                if (n && mydbuff[n - 1] == '\n') mydbuff[n - 1] = '\0';
                if (mydbuff[0] != '\0') vrecords.push_back(mydbuff);
                //if (mydbuff[0] != '\0') vrecords.push_back("0.001");
                //vrecords.push_back("0.003");
                //vrecords.push_back("0.0032");
                //vrecords.push_back("0.0033");
                //vrecords.push_back("0.0034");
        }
        cout << "I got my own data" << endl;
        return vrecords;
}



When one buy and one sell are sitting in the queue. Code functions as expected:

gentoo-mini # ./masterMain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
I got my own data
I just got market buy data
Bork!
Bork2!
You 'do' have buy string match data!
my max price is 0.00492975 at position 0
market max price is 0.00560504 at position 0
Highest is market!0.005605 0.00100000
php buycoin.php 155 0.005605 0.00100000 Command
0.005605 0.00100000 sendbuy
0.005605 0.00100000 mkbuybuff
0.00560505 *biggestMark
0.00100000
You bought coins, you are now top buyer
I got my own data
I just got market sell data
my min price is 0.00760479 at position 0
market min price is 0.00564973 at position 0
Lowest is market!php sellcoin.php 155 0.00564972
0.00564972
226250684




Data from the www following is as expected:

gentoo-mini # php getMyorders.php 155
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
224122032
2014-12-21 18:17:43
Buy
0.00492975
0.00010000
0.00010000
0.00000049
226250651
2014-12-26 10:56:48
Buy
0.00560500
0.00100000
0.00100000
0.00000561
226250684
2014-12-26 10:56:55
Sell
0.00564972
0.00100000
0.00100000
0.00000565
226250585
2014-12-26 10:56:31
Sell
0.00760479
0.00100000
0.00100000
0.00000760


BUT,,,
If I clear sell data and have only buy data in the queue, I get a segment fault:

gentoo-mini # ./masterMain
1
2
3
4
5
I got my own data
I just got market buy data
Bork!
Bork2!
Segmentation fault


Data sitting in queue is as follows:

gentoo-mini # php getMyorders.php 155
1
2
3
4
5
6
7
224122032
2014-12-21 18:17:43
Buy
0.00492975
0.00010000
0.00010000
0.00000049



gdb

gentoo-mini # gdb masterMain
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
GNU gdb (Gentoo 7.7.1 p1) 7.7.1
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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>.
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 masterMain...done.
(gdb) run
Starting program: masterMain
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
I got my own data
I just got market buy data
Bork!
Bork2!

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b9355b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b9355b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/libstdc++.so.6
#1  0x0000000000404ffc in 
    __p=0x60a1b0)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/alloc_traits.h:393
#4  0x00000000004041ad in std::vector<std::string, std::allocator<std::string> >::_M_emplace_back_aux<std::string const&> (this=0x7fffffffbfc0)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/vector.tcc:408
#5  0x0000000000403a69 in std::vector<std::string, std::allocator<std::string> >::---Type <return> to continue, or q <return> to quit---   q
Quit
(gdb)
#0  0x00007ffff7b9355b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/libstdc++.so.6
#1  0x0000000000404ffc in __gnu_cxx::new_allocator<std::string>::construct<std::string<std::string const&> > (this=0x7fffffffbfc0, __p=0x60a1b0)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/ext/new_allocator.h:120
#2  0x0000000000404b19 in std::allocator_traits<std::allocator<std::string> >::_S_construct<std::string<std::string const&> >(std::allocator<std::string>&, std::allocator_traits<std::allocator<std::string> >::__construct_helper*, (std::string<std::string const&>&&)...) (__a=..., __p=0x60a1b0)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/alloc_traits.h:254
#3  0x0000000000404123 in std::allocator_traits<std::allocator<std::string> >::construct<std::string<std::string const&> >(std::allocator<std::string>&, std::string<std::string const&>*, (std::string<std::string const&>&&)...) >::---Type <return> to continue, or q <return> to quit---
push_back (this=0x7fffffffbfc0,
    __x=<error reading variable: Cannot access memory at address 0xfffffffffffffff2>)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_vector.h:911
#6  0x0000000000401e62 in buymngr () at buymngr.h:38    (actually pointing to line 18 -> qworkcoin.push_back(buydat[y+1]);))
#7  0x000000000040329f in main () at masterMain.cpp:25 
Not knowing what you are doing, you could catch the error and ignore it.
http://www.cplusplus.com/reference/stdexcept/out_of_range/
In void buymngr(), at line 17, you have int y = 3; y+=7;. You could just write that as y=10. If you have only one record, buydat does not go that far. I assume you meant y=b+1;
Topic archived. No new replies allowed.