quick question

This is a discussion on quick question within the C Programming forums, part of the General Programming Boards category; just dug a program out of the grave. wrote it some time ago. i remember that it would run fine ...

  1. #1
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    quick question

    just dug a program out of the grave. wrote it some time ago. i remember that it would run fine on my machine (dos proggie in w98 dos box) but other people reported crashes. so i dug it out of my archives today and scanned through it, spotted this:

    Code:
    /* snip */
    int main (void)
    {
      char *filenom;
      int i;
      /* snip */
      sprintf (filenom, "samp%3i.wav", i + 1);
      /*snip */
    }
    /* snip */
    assuming that i is initialized correctly, would this cause a problem because i havent allocated any space for filenom?? i'm not the greatest with C or pointers but i think that's it.....

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    yes

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    ditto.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    thanks!

    i replaced it with

    Code:
    char filenom[12];

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >would this cause a problem because i havent allocated any space for filenom??
    Yes, because you are trying to assign data to memory that you don't own (ie. the random value that filenom starts with since it is local and uninitialized). In such a case you should either assign memory to filenom with malloc, calloc, or realloc, or just use a static array. Though static arrays will limit the flexibility of your program.

    To avoid the little crashing problem, initialize your pointers to NULL so that you will always get a run-time error when you try to assign to it. This tips you off that something not good is happening and you can fix it before distributing the program.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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