sprintf() and SDK 10.6

Discussion in 'Mac Programming' started by Appleness, Nov 12, 2009.

  1. Appleness macrumors member

    Joined:
    Feb 26, 2009
    #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
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    post your code. this is pretty vague, and without more information we can't really do much for you.

    -Lee
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    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).
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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.
     
  5. Appleness thread starter macrumors member

    Joined:
    Feb 26, 2009
    #5
    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
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    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.
     

Share This Page