0

I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:

    Qp[j] = (Cp - Cm)/(Bp + Bm);

Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?

I tried searching online but the keywords I used brought up results relating to global variables.

--- EDIT

Hi all. To those have responded, thank you. Just to clarify my question:

Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?

Eric Inclan
  • 327
  • 1
  • 5
  • 14

2 Answers2

2

I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.

Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).

dynamic
  • 46,985
  • 55
  • 154
  • 231
  • The code works either way because Qp[j] should never be accessed by any other line of code. It's a dummy value. So the fact that it impacts the code at all is what worries me. The issue is not that line of code, it's the other lines that use that value. Also, Bp+Bm never equals 0. – Eric Inclan Jan 20 '14 at 23:53
  • Wait. You just said you Qp[j] shouldn't be accesses. Then why would the issue be other lines using it? Why would other lines access it at all if they shouldn't? – Gasim Jan 21 '14 at 00:01
  • is your app multi threaded? consider that vector isn't synched – dynamic Jan 21 '14 at 00:04
  • Hi @Gasim. That's my point. Other lines shouldn't access it. I want to know which ones do, so I can fix the problem. I'm hoping there's some feature in VS or some wrap around program that makes this easy so I can speed up the process. – Eric Inclan Jan 21 '14 at 00:11
  • You can search for Qp in your project with [Find in Project](http://msdn.microsoft.com/en-us/library/vstudio/sdyw19de(v=vs.100).aspx) dialog box? And check out every match throughout your application. – Gasim Jan 21 '14 at 00:16
0

Qp may not have as many elements as the index j, check the size of Qp.

nightwing
  • 114
  • 1
  • 5
  • that's unlikely because it would trigger an excpection – dynamic Jan 21 '14 at 00:09
  • @llnk The subscript operator operator usually doesn't throw, the method `.at(index)` does. At least afaik `operator[]` with index out of bounds is UB, while `at` is required to throw an out-of-bound exception. See http://en.cppreference.com/w/cpp/container/vector/operator_at – stefan Jan 21 '14 at 00:15
  • Sorry! You are right. The subscript operator does not have bounds checked, so no exception is raised but if the vector is empty and the (nonexistent) third element is accessed, it will crash. Correct me if I'm wrong – nightwing Jan 21 '14 at 00:24
  • @nightwing `std::vector foos; foos.reserve(100); Foo foo = foos[3];` Such a code is UB, but it won't crash, because the memory position is valid (because it's reserved!). This will be a silent error. When in doubt, use `at` or `assert` that the bound is correct. – stefan Jan 21 '14 at 00:40
  • Yes, but we are not sure if that reserve function had been called? If it had been, memory position is valid. What if the code had been like this: std::vector foos; Foo foo = foos[3]; // Error! – nightwing Jan 21 '14 at 00:58
  • @nightwing Of course. I don't think there will ever be a STL implementation that reserves space without beeing asked for. In the general case it holds that access in the reserved range probably won't crash fatally (but it's still UB), but outside of the reserve range, anything can happen, including a crash (it might still work, it's simply undefined). – stefan Jan 21 '14 at 01:07
  • @stefan : yup, true! within reserved range it will be UB – nightwing Jan 21 '14 at 01:10