Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Appleness

macrumors member
Original poster
Feb 26, 2009
34
1
I am developing command line apps in C.

Having turned off 64 bits and chosen GCC 4.0 (otherwise my programs crash), I found some strange behaviour with sprintf() function.

If I build the project with SDK 10.5 it works fine, but if I choose 10.6 it crashes. I noticed that (with 10.6) the editor shows "sprintf" in other color (different from printf statements), as if they were preprocessor statements.

What am I doing wrong?

Thanks
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
If your programs crash when you try and compile them with anything other than GCC 4 then it suggests you have some strange issues. I'm not even sure what GCC 4 specific items were removed from GCC 4.2 or even Clang for that matter (which has attempted to remain compatible with GCC extensions).
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I am developing command line apps in C.

Having turned off 64 bits and chosen GCC 4.0 (otherwise my programs crash), I found some strange behaviour with sprintf() function.

If I build the project with SDK 10.5 it works fine, but if I choose 10.6 it crashes. I noticed that (with 10.6) the editor shows "sprintf" in other color (different from printf statements), as if they were preprocessor statements.

What am I doing wrong?

Thanks

I don't know what you are doing wrong, but if it runs with GCC 4.0 and not with GCC 4.2 or clang, then 99.99% it is something wrong with your code. Uninitialised variables, dependence on order of evaluation, lots of things that can go wrong with one compiler and work with an other, but all bugs in the original code.

As an example, take

Code:
char buffer [10];
long x;

sprintf (buffer, "%ld", x);

and think about why this code might crash on one compiler and not on another, and why it is more likely to crash on a 64 bit system.
 

Appleness

macrumors member
Original poster
Feb 26, 2009
34
1
Thanks for your replies.

I gave it for granted that if one code worked with GCC 4.0, it would too with 4.2. Now I see that I probably have wrong code anywhere.

I will check it all (thousands of lines, God!), and I will post if I find anything interesting.

Thanks again
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
Use the debugger. If you don't know how, then now might be a good time to learn. Especially compared to the time it will take you to manually inspect all your code.

If it's C or Obj-C code, you might also run it through the static analyzer in Xcode 3.2. Google keywords: xcode static analyzer.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Yeah, manual code inspection is rarely an efficient way to work. Use the debugger, GuardMalloc, etc... there are *tons* of tools for dealing with this stuff.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.