Reputation: 2914
A bitmap sort programme,in which the assign part is:
for(i = MAX/64-1;i >= 0;i--){
for(j = 0;j < 64;j++){
if(0 != (arr[i] & (1 << j))){
*p++ = j + 64 * i;
}
}
}
I gdb it,sometimes when the if condition returns 0 and the program still enter the block and execute the statement:
*p++ = j + 64 *i;
which cause segmentfault at last,I just don't understand under which circumstance would it happen
example:
when gdb status like this:
j=44, i=6250, arr[i]=4096 and print (arr[i] & (1 << j))
gives 0
but programme still enter the block
Upvotes: 2
Views: 120
Reputation: 213190
My guess is that you have 32 bit ints but your array data type is int64_t
. In which case you need to change:
if(0 != (arr[i] & (1 << j))){
to:
if(0 != (arr[i] & (1LL << j))){
since 1 << j
is undefined for j >= sizeof(int) * CHAR_BIT
(i.e. j >= 32
in your case).
Upvotes: 4
Reputation: 400109
1 << j
is not very well-defined if it's int
and j
is greater than or equal to CHAR_BIT * sizeof (int)
. This might be a problem since you use j = 44
in your example.
Upvotes: 3