Thread: curious code

  1. #1
    Registered User
    Join Date
    Apr 2002

    Question curious code

    Ok, this might take some explaining. here goes.
    In my program the user enters a full filepath and the program takes the filename and outputs access time, modify time, and size to screen and file until the user enters quit.

    I ran into a segmentation fault just after the beginning of my main while loop (where user enters the filenames). the weird thing is that after i entered the third filename the seg fault happened.
    (why 3?) also i put an ordinary cout<<"1\n"; at the very beginning of the while loop and my problems were solved.

    why did this cout fix my segmentation fault?

    If i my explanation is confusing or if any ?'s email me at or post a message here |


  2. #2
    Registered User
    Join Date
    Apr 2002
    I'm not sure what you will need to know, so here is the source.

    FYI: This is my first attempt at a useful program.
    I'm not sure if i am even going in the right direction. i'm doing this without much help.

    its messy.

  3. #3
    Registered User
    Join Date
    Apr 2002
    also, i removed the cout and the segmentation fault didnt come back i dont know why.
    the loop starts at line 107

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >line 149 (maybe others): while(!feof(fdtmp))
    don't control the loop in this manner, use the return code from the function doing the reading.

    >line 36: char IOFilename[33];
    >line 165: filename[50]
    The lengths are different.

    >line 182: strcpy(n_ptr->IOFilename,filename);
    If I'm following your code correctly, I think both these pointers point to the same memory? Is the result undefined behaviour?

    Hope this helps a little!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > struct Frec *s_ptr=(struct Frec *)malloc(sizeof(Frec));// initializes the linked list
    This isn't the best way to initialise a linked list - you're forcing the user to have at least one node in the list

    An empty list is
    struct Frec *s_ptr = NULL;

    In addition, this being C++, you don't need struct, so

    Frec *s_ptr = NULL;

    Finally, you should be using new/delete for memory allocations in C++

    Frec *s_ptr = new Frec;

    > FILE *fdtmp=fopen("crdet.init","r");
    > FILE *fd=fopen("crdet.init","a");
    My initial impression is that fd will always be NULL - you're opening (always) the same file using two incompatible modes.

    I suggest you open the file in the appropriate mode, when you've decided which command line option you're dealing with

    It would help to know which command line option you tried.

  6. #6
    Registered User
    Join Date
    Apr 2002
    the command line i used was: ./crdet -init
    then i used /bin/ls
    those are what i used to test input and such.

    thanks for the help on linked lists i don't know them well so this was just an experiment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM