c++ evaluation/order of operations

Started by
3 comments, last by Driv3MeFar 15 years, 10 months ago
It goes like this. n equals 0 in each equation. n=BinList_n[SplitLogic[(BinList[(P_ParentQuad/12)])+1+move] / 12] n=BinList_n[60 / 12] n=BinList_n[5] n=0 I have evaluated them by themselves and they return valid values. int n1 =SplitLogic[(BinList[(P_ParentQuad/12)])+1+move]; //60 int n2 =n1 / 12; //5 int n3 =BinList_n[n2]; if n2 is 5 it should be equivalent to the following: int n3 =BinList_n[5]; But the second works and using n2 as the variable it crashes. Why?
Advertisement
It probably "crashes" every time, but when using literal, the compiler finds out the correct result during compile-time, so the array is never dereferenced.

For example:
int values[2];values[5] = 0;std::cout << values[5];
could be compiled into:
std::cout << 0;


I'm not claiming this actually is the case, but the code you posted doesn't allow any other conclusion.

Either array or arithmetic operations can't crash.

What can happen however is they result, during run-time, in undefined behavior when you exceed the boundaries of allocated memory. You don't however ensure that you really are accessing valid index here.
Yes, the compiler did say it was access violation, reading.
I just solved the problem after like 3 days and the problem was so tiny it boggled my mind. In the array there is a terminal number that ends the search, and basically there is two functions that stamp it there, and on the 2nd function i simply forgot to add it, and since I had never used that function before I didnt notice it. So the searcher was finding a value at the end of allocation like -920983248 and using that as an index.

For some reason these simple problems keep stalling my progress and they're so annoying and small that I overlook them. Does anyone else have these problems?
Quote:Original post by VprMatrix89
Does anyone else have these problems?


Only when I work with C/C++.
Quote:Original post by VprMatrix89
In the array there is a terminal number that ends the search, and basically there is two functions that stamp it there, and on the 2nd function i simply forgot to add it, and since I had never used that function before I didnt notice it. So the searcher was finding a value at the end of allocation like -920983248 and using that as an index.


This is why std::vectors and assertions exist.

This topic is closed to new replies.

Advertisement