free() Failing

This is a discussion on free() Failing within the C Programming forums, part of the General Programming Boards category; Hi All I have program: Code: #include<stdio.h> void main() { char *pd,*ps="I LOVE CAKE"; int i = strlen(ps); pd=(char *)malloc(strlen(ps)); ...

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

    free() Failing

    Hi All

    I have program:
    Code:
    #include<stdio.h>
    void main()
    {
        char *pd,*ps="I LOVE CAKE";
    	int i = strlen(ps);
        pd=(char *)malloc(strlen(ps));
        strcpy(pd,ps);
        printf("%s",pd);
        free(pd);
    	getchar();
    }
    In this free() is failing. I am not able to understand why>
    Since pd has it know memory and I don't see any issue while deleting it.


    Can any body help in this?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    Observe this line:
    Code:
    pd=(char *)malloc(strlen(ps));
    So, you allocated strlen(ps) number of bytes, but ps has strlen(ps) + 1 number of characters. Hence, when you do this:
    Code:
    strcpy(pd,ps);
    You end up with a buffer overrun: the null character is copied, but no space was allocated for it. This leads to undefined behaviour. If you fix the malloc, your problem with free() might also be solved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Your main function needs to be "int main".

    Your string internally looks like this:

    'I' blank 'L' 'O' 'V' 'E' blank 'C' 'A' 'K' 'E' binary zero

    The binary zero at the end is added so all functions know where your string ends. So your string needs one byte of memory more than it's length.

    As you are one byte short, your functions write to a byte that does not belong to you. That's bad in itself and it will probably make your compiler issue some kind of warning when you free the memory.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Two problems:

    1) you don't include "stdlib.h" and "string.h"
    2) you allocate one byte too little (strlen returns the length of the string without the terminating '\0')

    What is the exact error message you get?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    88
    Oh Thanks.. I got it.
    I changed it and now problem solved.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Just a few things to note:
    What is the 'i' variable being used for?
    Void main is bad. Read more about why, here:void main(void) - the Wrong Thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 01:33 AM
  2. Why is setxattr failing?
    By rak1986 in forum C Programming
    Replies: 3
    Last Post: 01-28-2009, 05:38 AM
  3. failing RegisterClass
    By Bajanine in forum Windows Programming
    Replies: 6
    Last Post: 02-02-2008, 08:25 AM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. What's cooler than free boobs? 5 free assembly books from AMD.
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-13-2003, 07:22 PM

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