g++ 3.2.2 realloc bug?

  • Thread starter Jonathan.Bailleul
  • Start date
J

Jonathan.Bailleul

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
 
J

Jonathan.Bailleul

Ron said:
Jonathan.Bailleul said:
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.
 
R

Ron Natalie

Ok, I interpreted man at the letter without thinking. That was that...
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.
 
J

John Harrison

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

Sorry, if NDEBUG is defined.

john
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top