# c++ evaluation/order of operations

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by VprMatrix89Does anyone else have these problems?

Only when I work with C/C++.

##### Share on other sites
Quote:
 Original post by VprMatrix89In 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.

##### Share on other sites

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984423

• 25
• 11
• 10
• 16
• 14