Thread: Help: double free corruption??

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Unhappy Help: double free corruption??

    Code:
    int main()
    {
            int *a,i;
            a=(int *)malloc(10*sizeof(int));
            for(i=0;i<10;i++)
                    *(a+i)=i*i;
            for(i=0;i<10;i++)
                    printf("%d",*a++);
            free(a);
    return 0;
    }
    why this code is showing double free corruption?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It may not be a double free per se, but you are incrementing a. Rather, have another pointer to be a copy of a, and increment that. That way, free(a) will operate on the original location pointed to by a.
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This smells like a test question.

    What is a? What is its value prior to the final loop? What about after? How can that affect the free operation?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Thanks laser

    I have used a+i in place of a++ it fine now.

    But another doubt

    Code:
    main()
    {
        double d;
        printf("%d", ((double *)0+2));
    }
    How this code is giving 16?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because %d does NOT print doubles.

    r2r is right, you're just trotting off quiz questions.
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    He's printing a pointer value there. 16 would be the expected offset from NULL.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by GOGO1104 View Post
    Thanks laser

    I have used a+i in place of a++ it fine now.

    But another doubt

    Code:
    main()
    {
        double d;
        printf("%d", ((double *)0+2));
    }
    How this code is giving 16?
    It's not a doubt, it's a question.

    I think whoever is teaching English in that part of the world needs to find another career.

    Don't worry, I am not talking about you but of the person that taught you English.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    %d does not print pointers either. That's undefined behavior.
    Furthermore, main must have a return type of main and a return 0; at the end.
    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. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  2. double free or corruption (out)
    By Mouser58907 in forum C Programming
    Replies: 5
    Last Post: 02-25-2009, 12:20 AM
  3. Replies: 7
    Last Post: 11-26-2007, 01:11 PM
  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. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM

Tags for this Thread