Why do I have a core dump ?

Discussion in 'C Programming' started by QQ, Jun 1, 2005.

  1. QQ

    QQ Guest

    I can sense that I get wrong with char *name,
    however, how to fix it ?

    Thanks a lot!
    int Roamer_Create(const char *dbname)
    {
    char *name;
    printf("dbname is %s\n",dbname);
    sprintf(name,"CREATE DATABASE %s",dbname);
    printf("command is %s\n",name);
    return 1;
    }


    main()
    {
    Roamer_Create("Roamer");
    }
     
    QQ, Jun 1, 2005
    #1
    1. Advertising

  2. QQ

    -berlin.de Guest

    QQ <> wrote:
    > I can sense that I get wrong with char *name,
    > however, how to fix it ?


    > int Roamer_Create(const char *dbname)
    > {
    > char *name;


    Now you have a pointer that points to some random place in memory.
    If you're lucky it points to a place that you have no permissions
    to access.

    > printf("dbname is %s\n",dbname);
    > sprintf(name,"CREATE DATABASE %s",dbname);


    Now you try to write to that random place in memory. You're lucky
    that the program crashed instead of blissfully overwriting some
    important data of your program, making it look like everything is
    ok before crashing horribly (and unexplainably) half an hour later...

    To get rid of the problem you need to make 'name' point to some
    memory you own, large enough to hold the string you want to put
    there (including the the terminating '\0' character.

    You could do use e.g.

    if ( ( name = malloc( strlen( "CREATE DATABASE " ) +
    strlen( dbname ) + 1 ) ) == NULL ) {
    fprintf( stderr, "Running out of memory!\n" );
    exit( EXIT_FAILURE );
    }

    before you call sprintf(). Don't forget to include <stdlib.h>, it's
    needed for both the definition of malloc() and the define for the
    macro EXIT_FAILURE.

    > printf("command is %s\n",name);
    > return 1;
    > }


    > main()


    This should be

    int main( void )

    since main() is always supposed to return an int and unless you declare
    it to do so you'll get into trouble with C99 compilers.

    > {
    > Roamer_Create("Roamer");
    > }


    And since main() must return an int you also should have a return
    statement at the end.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jun 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    0
    Views:
    738
  2. halfdog
    Replies:
    12
    Views:
    12,585
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  4. Army1987

    Why does this dump core?

    Army1987, May 12, 2007, in forum: C Programming
    Replies:
    7
    Views:
    396
    Army1987
    May 24, 2007
  5. Gancy
    Replies:
    1
    Views:
    130
    Anno Siegel
    Feb 6, 2005
Loading...

Share This Page