Thread: problem with free()

  1. #1
    Registered User
    Join Date
    Dec 2009

    problem with free()

    Could anyone explain to me what I am doing wrong here please?
    void testFree(){
    	char * s=(char *)calloc(sizeof(char),5);
    	sprintf(s, "[%d]",321);
    I get this error:
    problem with free()-2150ody-png

  2. #2
    Registered User
    Join Date
    Sep 2011
    Stockholm, Sweden
    The string you are writing to s contains 6 elements (remember the terminating null character), but you only allocated memory for 5.

  3. #3
    Registered User
    Join Date
    Dec 2009
    ok, I thought I'd forgotten someting having to do with EOS. Thx!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > printf(s);
    Also, get into the habit of ALWAYS specifying a const format string for printf, even if it is just "%s".

    If s were user input, any % characters would cause all sorts of havoc.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have a problem with free();
    By nik2 in forum C Programming
    Replies: 12
    Last Post: 03-07-2010, 02:09 AM
  2. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  3. free() problem
    By audinue in forum C Programming
    Replies: 19
    Last Post: 07-19-2008, 11:18 PM
  4. buy one get one free problem
    By Nick_365 in forum C++ Programming
    Replies: 29
    Last Post: 02-12-2006, 02:17 AM
  5. free problem with struct
    By Massive in forum C Programming
    Replies: 8
    Last Post: 07-29-2003, 06:51 AM