Thread: sprintf() giving crash to program!!

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    sprintf() giving crash to program!!

    Hi All,
    Wish u a very happy new year 2006.

    I am programming in DirectFB, it is used to make GUI in Linux without the support of XServer. In my program i have to use sprintf() function to address some values in a pointer. But due to using Sprintf() my program is going to crash without showing GUI. so plz tell me whts ging wrong with my code.

    Code:
    //getting Hour, Minute and second. # is a delimeter  for separation between different values. Eg. 00#01#02#...... 
    sprintf(ptr,"#%d#%d#%d#",hour,min,sec);
    
    //Function creating GUI and using ptr value inside it.
    DIrectFB_initilization_function();
    
    //Hamdling keyboard and mouse events.
    Event_handling_function();
    
    //at last when user exit then release the frame buffer
    release_function();
    i think if i use internal funtionality of sprintf() then it can be fine. But how to put '#10' in a variable.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    sprintf might crash if your buffer (ptr) isn't long enough to hold the data. How are you declaring ptr? Is it a character array? (char ptr[20]) Or allocated memory?

    We'll need to see a bit more code to be sure (namely ptr's declaration), but it's likely that you've either not allocated memory or not enough memory.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Quote Originally Posted by Cactus_Hugger
    sprintf might crash if your buffer (ptr) isn't long enough to hold the data. How are you declaring ptr? Is it a character array? (char ptr[20]) Or allocated memory?

    We'll need to see a bit more code to be sure (namely ptr's declaration), but it's likely that you've either not allocated memory or not enough memory.
    HI,

    see i tried in both the ways. but i have seen any printf() or sprintf(). is not working there. it just giving a crash with saying process killed at some signal.

    i think if i ll put value directly in a array or pointer. like
    a[0]= hour;
    a[1]=min;
    a[2]=sec;

    then it could work.. but the problem is i have to store a delimeter also like
    a[0]=#hour;

    Now tell me how can i do it.
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by maven
    HI,

    see i tried in both the ways. but i have seen any printf() or sprintf(). is not working there. it just giving a crash with saying process killed at some signal.

    i think if i ll put value directly in a array or pointer. like
    a[0]= hour;
    a[1]=min;
    a[2]=sec;

    then it could work.. but the problem is i have to store a delimeter also like
    a[0]=#hour;

    Now tell me how can i do it.

    I'm making some HUGE assumptions here, because you haven't really posted enough code to "fill in the blanks."

    If what you are trying to accomplish is something like this:

    Code:
    char ptr[20];
    
    sprintf(ptr,"#%d#%d#%d",hour, min, second);
    Then what you will end up with (assuming of course that your ptr buffer is large enough) would be something like this:

    ptr[0] = '#'
    ptr[1] = 12 /*assuming hour is 12 */
    ptr[2] = '#'
    ptr[3] = 38 /* assuming minute is 38 */


    So, in other words, you won't have something like:

    ptr[0] = "#hour"

    If you want something like that, you'd probably need an "array of strings" (generically speaking).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well until you figure out the need to allocate memory, all your programming efforts are doomed.

    You're simply going to have to post more code than the line which it's failing on - preferably a complete program which compiles and then fails, so that we can be sure everyone is running the same thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Replies: 2
    Last Post: 05-10-2009, 11:12 PM
  3. Problem using structures (they crash my program)
    By shadow1515 in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 08:36 AM
  4. Why Does My Program Crash?
    By Grantyt3 in forum C++ Programming
    Replies: 20
    Last Post: 09-29-2005, 05:34 PM
  5. Why does the program crash?
    By SkyRaign in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2005, 10:06 AM