Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this