g++ 3.2.2 realloc bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan.Bailleul

    g++ 3.2.2 realloc bug?

    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] = i + 1;

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

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

    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

  • Jonathan.Bailleul

    #2
    Re: g++ 3.2.2 realloc bug?

    Ron Natalie wrote:[color=blue]
    >
    > "Jonathan.Baill eul" <bailleul@greyc .ismra.fr> wrote in message news:3F33C1D3.8 53623A2@greyc.i smra.fr...[color=green]
    > > Am I ignorant of bug releases and fixes, a bad man-reader, or is it a
    > > bug???[/color]
    >
    > Nope your program has an error.
    > [color=green]
    > > assert(tab = (int*) realloc(tab, size));
    > > for (i = 0, cout << endl; i < size; i++) cout << tab[i] << " ";[/color]
    >
    > realloc takes a size in bytes. You forgot to multiply size by sizeof (int).[/color]

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

    I don't get it. Is that for the same reason of your following remark?
    [color=blue]
    > Your program will fail to invoke the realloc when asserts are disabled.[/color]

    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

    Comment

    • Ron Natalie

      #3
      Re: g++ 3.2.2 realloc bug?


      "Jonathan.Baill eul" <bailleul@greyc .ismra.fr> wrote in message news:3F33D433.1 B12D6B3@greyc.i smra.fr...

      [color=blue]
      > Ok, I interpreted man at the letter without thinking. That was that...[color=green]
      >> It's also extremely bad form to put code in an assert that does something.[/color]
      >I don't get it. Is that for the same reason of your following remark?[/color]

      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.


      Comment

      • John Harrison

        #4
        Re: g++ 3.2.2 realloc bug?

        > Code within an assert will be removed if NDEBUG is not defined.

        Sorry, if NDEBUG is defined.

        john


        Comment

        Working...