By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

g++ 3.2.2 realloc bug?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ron Natalie wrote:

"Jonathan.Bailleul" <ba******@greyc.ismra.fr> wrote in message news:3F***************@greyc.ismra.fr...
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
Jul 19 '05 #2

P: n/a

"Jonathan.Bailleul" <ba******@greyc.ismra.fr> wrote in message news:3F***************@greyc.ismra.fr...

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.
Jul 19 '05 #3

P: n/a
> Code within an assert will be removed if NDEBUG is not defined.

Sorry, if NDEBUG is defined.

john
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.