Thread: alternate function to sprintf

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    alternate function to sprintf

    Ive learned how to use the debugger and ive found that sprintf() is the root of all the access violations im getting.


    is there another simple function that can do about the same thing?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    A) Do you have enough memory allocated for the buffer?

    B) You can use stringstreams.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ive learned how to use the debugger and ive found that sprintf() is the root of all the access violations im getting.
    Perhaps your use of sprintf is the root of your access violations, but I seriously doubt that sprintf itself is to blame. Show us your code and we'll see if there's a problem you aren't seeing.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    OK, I dont think my standard library is 'broken', the program runs fine unless debugging, where my call to sprintf

    sprintf(strLoadCard, "Cards Loaded = %i Percent", cards);

    calls to write_char in OUTPUT.C (A standard library file) where an access violation occurs

    cards is a integer that has been initilized and has a current value of 0; strLoadCard has been declared in the objects constructor as

    strLoadCard = new char[500];
    strLoadCard = "";

    in the destructor I have this

    delete [] strLoadCard;

    and the object its self has this as a member

    char* strCardLoad;


    this is the function where the error occurs for what its worth

    LOCAL(void) write_char (
    int ch,
    FILE *f,
    int *pnumwritten
    )
    {
    if (_putc_lk(ch, f) == EOF) <<< Unhandled Exception - Access Violation
    *pnumwritten = -1;
    else
    ++(*pnumwritten);
    }

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    strLoadCard = "";

    this is a problem. U have gotten a pointer to a dynamically sized piece of memory then you repoint it to a static string containing just a null byte. You have lost the memory u got from new by doing this.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    411



    duh. I know that, I cant believe I actually posted this....



    To make it worthwhile, can anyone tell me how I can use the debugger to track any memory leaks?
    Last edited by Eber Kain; 12-27-2002 at 10:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM