g++ 3.2.2 realloc bug?

Discussion in 'C++' started by Jonathan.Bailleul, Aug 8, 2003.

  1. Am I ignorant of bug releases and fixes, a bad man-reader, or is it a
    bug???

    In advance, thanks for your help.


    #include <iostream>
    #include <string>
    #include <math.h>


    using namespace std;


    int
    main(void)
    {

    int i, size = 5;
    int* tab;
    assert(tab = (int*) calloc(size, sizeof(int)));
    for (i = 0; i < size; i++) tab = i + 1;

    for (i = 0, cout << endl; i < size; i++) cout << tab << " ";

    size = 10;
    assert(tab = (int*) realloc(tab, size));
    for (i = 0, cout << endl; i < size; i++) cout << tab << " ";

    return EXIT_SUCCESS;
    }

    //1 2 3 4 5
    //1 2 3 4 16 0 139048 0 0 0


    // /users/these/bailleul/These/OpenSource/Tests>g++ --version
    //g++ (GCC) 3.2.2
    --
    -----------------------------------
    Jonathan BAILLEUL, Doctorant
    GREYC Image - Université de Caen
    http://www.greyc.ismra.fr/~bailleul
     
    Jonathan.Bailleul, Aug 8, 2003
    #1
    1. Advertising

  2. Ron Natalie wrote:
    >
    > "Jonathan.Bailleul" <> wrote in message news:...
    > > Am I ignorant of bug releases and fixes, a bad man-reader, or is it a
    > > bug???

    >
    > Nope your program has an error.
    >
    > > assert(tab = (int*) realloc(tab, size));
    > > for (i = 0, cout << endl; i < size; i++) cout << tab << " ";

    >
    > realloc takes a size in bytes. You forgot to multiply size by sizeof (int).


    Ok, I interpreted man at the letter without thinking. That was that...

    > It's also extremely bad form to put code in an assert that does something.


    I don't get it. Is that for the same reason of your following remark?

    > Your program will fail to invoke the realloc when asserts are disabled.


    Can we? My vision of the assert is to immediately interrupt the program
    if given condition is not fulfilled.

    --
    -----------------------------------
    Jonathan BAILLEUL, Doctorant
    GREYC Image - Université de Caen
    http://www.greyc.ismra.fr/~bailleul
     
    Jonathan.Bailleul, Aug 8, 2003
    #2
    1. Advertising

  3. Jonathan.Bailleul

    Ron Natalie Guest

    "Jonathan.Bailleul" <> wrote in message news:...


    > Ok, I interpreted man at the letter without thinking. That was that...
    >> It's also extremely bad form to put code in an assert that does something.

    >I don't get it. Is that for the same reason of your following remark?


    assert.h has the equivelent of

    #ifdef NDEBUG
    #define assert(ignore) ((void)0)
    #else
    #define assert(cond) ...something implementation specific...
    #endif

    Typically NDEBUG is defined for final (release mode) builds. This
    means that all the code that you put in the parameter to assert is
    not evaluated in that case.
     
    Ron Natalie, Aug 8, 2003
    #3
  4. > Code within an assert will be removed if NDEBUG is not defined.

    Sorry, if NDEBUG is defined.

    john
     
    John Harrison, Aug 8, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr T
    Replies:
    0
    Views:
    612
  2. Eitan Michaelson

    Impossible Leak realloc

    Eitan Michaelson, Jun 27, 2003, in forum: C++
    Replies:
    11
    Views:
    1,229
  3. Bren
    Replies:
    8
    Views:
    2,069
    Stephen Howe
    Sep 4, 2003
  4. Henrik J

    Realloc a struct **

    Henrik J, Nov 10, 2003, in forum: C++
    Replies:
    1
    Views:
    422
    David White
    Nov 10, 2003
  5. DrBob
    Replies:
    2
    Views:
    602
    Unforgiven
    Nov 26, 2003
Loading...

Share This Page