Thread: free()

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    free()

    ..
    I have 4 huge char segments allocated thru malloc..
    the program runs as reqd, in the end when i try to free them, two of them get freed, and then i get a segmeantation fault..

    two of them are of size 60,000 bytes, the other 2 are about 1000 bytes..
    any ideas as to waht can be going wrong ??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, you ran off the end of one of the allocated arrays, trashing vital information used by the memory pool to allow malloc and free to work properly.
    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.

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    i think,i have taken enough care that i dont cross the limit...will check anyhow..
    besides, the data i am handling is not as big as the space i have allocated..so, i guess there has to be some other reason... and i am not freeinfg freed pointer either..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Which OS/Compiler are you using?
    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.

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    OS : linux RED HAT
    compiler :GCC..

  6. #6
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    As a last resort, you can always pick up dmalloc and link it in with your program. It has good logging that will let you know what's going on.

    That is, of course, if a reading of the code doesn't turn up a problem first.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In which case do
    gcc -g prog.c -lefence
    gdb ./a.out

    When you run the code in the debugger, you'll hit a breakpoint at the first instruction which oversteps the bounds of it's allocated memory.

    One of the more common mistakes is believing
    int *p = malloc ( 50 );
    allocates space for 50 integers.

    Another common mistake is believing
    char *p = malloc( strlen(str) );
    allocates enough space to make a copy of str.

    A third common mistake is believing that realloc() never returns a different pointer to the one you passed to it.
    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.

  8. #8
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    that problem is more or less fixed..
    just out of curiosity, when we have used "malloc" , what are the other alternatives to free the mem segment allocated..
    how exactly does memset work ?? can we use it here ?

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    memset just sets bytes of memory to a specifieed value, it has nothing to do with memory allocation.

    If you are using malloc(), the only thing you want to use to free it, is free().

    By the way, valgrind is a useful tool under linux for diagnosing memory allocation/leak/heap corruption issues.

  10. #10
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    I habe learnt about the data structure heap, can someone explain how exactly the memory is allocated , when i call malloc().. and does using malloc() increase the build time

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > can someone explain how exactly the memory is allocated
    malloc is an abstraction for the real instruction to the OS to allocate some memory from the heap, so the operating system does all the work. malloc is guaranteed to return NULL or a pointer to at least the amount of space you asked for, so it's nothing for you to worry about how this is done.

    The instruction itself may be OS specific, but malloc is standardized: so I think the compiler just writes the correct instruction for malloc depending on the system you compiled the program under.

    > and does using malloc() increase the build time
    No. All memory is allocated at run time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM