Help: double free corruption??

This is a discussion on Help: double free corruption?? within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    %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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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-24-2009, 11:20 PM
  3. Replies: 7
    Last Post: 11-26-2007, 12: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


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