NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Weird behaviour with comparison operator

 
Post new topic   Reply to topic    NetBeans Forums -> C/C++ Users
View previous topic :: View next topic  
Author Message
medicineman25



Joined: 03 May 2016
Posts: 2

PostPosted: Mon Dec 05, 2016 11:10 am    Post subject: Weird behaviour with comparison operator Reply with quote

Hey guys,

I am getting weird behaviour from a deque experiment I wrote. It's not even that significant; it's a comparison operator.

Here is the 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);
}




Here's the weird part: This following if statement won't evaluate to true. 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:


 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:


 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. So, if it sees INT_MIN it should return the address of that element and push a value to it...

So I want it to return the address of that member when it finds INT_MIN. 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!!! ... hold on to your hats ladies and gentlemen because it's about to get spooky action at a distance in here; IF I now change the comparison operator BACK to ==... (with the bodies of each conditional swapped)... all of a sudden the IF statement starts evaluating to true...

WTF IS GOING ON!!! Smile

Is this somehow short-circuiting in a way that I am not yet aware?

Thanks in advance!
MedicineMan25

p.s. I also tried changing my compiler chain, updating the software and restarting the pc. nothing worked. or it did for a time and then stopped working..
Back to top
Paulo R. Panhoto
Posted via mailing list.





PostPosted: Tue Dec 06, 2016 8:53 am    Post subject: Weird behaviour with comparison operator Reply with quote

I tried to run your code and, only after that, one thing popped into my attention:

You're definin your left and right arrays with size 127 but you're attempting to initialise their data with 256 entries. This will yield undefined results. In my case, the software crashed.


On Mon, Dec 5, 2016 at 9:10 AM, medicineman25 <address-removed ([email]address-removed[/email])> wrote:
Quote:
Hey guys,

I am getting weird behaviour from a deque experiment I wrote. It's not even that significant; it's a comparison operator.

Here is the 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);
}






Here's the weird part: This following if statement won't evaluate to true. 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:


 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:


 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. So, if it sees INT_MIN it should return the address of that element and push a value to it...

So I want it to return the address of that member when it finds INT_MIN. 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!!! ... hold on to your hats ladies and gentlemen because it's about to get spooky action at a distance in here; IF I now change the comparison operator BACK to ==... (with the bodies of each conditional swapped)... all of a sudden the IF statement starts evaluating to true...

WTF IS GOING ON!!! Smile

Is this somehow short-circuiting in a way that I am not yet aware?

Thanks in advance!
MedicineMan25

p.s. I also tried changing my compiler chain, updating the software and restarting the pc. nothing worked. or it did for a time and then stopped working..




Back to top
Paulo R. Panhoto
Posted via mailing list.





PostPosted: Tue Dec 06, 2016 8:59 am    Post subject: Weird behaviour with comparison operator Reply with quote

This is my version:

/*
 * 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 = NULL;
            printf("%d pointer flushed\n", i);
        }


        for(int i = 0; i < 127; 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 < 127; 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_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_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){


    *index = *a;
    printf("%d", *index);
    return index;
}


int* push_right(int* index, int* b){


    *index = *b;
    printf("%d", *index);
    return 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);
}





On Tue, Dec 6, 2016 at 6:51 AM, Paulo R. Panhoto <[i]address-removed
([email]address-removed[/email])> wrote:
Quote:
I tried to run your code and, only after that, one thing popped into my attention:

You're definin your left and right arrays with size 127 but you're attempting to initialise their data with 256 entries. This will yield undefined results. In my case, the software crashed.


On Mon, Dec 5, 2016 at 9:10 AM, medicineman25 <address-removed ([email]address-removed[/email])> wrote:
Quote:
Hey guys,

I am getting weird behaviour from a deque experiment I wrote. It's not even that significant; it's a comparison operator.

Here is the 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);
}






Here's the weird part: This following if statement won't evaluate to true. 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:


 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:


 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. So, if it sees INT_MIN it should return the address of that element and push a value to it...

So I want it to return the address of that member when it finds INT_MIN. 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!!! ... hold on to your hats ladies and gentlemen because it's about to get spooky action at a distance in here; IF I now change the comparison operator BACK to ==... (with the bodies of each conditional swapped)... all of a sudden the IF statement starts evaluating to true...

WTF IS GOING ON!!! Smile

Is this somehow short-circuiting in a way that I am not yet aware?

Thanks in advance!
MedicineMan25

p.s. I also tried changing my compiler chain, updating the software and restarting the pc. nothing worked. or it did for a time and then stopped working..









Back to top
medicineman25



Joined: 03 May 2016
Posts: 2

PostPosted: Tue Dec 06, 2016 12:36 pm    Post subject: Reply with quote

hey, yeh so I fixed all of that. Can't believe I did that :/

Really I was posting on the nb forum because I thought it would be one of those bugs that everyone is dealing with and/or someone would jump out and say "oh yeh netbeans does this weird well-known thing that we do THIS to fix" but that's cool, as i've discovered it is in fact a segfault issue.

So.. to the segfault mobile.

It seems to be segfaulting no matter what. However this way:

Code:

printf("This is the check left function executing\n");
    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; 
         
    }


segfaults AFTER recursively calling until the end of the array i.e. the if conditional evaluates to true repeatedly until the end of the array at which point it segfaults for obvious reasons: their is a INT_MIN value at the end of the array, thus it keeps iterating off the end of the array. NOT what I want.

Whereas this way:

Code:

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);
 
 
    }


Segfaults at the point that the if conditional evaluates. Which indicates that I am dereferencing either a void pointer or a pointer to an address that holds no meaning. I am going to attempt debugging with some tools as per this post on cprogramming.com: Debugging Segmentation Faults and Pointer Problems - Cprogramming.com

Any suggestions in the meantime, are welcome.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> C/C++ Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo