question about runtime error when deallocating pointer in struct

This is a discussion on question about runtime error when deallocating pointer in struct within the C Programming forums, part of the General Programming Boards category; Simple question. I have a char pointer in a struct. When I try to free it, I get the following ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    question about runtime error when deallocating pointer in struct

    Simple question. I have a char pointer in a struct. When I try to free it, I get the following error at run time:

    a.out(1186) malloc: *** error for object 0x1ffc: Non-aligned pointer being freed
    *** set a breakpoint in malloc_error_break to debug

    Why?

    Code:
    typedef struct foo {
      char *str;
    } foo;
    
    void
    foo_free(foo *f) {
      free(f->str);
    }
    
    int
    main(int argc, char **argv) {
    
      foo *f = malloc(sizeof(foo));
      f->str = "foo";
      foo_free(f);
    
      return 0;
    
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should only free pointer returned by malloc, you cannot free pointer just because you like to write free here and there in the code

    f->str was not allocated with malloc - so you are not allowed to free it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Plus string literals should be const: http://cpwiki.sourceforge.net/Common...kes_and_errors
    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.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Elysia View Post
    Plus string literals should be const: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Who says that the only thing which will ever be assigned to "str" is a literal?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Change it accordingly. If you're going to assign a string literal, make it const.
    If you're going to use strcpy, then don't. The point is that the lesson needs to be taught. And often when someone assigns a string literals to non-const, they don't know what they're doing.
    Perhaps the faq should be updated with more information about string literals, though.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So this code is invalid according to you (not complete):
    Code:
    int main()
    {
       char *res = "Unknown";
       char buffer[100];
    
       int somestuff = somefunc();
       switch(somestuff)
       {
          case 1:
          case 2:
             sprintf(buffer, "Result = %d", somestuff);
             res = buffer;
             if ((somestuff = someotherfunc()))
                  strcat(res, " extra stuff");
             break;
           default: 
               // ... 
       }
       printf("Res=%s\n", res);
       return 0;
    }
    Yes, I'm sure it CAN be rewritten to use const wherever needed, but I can certainly see how you would want to do similar work in a function SOMETIMES.

    --
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    As I said, the point was more to learn about string literals.
    The point was that usually, when using char* to store a string literal, most newbies lack understanding of string literals and how they work. Yes, it's valid, but that doesn't mean you should use it, unless you find a good reason for doing it.
    I'm trying to get them a good idea of what string literals are, so they do not commit typical mistakes.
    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
    But surely, the issue here is freeing something that didn't come from malloc, not about const.

    --
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Yes, yes, absolutely. That is the error, but a little lesson about string literals doesn't hurt either
    I also understand it as that the OP is not understanding string literals correctly and therefore tried to free it.
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yes, yes, absolutely. That is the error, but a little lesson about string literals doesn't hurt either
    I also understand it as that the OP is not understanding string literals correctly and therefore tried to free it.
    I'd say the OP doesn't understand memory management. String literals is just one of many circumstances when there is a poitner to non-malloc'ed memory.

    --
    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.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    That too. String literals is just one part of the whole package.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Passing a struct via pointer question
    By Thantos in forum C Programming
    Replies: 3
    Last Post: 11-29-2003, 06:54 PM

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