casting malloc question. 
Author Message
 casting malloc question.

I'm using VC++ 5.0 (Win32 console application).  I created a
workspace and project from scratch with the source:

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
     char *element;

     if ((element = malloc (40000)) == NULL)
     {
          printf("Failure.\n");
          exit(1);
         }
     else printf("Success\n");

     return 0;

Quote:
}

I was surprised to get the error message:

C:\My Documents\Temporary\temp\temp.cpp(9) :
error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

temp.exe - 1 error(s), 0 warning(s)

My compiler requires the inclusion of both malloc.h and stdlib.h
for malloc to be in scope.  The problem (or rather, the error
message) disappears when I cast (char *).  But I'm still curious,
and I'd like to know whether my code is incorrect somehow.

Thanks (and, incidentally, may the force be with you),
V



Mon, 24 Sep 2001 03:00:00 GMT  
 casting malloc question.
: I'm using VC++ 5.0 (Win32 console application).  I created a
: workspace and project from scratch with the source:

: #include <malloc.h>
: #include <stdlib.h>
: #include <stdio.h>

: int main(void)
: {
:      char *element;

:      if ((element = malloc (40000)) == NULL)
:      {
:           printf("Failure.\n");
:           exit(1);
:          }
:      else printf("Success\n");

:      return 0;
: }

: I was surprised to get the error message:

: C:\My Documents\Temporary\temp\temp.cpp(9) :
: error C2440: '=' : cannot convert from 'void *' to 'char *'
: Conversion from 'void*' to pointer to non-'void' requires an explicit cast
: Error executing cl.exe.

: temp.exe - 1 error(s), 0 warning(s)

: My compiler requires the inclusion of both malloc.h and stdlib.h
: for malloc to be in scope.  The problem (or rather, the error
: message) disappears when I cast (char *).  But I'm still curious,
: and I'd like to know whether my code is incorrect somehow.

No, it's correct C, and incorrect C++.  The compiler probably thinks
it's compiling the latter.  And you shouldn't need the <malloc.h>
header for ANSI C, so something must be off somewhere.

Will

Will



Tue, 25 Sep 2001 03:00:00 GMT  
 casting malloc question.
Quote:

> I'm using VC++ 5.0 (Win32 console application).  I created a
> workspace and project from scratch with the source:

> #include <malloc.h>

  <malloc.h> is not a standard header.
[...]

Quote:
> I was surprised to get the error message:

> C:\My Documents\Temporary\temp\temp.cpp(9) :
> error C2440: '=' : cannot convert from 'void *' to 'char *'
> Conversion from 'void*' to pointer to non-'void' requires an explicit cast

Then you are almost certainly using your compiler as a C++ compiler, not
a C compiler.  

Quote:
> Error executing cl.exe.

> temp.exe - 1 error(s), 0 warning(s)

> My compiler requires the inclusion of both malloc.h and stdlib.h
> for malloc to be in scope.  

I doubt very much that your compiler requires the inclusion of a
nonstandard header.  This claim implies that somehow the prototype for
malloc is splintered somehow between the two headers.  This is a curious
construction that I would like to see.

--




Tue, 25 Sep 2001 03:00:00 GMT  
 casting malloc question.

Quote:

>I'm using VC++ 5.0 (Win32 console application).  I created a
>workspace and project from scratch with the source:

>#include <malloc.h>
>#include <stdlib.h>
>#include <stdio.h>

>int main(void)
>{
>     char *element;

>     if ((element = malloc (40000)) == NULL)
>     {
>          printf("Failure.\n");
>          exit(1);

For that to be portable you should use exit(EXIT_FAILURE).

Quote:
>         }
>     else printf("Success\n");

>     return 0;
>}

>I was surprised to get the error message:

>C:\My Documents\Temporary\temp\temp.cpp(9) :

By using the .cpp extension you are telling your compiler to compile
the code as C++ not C. Use a .c extension for C code.

Quote:
>error C2440: '=' : cannot convert from 'void *' to 'char *'
>Conversion from 'void*' to pointer to non-'void' requires an explicit cast
>Error executing cl.exe.

>temp.exe - 1 error(s), 0 warning(s)

>My compiler requires the inclusion of both malloc.h and stdlib.h
>for malloc to be in scope.

I just tried compiling you code (in a source file called malloc.c) using
MSVC+ 5.0 both with and without the <mallo.h> line. It compiled cleanly
both times. Since <malloc.h> isn't a standard header and is unnecessary you
should omit it.

Quote:
> The problem (or rather, the error
>message) disappears when I cast (char *).  But I'm still curious,
>and I'd like to know whether my code is incorrect somehow.

It is incorrect C++ but perfectly valid C.

--
-----------------------------------------


-----------------------------------------



Wed, 26 Sep 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. casting malloc question.

2. malloc: to cast or not to cast

3. C question : redefining a new malloc() calling standard malloc()

4. to malloc() or not to malloc(), that is the question

5. never cast malloc?

6. casting return value from malloc

7. type casting malloc() to void *

8. cast to malloc

9. Why not cast the rerurn from malloc()?

10. More about casting malloc()...

11. About casting malloc()...

12. Type casting malloc??

 

 
Powered by phpBB® Forum Software