PDA

View Full Version : Code works, but segfaults under GCC 4.1, GCC 3.4 is just fine



montgoej
June 19th, 2007, 02:31 AM
Okay, my problem is with this file http://kyol.net/~hatc101/binstring.c
It compiles and works as expected under GCC 4.1 and GCC 3.4, but for some reason segfaults at the end if compiled with GCC 4.1. Anyone know why this happens and what can be done to fix this?
~Jordan Montgomery

joe_bruin
June 19th, 2007, 02:41 AM
You're reading an int into a short (usually 32 bits into 16). scanf()'s %d format reads an int. Try changing the type of 'value' to unsigned int and changing the format to %u (unsigned int).

Smygis
June 19th, 2007, 02:45 AM
jupp, Nothing to see heare, Move on peapole

montgoej
June 19th, 2007, 02:48 AM
Thanks. My code is meant to work on 16 bit values, but doing those changes and changing binstring() to work on 32 bit values does work. BTW, the code is from C For Dummies: All-In-One Desk Reference by Dan Gookin.
~Jordan Montgomery

vexorian
June 19th, 2007, 02:50 AM
sudo apt-get install valgrind

Then run your 4.1 compiled program with valgrind from console like:


~@# valgrind ./myprogram

It should at least tell you suspicious lines of codes that got issues.


I once had issues with 4.1 and tinyxml, it was all about some changes in STL support


EdiT: DOH, I was unable to read the previous posts correctly, sorry for the unnecessary post.

Lux Perpetua
June 19th, 2007, 04:02 AM
To scanf a 16-bit integer, use "%hd" instead of "%d".