Integer not comparing equal to its value

Patrick

I've got a piece of code I am trying to debug, which is producing bizarre results. An integer, which very clearly contains the value six, compares false to the literal 6.
In my code, this code:

int allGreen = 0;
int index = 0;
while (ch->fb[index]->selection_color() == FL_GREEN) 
{
    ++allGreen;
    ++index;
}
std::cout << allGreen << std::endl;
std::cout << std::boolalpha << (allGreen == 6) << std::endl;

Produces the output:

6
false

I am compiling with g++ 4.8.2 on Ubuntu.

How can this possibly happen?

Edit: Removing all green from the condition doesn't help. Edit 2: index and allGreen are equal, as expected. Neither is equal to 6, despite both being 6.

user2512323

What is the size of ch->fb array? If it is 6 or less, it could be compiler optimizing undefined behavior here.

When seeing some simple loop like

while ( a[i] ) i++;

Compiler can make an assumption that i never goes outside of a array, otherwise program invokes undefined behavior. That is i lies between 0 and size of a - 1.

Later, if compiler sees something like i == 8, there 8 is larger than size of a - 1, it replaces this condition with false.

Just to be sure, you can look at assembly code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Comparing List<Integer> by index

From Dev

Sorting a dictionary by its value, and then its key if values are equal then outputting a list

From Dev

Two integer value equal in java?

From Dev

Comparing A String To Integer, Not Working?

From Dev

How to round a floating value to its closest integer in Jquery

From Dev

Sort TStringGrid by row and its integer value

From Dev

Comparing String with Integer in javascript

From Dev

Comparing Performance in Python: equal versus non equal

From Dev

Enumeration object set to a value not equal to any of its respective enumeration constants

From Dev

Comparing BOOLEAN TRUE to INTEGER value in PHP

From Dev

NaNs comparing equal in Numpy

From Dev

Search a sorted integer array for an element equal to its index, where A may have duplicate entries

From Dev

MySQL select rows where its columns sum equal value

From Dev

Comparing two dataframes of different length row by row and adding columns for each row with equal value

From Dev

comparing with equal operator in fsharp

From Dev

How to turn a signed integer value to unsigned, according to its bits?

From Dev

Comparing an int and Integer in Java

From Dev

write a hexadecimal integer literal equal to Int.MIN_VALUE in Kotlin

From Dev

Comparing a float and its copy

From Dev

comparing integer value in if statement in bash

From Dev

how to get the value of id if its id is an integer value in jquery

From Dev

Comparing char with integer

From Dev

Check an input of type "checkbox" if its value is equal to an array member?

From Dev

MySQL select rows where its columns sum equal value

From Dev

Comparing integer with array of integers

From Dev

Hide option if its value is equal to another option

From Dev

Comparing a float and its copy

From Dev

Finding documents in mongodb collection where a field is equal to given integer value

From Dev

How do I check if the value attribute is equal object key, if its equal take the key value and add a class to it

Related Related

  1. 1

    Comparing List<Integer> by index

  2. 2

    Sorting a dictionary by its value, and then its key if values are equal then outputting a list

  3. 3

    Two integer value equal in java?

  4. 4

    Comparing A String To Integer, Not Working?

  5. 5

    How to round a floating value to its closest integer in Jquery

  6. 6

    Sort TStringGrid by row and its integer value

  7. 7

    Comparing String with Integer in javascript

  8. 8

    Comparing Performance in Python: equal versus non equal

  9. 9

    Enumeration object set to a value not equal to any of its respective enumeration constants

  10. 10

    Comparing BOOLEAN TRUE to INTEGER value in PHP

  11. 11

    NaNs comparing equal in Numpy

  12. 12

    Search a sorted integer array for an element equal to its index, where A may have duplicate entries

  13. 13

    MySQL select rows where its columns sum equal value

  14. 14

    Comparing two dataframes of different length row by row and adding columns for each row with equal value

  15. 15

    comparing with equal operator in fsharp

  16. 16

    How to turn a signed integer value to unsigned, according to its bits?

  17. 17

    Comparing an int and Integer in Java

  18. 18

    write a hexadecimal integer literal equal to Int.MIN_VALUE in Kotlin

  19. 19

    Comparing a float and its copy

  20. 20

    comparing integer value in if statement in bash

  21. 21

    how to get the value of id if its id is an integer value in jquery

  22. 22

    Comparing char with integer

  23. 23

    Check an input of type "checkbox" if its value is equal to an array member?

  24. 24

    MySQL select rows where its columns sum equal value

  25. 25

    Comparing integer with array of integers

  26. 26

    Hide option if its value is equal to another option

  27. 27

    Comparing a float and its copy

  28. 28

    Finding documents in mongodb collection where a field is equal to given integer value

  29. 29

    How do I check if the value attribute is equal object key, if its equal take the key value and add a class to it

HotTag

Archive