can't free struct

This is a discussion on can't free struct within the C Programming forums, part of the General Programming Boards category; Boy, it's been a few days of crazy free() trips here for me... Anyway, when I do this: Code: int ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    can't free struct

    Boy, it's been a few days of crazy free() trips here for me...

    Anyway, when I do this:
    Code:
    int main () {
          struct in_addr *address=malloc(sizeof(struct in_addr));
          struct hostent *test=malloc(sizeof(struct hostent));
          if ((test==NULL) || (address==NULL)) exit (-1);
          [...struct are both filled and used]
          free(address);
          free(&test);    // alt version
    Both these free() calls cause a double-free/corruption error, which means I cannot free the memory allocated to either struct?
    Last edited by MK27; 11-27-2008 at 01:03 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Literally remove the portion that you omitted from your example. Do you still get the error?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Usually it's right on one or the other:
    * Double free
    * Corruption

    It is likely that some of the code you have omitted is overwriting the end of your buffer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Code:
    free(&test);    // alt version
    Is incorrect, because you need to free the pointee, not the pointer itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Usually it's right on one or the other:
    * Double free
    * Corruption

    It is likely that some of the code you have omitted is overwriting the end of your buffer.

    --
    Mats
    So in other words freeing() the pointers corrupts vital memory that got overwritten into where the structs are allocated?
    I thought free() just affected the pointer and not the memory...but okay.
    Nothing happens after the free() except a final return. I tried laserlight's recommendation and yep, it must be some other part of my code. Guess I better get on with installing valgrind or use a bigger font...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Quote Originally Posted by MK27
    I tried laserlight's recommendation and yep, it must be some other part of my code.
    That's weird though. In view of Elysia's observation, which appears correct, I would expect the problem to remain. Perhaps it merely did not surface except in the presence of the code you omitted. Undefined behaviour is very problematic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by MK27 View Post
    So in other words freeing() the pointers corrupts vital memory that got overwritten into where the structs are allocated?
    No, it does not...

    I thought free() just affected the pointer and not the memory...but okay.
    That is correct...

    Usually, some corruption occurs in the application. Like the control structures that tells free how big that allocated block is, etc, has been corrupted, overwritten or something like that.
    Or you could have freed that memory block once already, and so doing it twice causes an error (since the internal information is now gone).

    These errors are usually easy to find with a good debugger.
    For the second probability, set all pointers to NULL after freeing them to avoid double-free. Also use a good debugger that marks freed memory with a known pattern (Visual Studio does this), so you can see if you are double-freeing.
    For the first probability, use a breakpoint to break if 1-4 bytes before and after your allocated memory gets written. That will find you the source of the corruption.

    A good debugger will do all of this. Visual Studio certainly can.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    So in other words freeing() the pointers corrupts vital memory that got overwritten into where the structs are allocated?
    I thought free() just affected the pointer and not the memory...but okay.
    Nothing happens after the free() except a final return.
    malloc and free both uses a small amount of data that is before and/or after the data you actually get back - let's call it metadata. This is the "admin" side of malloc/free. If this metadata is overwritten by broken code, then free may well say "double free or corruption", because it validates the content before it allows the memory to be officially freed. So, for example, if you allocate a string with space for 10 characters, and write a 14 character string into it, you may well get this type of error.

    I think Elysia's point is a good'un tho'. You should probably not pass the address of the pointer, but the pointer itself. I should have spotted that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, it's not been freed before so, it's not really a double free, so...

    Quote Originally Posted by matsp View Post
    malloc and free both uses a small amount of data that is before and/or after the data you actually get back - let's call it metadata. This is the "admin" side of malloc/free. If this metadata is overwritten by broken code, then free may well say "double free or corruption", because it validates the content before it allows the memory to be officially freed.
    Mats
    Just to make sure I have it straight, if this "admin" info (which is attached to the struct) is overwritten, then trying to free the struct could cause that error?

    Thanks all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by MK27 View Post
    Well, it's not been freed before so, it's not really a double free, so...
    You don't know that.

    Just to make sure I have it straight, if this "admin" info (which is attached to the struct) is overwritten, then trying to free the struct could cause that error?

    Thanks all.
    Yes, it would.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Just to make sure I have it straight, if this "admin" info (which is attached to the struct) is overwritten, then trying to free the struct could cause that error?

    Thanks all.
    Correct. But did you fix the extra & sign that shouldn't be there?

    Also, why are you allocating a hostent structure in the first place. As long as you only need ONE hostent, there's extremely little reason to allocate the memory in the first place. It is, after all only 4 pointers (16/32 bytes in a 32/64-bit machine) and two integers (8 bytes in nearly all machines).

    And in_addr is even smaller - 4 bytes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    You don't know that.
    Who but me would have freed() it?

    Quote Originally Posted by matsp View Post
    Correct. But did you fix the extra & sign that shouldn't be there?

    Also, why are you allocating a hostent structure in the first place. As long as you only need ONE hostent, there's extremely little reason to allocate the memory in the first place. It is, after all only 4 pointers (16/32 bytes in a 32/64-bit machine) and two integers (8 bytes in nearly all machines).

    And in_addr is even smaller - 4 bytes.

    --
    Mats
    I wasn't really using that &, I was just covering a personal uncertainty

    vis. NOT allocating the memory, what then: isn't there the danger of the same problem if those 24 bytes are written somewhere "unallocated"? in_addr is the same size as a pointer, but that doesn't mean those 4 bytes will be written into the pointer does it?

    Or (this is a real question): if I'm using a pointer to structure like this but the logic of the program is such that the info from the struct gets used or copied out in the very next set of instructions, so it doesn't matter what happens to it, mean that I don't need to allocate memory for the return value of function before calling it because this unallocated returned data can't overwrite anything else because it was staticly allocated as part of the function, is now free, but still exists uncorrupted because there are no instructions writing anything to memory?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Who but me would have freed() it?



    I wasn't really using that &, I was just covering a personal uncertainty

    vis. NOT allocating the memory, what then: isn't there the danger of the same problem if those 24 bytes are written somewhere "unallocated"? in_addr is the same size as a pointer, but that doesn't mean those 4 bytes will be written into the pointer does it?

    Or (this is a real question): if I'm using a pointer to structure like this but the logic of the program is such that the info from the struct gets used or copied out in the very next set of instructions, so it doesn't matter what happens to it, mean that I don't need to allocate memory for the return value of function before calling it because this unallocated returned data can't overwrite anything else because it was staticly allocated as part of the function, is now free, but still exists uncorrupted because there are no instructions writing anything to memory?
    Using unallocated memory is Bad. Very Bad.

    But malloc is so far past overkill for this problem, you can't even see overkill from where you are. Why declare a pointer and keep malloc'ing space for it and freeing it over and over again, when you could just declare an actual struct in the first place?

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Using unallocated memory is Bad. Very Bad.

    But malloc is so far past overkill for this problem, you can't even see overkill from where you are. Why declare a pointer and keep malloc'ing space for it and freeing it over and over again, when you could just declare an actual struct in the first place?
    I guess you make a fine point right here. Technically, it's because the function calls for a pointer, and I thought declaring a struct and casting it as a pointer and declaring a pointer then allocating it a struct were exactly the same thing, but I suppose there is some stack vs. heap difference or something...

    On the other hand, I would never have noticed my evident memory corruption problem without this

    ps. you didn't exactly answer my last question, but of course you don't have to either
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    I guess you make a fine point right here. Technically, it's because the function calls for a pointer, and I thought declaring a struct and casting it as a pointer and declaring a pointer then allocating it a struct were exactly the same thing, but I suppose there is some stack vs. heap difference or something...

    On the other hand, I would never have noticed my evident memory corruption problem without this

    ps. you didn't exactly answer my last question, but of course you don't have to either
    Well I have no idea exactly what function you refer to, since all I can find for the code is "...". All we know about that code is that (a) you call a function and (b) you overwrite some memory somewhere in it. And the bug would have seemed different, since instead of overwriting the part of memory that free() needed to do its job, you would have overwritten maybe another variable instead. (And you wouldn't cast the struct to a pointer -- you would just hand the function the address of the variable instead.)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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