Inputting into double-sized vector freezes?!

Good morning everyone,

This is actually my first C++ experience and I've been trying to complete an assignment. Everything is going just fine however, there seems to be an issue.
Whenever the code reaches:
matrix[atoi(tmp[1].c_str())][atoi(tmp[2].c_str())] = strtod(tmp[3].c_str(), NULL);
the app just blocks. Could it be due to a lack of RAM (since its inside a massive loop and I'm currently using an old-ass pc with 1GB RAM) or is it bad programming?
If I remove that line everything runs smoothly.


Thanks in advance.
Last edited on
This is not enough information to know what the problem is. We don't even know what matrix is. It can't be an array (or a vector) because you can't use a double as an index to an array. Remember that indexes start at zero so if the tmp array only has 3 elements tmp[3] is out of bounds.
Problem solved.
Issue was: in math - hence in the file - the columns and rows start in 1, whilst in c++ they start in 0, thus, we would get an OutOfBounds exception.
Last edited on
You're packing an awful lot into that one line of code. It'll be easier to read - and to debug - if you break it down a bit, e.g.

1
2
3
4
5
int line = atoi(tmp[1].c_str());
int column = atoi(tmp[2].c_str());
double value = strtod(tmp[3].c_str(), NULL);

matrix[line][column] = value;


It'll help you figure out exactly which operation is causing your program to hang.

Edit: I recommend running it in a debugger, so that you can see exactly what's happening at the point where it hangs.
Last edited on
Thanks for your help MikeyBoy.
I had tried that earlier just to see what was wrong, but it showed the problem wasn't in the int,column and value variables.
Here's what I get from debugging (and the problem is for SURE in the matrix part since, if I remove it everything works):
1
2
3
4
5
6
7
8
9
Output to Display...
"Reading symbols from c:\\users\\banhudo\\desktop\\gui\\lp\\projectos\\trabalho 1\\output\\mingw\\Trabalho1.exe..."
Output to Display...
"done.\n"
Output to Display...
"[New Thread 3204.0x368]\n"
Stopped - signal received
Thread 1 stopped in load at line 56 in main.cpp with Segmentation fault
Debugger closed.


Line 56 being:
matrix[atoi(tmp[1].c_str())][atoi(tmp[2].c_str())] = strtod(tmp[3].c_str(), NULL);

Seems to be an Access Violation.
Last edited on
Oh, so it's actually crashing, rather than just hanging?

So, just to clarify, if you break the line down the way I suggested, it crashes on the line:

matrix[line][column] = value;

- is that right?

If so, the most likely answer is that the values of line and/or column are such that you're trying to put a value into memory outside the bounds of matrix. What are the values of n, line and column at the point where it crashes?
Last edited on
Yes its actually crashing, my bad.
I thought of that too MikeyBoy, and thank you, but I actually just figured out what it was. Its called being dumb.
The size of the matrix (squared matrix) is defined by a line at the beginning of the file (in this case, 926). The maximum value any column or line in the file takes is 926. However, just like in any programming language, the vector starts at 0, meaning,
matrix[atoi(tmp[1].c_str())][atoi(tmp[2].c_str())] = strtod(tmp[3].c_str(), NULL);
CANNOT work since it has to be:
matrix[atoi(tmp[1].c_str())-1][atoi(tmp[2].c_str())-1] = strtod(tmp[3].c_str(), NULL);
Since well, in a mathematical matrix the first column is 1 but in computers its 0.

Jeez I feel dumb now :)
It's an easy mistake to make :) Glad you found it!
Topic archived. No new replies allowed.