sprintf crashing my prog?

This is a discussion on sprintf crashing my prog? within the C++ Programming forums, part of the General Programming Boards category; Ive used sprintf plenty of times, but here im useing it the the argv[] array in a console prog. char ...

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

    sprintf crashing my prog?

    Ive used sprintf plenty of times, but here im useing it the the
    argv[] array in a console prog.

    char * texname;

    int main(int argc, char **argv) {
    if(argc < 2) {
    cout << "You must specify the index\n";
    exit(0);
    }
    sprintf(texname,"%s.jpg",argv[1]); //Crahs here


    Im useing a simple .bat file to launch the prog, something like this

    program.exe 0

    It just crashes on that one line everytime.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    uhm... you are writing to a pointer without memory allocated.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User Hoxu's Avatar
    Join Date
    Nov 2001
    Posts
    25
    texname = new char[80];

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thats probably why im haveing problems in my big prog, It could be attributed to memory overwrites.

    is there something i can use in place of char* or can you show me how to use it correctly.

    Given this function body.

    char* string = "";
    switch(game.mode) {
    case MODE_MENU:
    menu.Draw(mode);
    if(game.debug) sprintf(string, "MENU %i", glh.fps);
    break;
    if(game.debug) gui.Printl(1,string,7,-4.8,4.5);

    how can i allocate the pointer, and then deallocate it when i close the prog.

    I have 40+ source files, I do have a user defined exit function i can use. What im getting at is that i need to be able to delete a pointer in one source file that is created in a different one.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could either allocate the memory dynamically like Hoxu has shown or use a static char array that you think will be long enough -

    char string[256];

    otherwise -

    char* string;
    //to allocate
    string = new char[strlen(glh.fps)+1];
    //to deallocate
    delete [] string;
    zen

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    It is my understanding that memory allocated with the new operator is not affected by scope like variables declared with static memory are. Therefore memory allocated by new in one function can be deallocated in a different function.

    void function1()
    {
    char * word = new char[80];
    strcpy(word, "Jim Dandy");
    function2(word);
    }

    void function2(char *)
    {
    cout << word;
    delete [] word;
    word = NULL;
    }

    I hope I understood your question correctly.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The memory isn't but the pointer to that allocated memory is still affected by scope.If that pointer is lost then you cant delete the memory and you get a memory leak.
    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

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Which } does the compiler see first in the following sequence?

    void function1()
    {
    char * word = new char[80];
    strcpy(word, "Jim Dandy");
    function2(word);
    } //first }

    void function2(char * Word)
    {
    cout << Word;
    delete [] Word;
    Word = NULL;
    } //second }

    It is my understanding that function2 needs to be completed and that the compiler actually sees the second } before it sees the first }, therefore char * word (aka Word) doesn't go out of scope until after the memory has been deleted, and therefore there is no memory leak, correct?

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It is my understanding that function2 needs to be completed and that the compiler actually sees the second } before it sees the first }, therefore char * word (aka Word) doesn't go out of scope until after the memory has been deleted, and therefore there is no memory leak, correct?
    Yes, there wouldn't be a memory leak. However, setting Word to NULL in function2,will have no affect on the pointer in function1, as function2 will get a local copy. word in function1 will still point to the deleted memory.
    zen

  10. #10
    Registered User Hoxu's Avatar
    Join Date
    Nov 2001
    Posts
    25
    Originally posted by zen
    //to deallocate
    delete [] string;
    what for does that "[]" stand for?

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    when you use new to allocate memory for an object you must free that memory with delete.
    when you use new[] to allocate memory for an array of objects then you must correspondingly use delete[] to free the memory.
    If you use pObject= new object[10] and then free it like this... delete pObject. Then you have a memory leak. An array of 10 objects was defined yet the delete only called a destructor for the first element of that array. Correctly freeing that memory looks like this... delete[] pObject . Now delete knows that it is freeing an array of objects rather than just one.
    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

  12. #12
    Registered User Hoxu's Avatar
    Join Date
    Nov 2001
    Posts
    25
    Ok, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST inser method seems to be crashing my prog
    By ray_broome in forum C++ Programming
    Replies: 12
    Last Post: 04-22-2005, 01:02 PM
  2. How do I stop cin from crashing my prog
    By Panopticon in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2004, 03:41 PM
  3. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Crashing on prog quit
    By Hunter2 in forum Game Programming
    Replies: 11
    Last Post: 09-07-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21