Re: Thank You -- Thomas J. Gritzan
- From: Tom <Thomas-911@xxxxxxxxxxxxx>
- Date: Tue, 12 Sep 2006 20:40:52 GMT
On Tue, 12 Sep 2006 08:54:16 +0000, Richard Heathfield
<invalid@xxxxxxxxxxxxxxx> wrote:
Tom said:
<snip>
Now I am using malloc(), realloc() and free(). I am casting the
pointer output of malloc() and realloc() to match my data structure.
Why?
(The need for this casting is poorly described in the realloc() doco
in my opinion.)
There is no such need. The cast is unnecessary and obfuscatory, and can
conceivably conceal the bug of failing to provide a prototype for *alloc,
which happens from time to time.
<snip>
My compiler chokes without using the cast for malloc() or calloc().
When I don't match the realloc with the same cast ... the program
again fails to compile. Thus I am confused by your claim it is
unnecessary. Can you provide an short example without the cast that I
can test on my compiler?
The following is a MS example that FAILS to compile:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main( void )
{
long *buffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );
/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );
free( buffer );
exit( 0 );
}
=============================
Here's the compiler error:
Compiling...
main.cpp
c:\stock programs\temp project tbd\main.cpp(40) : error C2440: '=' :
cannot convert from 'void *' to 'long *'
Conversion from 'void*' to pointer to non-'void' requires an
explicit cast
Error executing cl.exe.
temp project tbd.exe - 1 error(s), 0 warning(s)
=============================
Someone did not even take the effort to compile their own example!!
Add the cast and it works. >>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main( void )
{
long *buf;
size_t size;
if( (buf= (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buf);
printf( "Size of block after malloc of 1000 longs: %u\n", size );
/* Fix Compile Problem By Adding Cast */
if((buf=(long *)realloc(buf,size+(1000*sizeof(long)))) == NULL )
exit( 1 );
size = _msize( buf);
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );
free( buf);
exit( 0 );
}
==================================
Deleting intermediate files and output files for project 'temp project
tbd - Win32 Debug'.
---------Configuration: temp project tbd - Win32 Debug---------
Compiling...
main.cpp
Linking...
temp project tbd.exe - 0 error(s), 0 warning(s)
.
- Follow-Ups:
- Re: Thank You -- Thomas J. Gritzan
- From: Martin Ambuhl
- Re: Thank You -- Thomas J. Gritzan
- From: Michael Mair
- Re: Thank You -- Thomas J. Gritzan
- From: CBFalconer
- Re: Thank You -- Thomas J. Gritzan
- From: Keith Thompson
- Re: Thank You -- Thomas J. Gritzan
- From: Christopher Benson-Manica
- Re: Thank You -- Thomas J. Gritzan
- References:
- Thank You -- Thomas J. Gritzan
- From: Tom
- Re: Thank You -- Thomas J. Gritzan
- From: Richard Heathfield
- Thank You -- Thomas J. Gritzan
- Prev by Date: Re: How many bits are '1' in a integer variable?
- Next by Date: Re: How many bits are '1' in a integer variable?
- Previous by thread: Re: Thank You -- Thomas J. Gritzan
- Next by thread: Re: Thank You -- Thomas J. Gritzan
- Index(es):
Relevant Pages
|