Hey guys,
I am getting weird behaviour from a deque experiment I wrote. It's not even that significant; it's a comparison operator.
Please just focus on the mentioned bug, this is incomplete and I would rather figure out the other problems myself. Thank you.
Here is the code:
Code:
Code: |
/*
* File: main.c
* Author: medicineman25
*
* Created on November 21, 2016, 8:16 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "limits.h"
/*
*
*/
int left_arr[127];
int right_arr[127];
int* ptr_arr[256];
void flush_ptr_arr(){
for(int i = 0; i < 256; i++){
ptr_arr[i] = NULL;
printf("%d pointer flushed\n", i);
}
for(int i = 0; i < 256; i++){
left_arr[i] = INT_MIN;
printf("left initialized: Value at leftarr [%d] = leftarr [%d] \n", i, left_arr[i]);
}
for(int i = 0; i < 256; i++){
right_arr[i] = INT_MIN;
printf("right initialized: Value at rightarr [%d] = rightarr [%d] \n", i, right_arr[i]);
}
}
int* check_left(int* left_ptr){
printf("This is the check left function executing\n");
if(*left_ptr == INT_MIN){ <<<<<<<<<----------------------------<<<<<<<<
printf("else: left array pointer = %d\n", left_arr[*left_ptr]);
return left_ptr;
} else {
printf("else cond. in check left left_ptr = %p \n", left_ptr);
left_ptr += 1;
check_left(left_ptr);
}
return left_ptr;
}
int* check_right(int* right_ptr){
if(right_arr[*right_ptr] == INT_MIN){
printf("%d", right_arr[*right_ptr]);
return right_ptr;
} else {
right_ptr += 1;
printf("else in check right");
check_right(right_ptr);
}
return right_ptr;
}
int* push_left(int* index, int* a){
left_arr[*index] = *a;
printf("%d", left_arr[*index]);
return &left_arr[*index];
}
int* push_right(int* index, int* b){
left_arr[*index] = *b;
printf("%d", right_arr[*index]);
return &right_arr[*index];
}
/*
int* check_ptr_array(int* ptr_array_index){
if(ptr_arr[*ptr_array_index] == NULL){
return ptr_array_index;
} else {
ptr_array_index += 1;
check_ptr_array(ptr_array_index);
}
}
*/
int main(int argc, char** argv) {
flush_ptr_arr();
int value = 7;
printf("This is inside the main loop just before check_left_ptr\n");
int* check_left_ptr = check_left(&left_arr[0]);
if(check_left_ptr != NULL){
printf("check_left_ptr = %p\n", check_left_ptr);
int* left_arr_index = push_left(check_left_ptr, &value);
} else {
int* check_right_ptr = check_right(&right_arr[0]);
if(check_right_ptr != NULL)
push_right(check_right_ptr, &value);
}
return (EXIT_SUCCESS);
} |
the logic is simple:
Code:
if(value){
check the left and right arrays;
if(INT_MIN){
stop iterating and return the address of that member, then push a value to that address;
} else { keep iterating until you find space for a new value;}
}
Here's the weird part: This following if statement won't evaluate to true.
Code:
Code: |
if(*left_ptr == INT_MIN){ <<<<<<<<<----------------------------<<<<<<<<
printf("else: left array pointer = %d\n", left_arr[*left_ptr]);
return left_ptr;
} else {
printf("else cond. in check left left_ptr = %p \n", left_ptr);
left_ptr += 1;
check_left(left_ptr);
} |
It did for a little bit but now again for no reason; it won't. So I tried all differing variations; two are as follows:
Code:
Code: |
if(*left_ptr == INT_MIN){ <<<<<<<<<----------------------------<<<<<<<<
printf("else: left array pointer = %d\n", left_arr[*left_ptr]);
return left_ptr;
} else {
printf("else cond. in check left left_ptr = %p \n", left_ptr);
left_ptr += 1;
check_left(left_ptr);
} |
became:
Code:
Code: |
if(*left_ptr != INT_MIN){ <<<<<<<<<----------------------------<<<<<<<<
printf("else: left array pointer = %d\n", left_arr[*left_ptr]);
return left_ptr;
} else {
printf("else cond. in check left left_ptr = %p \n", left_ptr);
left_ptr += 1;
check_left(left_ptr);
} |
Then the else statement would execute?????!!!!!! Which it should, because *left_ptr has been initialized as INT_MIN. But I don't want it to do that, I am trying to use INT_MIN as a way to check empty space. Why did the if statement not evaluate to true the first time??
I tried to simply switch the bodies of the if and else statements with eachother... this is where it get's REALLY wierd!! .... The if AND else statement stopped executing!!! ... if you think that's bad, hold on to your hats ladies and gentlemen because it's about to get spooky-action-at-a-distance in here; IF I then changed the comparison operator BACK to ==... (with the bodies of each conditional swapped)... all of a sudden the IF statement starts evaluating to true... i.e. like this:
Code:
Code: |
if(*left_ptr == INT_MIN){ <<<<<<<<<----------------------------<<<<<<<<
printf("else cond. in check left left_ptr = %p \n", left_ptr);
left_ptr += 1;
check_left(left_ptr);
} else {
printf("else: left array pointer = %d\n", left_arr[*left_ptr]);
return left_ptr;
} |
WTF IS GOING ON!!!
Is this somehow short-circuiting in a way that I am not yet aware?
NOTE: This DID start working for some reason, after I re-arranged like the above. However I finished where I started, nothing had actually been changed.
I'm using Netbeans on Arch.
Thanks in advance!
MedicineMan25
p.s. I also tried changing my compiler chain, updating the software and restarting the pc. nothing worked.