Thread: malloc/free behavior

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    malloc/free behavior

    Code:
    char *query;
    
    for(i=0;i<sizeof(tables)/sizeof(char *);i++)
            {
             query=malloc(39-sizeof(tables[i]));
            sprintf(query,"delete from %s where rowid=$1::int8;",tables[i]);
            printf("%s\n",query);
            //free(query);
            }
    }

    I've read somewhere that with every call to malloc there should be one to free. Why does above code fails if free is uncommented?
    Last edited by mr.wu; 07-05-2010 at 08:43 AM. Reason: change malloc line from a test line to actual line

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps because you're causing a buffer overrun?
    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.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    You are right. I change 39 to 41 and it works.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    correct thing should have been 35+sizeof

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that I would do something like:
    Code:
    const char query_[] = "delete from  where rowid=$1::int8;"
             query=malloc(sizeof(query_)+strlen(tables[i]) /* null terminator counted because of sizeof */);
            sprintf(query,"delete from %s where rowid=$1::int8;",tables[i]);
            printf("%s\n",query);
    I don't know what table[i] is, though. It may be a mere char pointer, so I used strlen. If it's an array in the local function, you may use sizeof. Otherwise you cannot.

    This should ensure you have enough space.
    I doubt that your calculation is anywhere near right.
    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
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    looks better since it does not use magic number.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Don't forget to check the returned pointer from malloc against NULL!
    Last edited by kermit; 07-05-2010 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New/delete vs malloc/free
    By KBriggs in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2009, 03:08 PM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. redirect malloc/free
    By pheres in forum C Programming
    Replies: 0
    Last Post: 03-21-2009, 03:28 AM
  4. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM
  5. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM