free and seg fault

This is a discussion on free and seg fault within the C Programming forums, part of the General Programming Boards category; Hi, I want to prase the options twice but no luck. It seg fault at free. See the code attached....

  1. #1
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18

    free and seg fault

    Hi,

    I want to prase the options twice but no luck. It seg fault at free. See the code attached.
    Attached Files Attached Files

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    The address in the pointer is invalid. Debug and check where it goes illegal.
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    in the code you post, the pointers didn't point to a memory allocated from the heap, they point to a memory in your 'data' or 'code' segment, depending on the compiling options you've used at compile time. so...
    >>> free(parse_argv[i]);

    when you try to free them - it chashes ...
    just avoid freeing memory which you didn't allocate it previously.

  4. #4
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18
    how to allocate memory from the heap for parse_argv?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > // why segmentation fault in free?
    There is no need to call free, because you haven't called malloc.

    > // why second parse has no result?
    Not sure - but as far as getopt is concerned, the array of pointers is the same. Maybe getopt has some internal state which causes it to remember where it got to the last time it was called.
    You would need to read the manual page to find out how to reset this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    dkt
    dkt is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    18
    Originally posted by Salem
    > // why segmentation fault in free?
    There is no need to call free, because you haven't called malloc.

    I see...

    > // why second parse has no result?
    Not sure - but as far as getopt is concerned, the array of pointers is the same. Maybe getopt has some internal state which causes it to remember where it got to the last time it was called.
    You would need to read the manual page to find out how to reset this.
    Yes, yes and yes. getopt has an integer (optind) which remembers how many times it has parsed. getopt is designed to parse command line argument and optind =0 at start. So I have to reset optind to 0 every time I parse. It's seg fault and I thought this is a memory problem in other places!

    Thank you for helping me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. seg fault in vector delete function
    By tytelizgal in forum C Programming
    Replies: 3
    Last Post: 10-18-2008, 04:45 PM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  5. Why do I get seg fault with large input?
    By Captain Penguin in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2002, 08:39 PM

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