Re: Thank You -- Thomas J. Gritzan



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)

.



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... If your compiler ... it works with a cast. ... Pointer arithmetic, as you probably know, is scaled by ... not sure about concerning realloc(). ...
    (comp.lang.c)
  • Re: Thank You -- Thomas J. Gritzan
    ... pointer output of mallocand reallocto match my data structure. ... 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. ... When I don't match the realloc with the same cast ... ... If your compiler complains about the version without cast, ...
    (comp.lang.c)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
    (comp.lang.c)
  • Re: about the array
    ... 3- If you cast to the wrong type by accident, ... are malloc and free. ... the compiler should issue a diagnostic - gcc ... unknown set of arguments, and return an int. ...
    (comp.lang.c)
  • Re: Can generics really produce strongly typed collections?
    ... The compiler won't detect the bug at compile time. ... will try to cast to B, ... The "foreach" statement specifically behaves in the way you ... interface, assuming one exists. ...
    (microsoft.public.dotnet.languages.csharp)