Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: How does 7 - 1 = 3?

NSLog(@"Before: %d",currentArticle);
currentArticle--;
NSLog(@"SUBTRACT %d",currentArticle);

"currentArticle" is an integer. This is only being echoed once in my console. If I do not run this subtraction, the number "currentArticle" remains at 7.

This is being run in the main thread, and only run once per user interaction.

I have also tried

currentArticle = currentArticle - 1;

With the same result. Am I taking crazy pills?

Thanks!

Edit:

Declared as follows:

extern int *currentArticle;

And assigned later as:

currentArticle = 0;

I tried rewriting as this:

int *curArticle; // in my .h file

curArticle = 1;

And then I run the

curArticle--;

and it still decrements by two...

I have stepped through the code and ensured there are no other calls hitting this variable.. Thanks for the feedback so far, I will keep hacking away at it.

like image 926
John Sloan Avatar asked Mar 10 '11 23:03

John Sloan


2 Answers

I concur with the comments above. I'd bet a dollar that your code looks like:

int *currentArticle = 7; // or something

currentArticle may not even be a pointer to an int, specifically, but it's very likely a pointer to some 4-byte type. The '--' and '++' operators, when applied to pointers, decrement or increment by the size of the type that's pointed to.

like image 182
Caleb Avatar answered Oct 08 '22 01:10

Caleb


Things I think of: Threads (if it's a strange problem, there are threads)? Or is it called by an event (which is triggered more than once)?

like image 42
Tobias Reithmeier Avatar answered Oct 08 '22 02:10

Tobias Reithmeier