Thread: General question about undefined behavior

  1. #46
    Registered User
    Join Date
    May 2013
    Location
    United States
    Posts
    22
    Well, I got a interesting reply on comp.os.msdos.djgpp.... from someone there. He made some the same complaints about my code , as everyone did here, but he did make one suggestion that I thought I would share here :


    My next guess would be that your buffers sizes are too small or
    incorrect. Does changing PARSE_SIZE_LIMIT to a larger value, say
    512 or 4096, fix the problem? If so, your buffers are too small.

    <sniped>

    Other suggestions or possible issues in the code below.

    > #define PARSE_SIZE_LIMIT 81



    #define PARSE_SIZE_LIMIT 81

    This should probably be split into two defines. This is to make
    sure the allocated buffer or character array is _always_ larger
    than the quantity read or written to the buffer. Use one define
    for the character arrays and another for the buffer size. They
    should be slightly larger by at least two chars for the DOS
    newline:

    #define PARSE_SIZE_LIMIT 80
    #define BUFFER_SIZE 82

    So, any place you declare a variable that used PARSE_SIZE_LIMIT,
    it would be changed to use BUFFER_SIZE:

    Change:
    char variable[PARSE_SIZE_LIMIT]

    To:
    char variable[BUFFER_SIZE]

    This would be done for the declarations of the following
    variables:
    target, worda, uword in main()
    subject in string_parser()
    line in kfgets()

    Leave the other non-declaration uses of PARSE_SIZE_LIMIT alone.
    I am not sure if GCC in DJGPP treats my newline character as it is in Linux (LF) or in MS-DOS (CR\LF).. since it is GCC ported from linux. He also forgot to +1 for the null character '\0'.

  2. #47
    Registered User
    Join Date
    May 2013
    Location
    United States
    Posts
    22
    Heres a good guide ( although they didn't provide a example to show it ) for strtok();

    strtok - cppreference.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined behavior
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 02-18-2013, 11:14 PM
  2. Static vs. Dynamic Arrays, Getting Undefined Behavior
    By StefPrez in forum C++ Programming
    Replies: 11
    Last Post: 01-28-2012, 11:39 PM
  3. Is x=x++; Undefined Behavior?
    By envec83 in forum C Programming
    Replies: 5
    Last Post: 10-04-2011, 01:27 AM
  4. Undefined behavior from VC6 to 2k5
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2011, 07:56 PM
  5. openGL: textures, gluLookAt, and undefined behavior
    By MK27 in forum Game Programming
    Replies: 7
    Last Post: 04-28-2009, 10:12 AM

Tags for this Thread