Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > g++ 3.2.2 realloc bug?

Reply
Thread Tools

g++ 3.2.2 realloc bug?

 
 
Jonathan.Bailleul
Guest
Posts: n/a
 
      08-08-2003
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
http://www.greyc.ismra.fr/~bailleul
 
Reply With Quote
 
 
 
 
Jonathan.Bailleul
Guest
Posts: n/a
 
      08-08-2003
Ron Natalie wrote:
>
> "Jonathan.Bailleul" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> > 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[i] << " ";

>
> 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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      08-08-2003

"Jonathan.Bailleul" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...


> 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.


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-08-2003
> Code within an assert will be removed if NDEBUG is not defined.

Sorry, if NDEBUG is defined.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Question about new vs malloc and realloc. DrBob C++ 2 11-26-2003 10:17 PM
JNI:Solaris 9-Signal 11 in realloc() Mr T Java 0 11-19-2003 10:22 AM
Realloc a struct ** Henrik J C++ 1 11-10-2003 08:54 AM
Problem with malloc, realloc, _msize and memcpy Bren C++ 8 09-03-2003 11:01 PM
Impossible Leak realloc Eitan Michaelson C++ 11 06-30-2003 10:33 AM



Advertisments