473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
http://www.greyc.ismra.fr/~bailleul
Jul 19 '05 #1
3 2710
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

"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
> Code within an assert will be removed if NDEBUG is not defined.

Sorry, if NDEBUG is defined.

john
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.