Wednesday, October 10, 2007

Where is my Pointer

being a TA, its my job to help the students when they encounter any problem in implementation or tools used for their projects, when Joseph said that  his program is receiving SIGSEGV, i thought it must be some simple problem either the index of array is not proper or memory not allocated, so i started to debug and Voila! the program works just fine giving correct output.
we all were wondering what is wrong, and eclipse(CDT) was the first target, but debugging in eclipse also worked fine, and now we decided to go manual line by line checking so few things are observed
  • they wanted a 2D array for which they know that the column size is two and row size should be dynamically allocated
  • Array of pointers were used
so i said to them, "fine, but i think we should convert those array of pointers to double pointers", and they agreed, so the next run gave the same SIGSEGV error.
now its time for more digging, so i prefer to user -Wall option to clean and make my code perfect, there i noticed one warning, function returns reference to local pointer, voila got it... so changed the function a bit, instead of returning a pointer we passed a pointer to the function and worked on it, and the result SUCCESS.
it shouldn't have taken much time for me to find out, being an OS student, we always deal with functions(system calls) which take pointer as an argument instead of returning them, so there must be something wrong/bad in returning the reference to local pointer, the memory gets lost!!!. but why not in debugging, the only reason that came to my mind is when debugging the memory areas used by the program is locked till it is freed or the program ends.

Blogged with Flock

No comments: