if >= condition changing the value of my elements in a global array
Hi all Thanks in advance for your time,
I have the following global arrays defined
int n_inactive=0;
long cc_no_issue[4]={0};
int issue[4];
and I increment the values
for(size_t k=0;k<contexts.size();k++)
{
if(issue[k]==0)
{
cc_no_issue[k]++;
}
else {
if(cc_no_issue[k]>=20)
{
n_inactive--;
}
cc_no_issue[k]=0;
}
if(cc_no_issue[k]==20)
n_inactive++;
}
Then I use a conditional if >=
if(cc_no_issue[disp_context_id]>=20)
after that my values are incremented over and over and then the condition is never satisfied and my program aborts.
When I use a conditional if ==
if(cc_no_issue[disp_context_id]==20)
The values stay and are not changed.
please note to avoid confusion disp_context_id is a number 0-3
Can anyone please provide some analysis as to why a if statement with a >= condition will change the value of my elements within the array
Thanks so much again.
Edit Here is how I am using the if statement
if(cc_no_issue[disp_context_id]>=10)
{
contexts_left.erase(contexts_left.begin()+current_context);
continue;
}
Edit: Here are some values for when i use == 7 10 3 9 10 13 6 12 2 10 56 8 4 0 58 10 0 1 10 7 3 0 13 10 10 2 1 17 0 8 7 10 2 10 9 12 3 11 10 13 3 29 28 10 0 134 10 115 0 2 10 18 1 10 18 26
And now when i change it to >= 2 6904 6904 6878 2 6904 6904 6878 0 6905 6905 6879 0 6905 6905 6879 0 6905 6905 6879 0 6906 6906 6880 0 6906 6906 6880 0 6906 6906 6880 0 6907 6907 6881 0 6907 6907 6881 0 6907 6907 6881 0 6908 6908 6882 0 6908 6908 6882 0 6908 6908 6882 0 6909 6909 6883 0 69开发者_开发百科09 6909 6883 0 6909 6909 6883 0 6910 6910 6884 0 6910 6910 6884 0 6910 6910 6884 0 6911 6911 6885 0 6911 6911 6885 0 6911 6911 6885 0 6912 6912 6886
The difference is easily notable
@Rob, Rob thanks for sticking with me. I really cant write a comparable compilable program because of many headerfiles and a datastructure. I can try to tell you in words
This cc_no_issue[4]={0}; array is defined initially as 0 I also have a array allocated for an issue array issue[4]
In another function of the code I have it to increment the elements 0-3 of Issue array
Then in the current function I have a check for elements 0-3 if any of them are equal to 0 then increment the cc_no_issue array.
I then reset the current element to 0 every time cc_no_issue[k]>=20
several lines down I want to remove a element from a data structure if a condition is met. That condition is within a while loop
if(cc_no_issue[k]>=10)
{
use .erase to remove it from the array
continue;
}
I hope it helps to clear things up.
If context.size() > 4
, you will write outside the bounds of cc_no_issue
. That would certainly be one reason for your program aborting.
You might also be outside the bounds of issue
as well. If you dereferenced an unavailable address, that would also abort your program.
精彩评论