Peter -
Yes, I have allocated a vector object that team_placementsp points to. It is modified by a function Find_teamplacements which takes a buffer, a length of that buffer and a vector of ints which it adds elements to until all the team placements in the buffer are found.
There is no problem with buffer, that I am sure. I've had that piece of code implemented for a long time -- simple fstream call that reads a the text file I'm interested in into the buffer array.
Yes, we will find the double quote character. The program worked as I wanted it to, then I partitioned a lot of the work from main() into separate functions, which is where I started running into problems. These are all good suggestions though and easy pitfalls.
Using valgrind was very helpful, thanks for the advice. It appears I was wrong about updating my compiler -- there is some very strange segfaults going on here. Its something to do with the buffer....
Here is the error valgrind is giving me --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Invalid read of size 1
==2964== at 0x8049543: FindString(char const*, std::string, int, int) (data_mine_ctxt.cpp:15)
==2964== by 0x80495E7: Find_teamplacements(char const*, int, std::vector<int, std::allocator<int> >&) (data_mine_ctxt.cpp:37)
==2964== by 0x8049C65: main (data_mine_ctxt.cpp:178)
==2964== Address 0x439b609 is 0 bytes after a block of size 496,673 alloc'd
==2964== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2964== by 0x8049BFF: main (data_mine_ctxt.cpp:169)
==2964==
==2964== Invalid read of size 1
==2964== at 0x8049543: FindString(char const*, std::string, int, int) (data_mine_ctxt.cpp:15)
==2964== by 0x80497B9: Find_scoreplacements(char const*, int, std::vector<int, std::allocator<int> >&) (data_mine_ctxt.cpp:73)
==2964== by 0x8049D2D: main (data_mine_ctxt.cpp:191)
==2964== Address 0x439b609 is 0 bytes after a block of size 496,673 alloc'd
==2964== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2964== by 0x8049BFF: main (data_mine_ctxt.cpp:169)
==2964==
|
What I don't understand is the three functions valgrind references -- FindString, Find_scoreplacements() and Find_teamplacements... don't do anything with dynamic memory, or have pointers and such. Buffer is created and allocated outside of those, in main.
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
|
int FindString(const char * haystack, const string needle, int start, int haystacklen) { //returns int i, where haystack[i] is the last character in needle
int i, j;
j=0;
for(i=start; i <= haystacklen; ++i) {
if(needle.length() <= j) //we have found the entire needle
return i-1;// i is the position in the array DIRECTLY AFTER the end of the string. Could cause problems if its the last member in the array!
else if(haystack[i] == needle[j]) //we have found part of the needle
++j;
else j = 0; //we found part or none of the needle and need to restart
}
return -1; //couldn't find the needle, returns an error
}
void Find_teamplacements(const char * buffer, int length, vector<int> & vector_of_teamps){
int i, j;
for(i=0; 0 <= i; ) {
j = i;
i = FindString(buffer, "_v_", j, length); // each team is denoted by "_v_", i.e. ManU_v_Arsenal
if( i!=-1)
vector_of_teamps.push_back (i);
}
}
void Find_scoreplacements(const char * buffer, int length, vector<int> & vector_of_scoreps){
int i, j;
for(i = 0; 0<=i; ){
j = i;
i = FindString(buffer, "<th align=\"center\" valign=\"top\" width=\"13%\">", j, length);// is the error because i'm double passing buffer?
if(i!=-1)
vector_of_scoreps.push_back(i);
}
}
|
here is their call in main, along with how buffer is allocated:
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
|
int main() {
int i, j;
char * buffer;
ifstream file;
int length;
file.open("2009_10_UEFA_Champions_League_group_stage.txt");
file.seekg(0, ios::end);
length = file.tellg();
file.seekg(0, ios::beg);
//allocate memory
buffer = new char[length];
//read data as a block:
file.read(buffer,length);
file.close();
vector<int> vector_of_teamps;
Find_teamplacements(buffer, length, vector_of_teamps);
vector<int> vector_of_scoreps;
Find_scoreplacements(buffer, length, vector_of_scoreps);
}
|
thanks for any help!