Weird behavior while doing C programming in eclipse -


i have not touched c long long time. first language c. have been taught c++, java , c# (in college days). work involve java , groovy. , have c again. not familiar how industry uses c never in project doing stuff in c.

i started in eclipse cdt, mingw on windows 10. new program grew big quickly. , lack of experience unrevealed me. when run, program used crash showing windows dialog saying "myprogram.exe has stopped working". had no clue whats going wrong. compilation clean no error, 1 warning. since java world, warnings not fatal. ignored (i thats lack of c experience). , went on debugging found cause. realised warning indeed cause. suffered mental frustation of wasting hours in debugging.

so here code replicating issue in original code:

1    #include "stdio.h" 2    #include "limits.h" 3     4    typedef struct tempstruct tempstruct; 5     6    struct tempstruct 7    { 8        int a; 9        tempstruct *next; 10    }; 11     12    int function1(tempstruct *param) 13    { 14        return param == null; 15    } 16     17    int function2(tempstruct **param) 18    { 19        if(function1(param)) 20        { 21            return int_min; 22        } 23        *param = (*param)->next; 24        return 0; 25    } 26     27    int main() 28    { 29        tempstruct *tempstructobj = null; 30        function2(&tempstructobj); 31        printf("does not reach here!!!"); 32        return 0; 33    } 

my c-noob eyes did not see wrong in it. knew how debugging. got following in debugging:

enter image description here

in main() done: *tempstructobj = null. so, expecting function1() return 1, making function2() returning line 21. issue function1() takes tempstruct*. on line 19, passed **param. inside function1(), param wasnt null. returned false (0 guess). function2() did not returned line 21. executed (*param)->next , hence program crashed.

my questions:

  • q1. shouldn't such issue reported error's instead of warnings? there setting can report such potentially fatal warnings error?
  • q2. eclipse logs reasons of such sudden app crash somewhere? instead of debugging stepping through each line, can refer report can possibly specify line number caused crash
  • q3. industry-standard approach deal such mistakes? of course 1 dont commit mistake. asking precautions taken avoid such mistakes or auto detect them. above asked settings make eclipse report issue fatal 1 or making crashes generate report stuff can fixed instead of hours long debuggins. use better (and possibly involving smaller learning curve) alternative (eclipse cdt + mingw + windows) provide more powerful debugging can avoid such errors.
  • q4. in point 1 of above diagram, error: multiple errors reported.... stuff occurred occasionally, not always, once in 5 debugging sessions. can reason behind such ad hoc behavior?
  • q5. in point 3 of above diagram, (0x62ff2c) value of param inside function1()? if keep signature of function1() correctly int function1(tempstruct **param) , change inside reference correctly *param, *param correctly 0x0 (i.e. null): enter image description here

    edit

    1. this on ideone works (with c) & prints "does not reach here!!!". dont know how handled (*param)->next.
    2. this on ideone (with c99 strict) gives error (not warning).

q1. no, warnings legit c code. could possible want such code. can use -werror on gcc make warnings errors. add other flags turning on more warnings -wall -wpedantic -wextra -wshadow -wconversion -wno-sign-compare etc. this'd bit closer you're used when using java ;-)

q2. atleast on linux have coredumps, iirc windows minidumps. these can loaded corresponding executable debugger. can access backtraces, values etc.

q3.

like above asked settings make eclipse report issue fatal 1 or making crashes generate report stuff can fixed instead of hours long debuggins.

log yourself. there can macros easing this.

do use better (and possibly involving smaller learning curve) alternative (eclipse cdt + mingw + windows) provide more powerful debugging can avoid such errors.

ide pretty irrelevant c imho. use linux native gcc instead, mingw nice can daunting (my experience). ofcourse ms vsc++ can compile c c++ compatible not specific 1 standard.

q4. well, multiple errors occured listed. if it's difficult reproduce might problem in setup, experience had mingw on windows.

q5. it's address -- have pointer pointer, first ("outer") pointer address, pointing pointer null. or more verbosely: tempstructobj pointer null (ie. int_ptr holds value 0x0.

to function2 pass int_ptr holds semi-random value/address of automatic variable int_ptr tempstructobj stored ie. have such:

address &tempstructobj: tempstructobj

in ram. when call function1, pass value of (not-null) pointer. of course comparison false. you'd need compare *param null.

even more: if compile gcc (on linux) , use verbose flags get:

gcc -std=c99 -wall -wpedantic -wextra -wshadow -wconversion -wno-sign-compare -o main main.c main.c: in function ‘function2’: main.c:19:18: warning: passing argument 1 of ‘function1’ incompatible pointer type [-wincompatible-pointer-types]      if(function1(param))                   ^ main.c:12:5: note: expected ‘tempstruct * {aka struct tempstruct *}’ argument of type ‘tempstruct ** {aka struct tempstruct **}’  int function1(tempstruct *param)      ^ 

so problem had ^^

also i'd remove function1 altogether, it's unnecessary , obfuscates code. i'd use different name struct , typedef, appending latter _t. i'd move 1 shorter piece of code. on side note: add \n in printf()-call.

edited code:

#include <stdio.h> #include <limits.h>  typedef struct tempstruct_s {     int a;     struct tempstruct_s *next; } tempstruct_t;  int function(tempstruct_t **param) {     if(!*param) {         return int_min;     }     *param = (*param)->next;     return 0; }  int main() {     tempstruct_t *tempstructobj = null;     function(&tempstructobj);     printf("does not reach here!!!\n");     return 0; } 

Comments