Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!

Tech-Archive recommends: Speed Up your PC by fixing your registry



"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:213AA21B-76AF-4503-B80F-D0C9307F185B@xxxxxxxxxxxxx
Okay, I have entered the code in VC++ and an error points to the same
line of code:

*obj_PASSCODE = malloc (sizeof (PASSCODE));

The error is:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\TEST_4\Test_1.cpp(70): error
C2440: 'type cast' : cannot convert from 'void *' to 'PASSCODE'

malloc() returns void*. Unlike C, there's no implicit conversion in C++
from void* to other pointer types. If you want this code to be valid C++
as well as C, make it

*obj_PASSCODE = (PASSCODE*)malloc(sizeof (PASSCODE));

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Inconsistent Program Results
    ... void rdinput; ... you need to test that restrict is not NULL before ... Don't cast the result of malloc(). ...
    (comp.lang.c)
  • Re: OT: Memory profiling on Linux
    ... I need to profile my milter daemon on linux. ... Then you can go through the allocation log, and any allocations that don't ... #define MALLOC malloc ... void *mymalloc ...
    (comp.lang.c)
  • Re: Cipher Lab / Syntech
    ... this is not Google Groups. ... The "simple" library without malloc is needed because I was told ... void foo { ... int dprintf { ...
    (comp.lang.c)
  • Re: (part 10) More Schildt-like errors in Dicky Heathens book
    ... void* q = realloc; ... So now this handles realloc(p, ... void* malloc(size_t s) ... a NULL result from malloc always indicates allocation failure, ...
    (comp.lang.c)
  • Re: (FAQ details:) malloc(), void * and casts
    ... return value from malloc(). ... cast is not needed and 2) the cast may mask errors. ... fact that the the pointer returned from mallocis a void *, ... a pointer to any other type. ...
    (comp.lang.c)