Jump to content
  • Advertisement
Sign in to follow this  
Maquiavel

C code with malloc doesn't compile!

This topic is 5175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, in the following code, the compiler returns the message: "invalid conversion from `void*' to `int*' " and the code doesn't compile!
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int *p;
  p = malloc(40 * sizeof(int));

...

So, I have tried this: p = (int)malloc(40 * sizeof(int)); and it doesn't compile too! What's wrong?

Share this post


Link to post
Share on other sites
Advertisement
o_O

My understanding is that you're not supposed to need to cast returns from malloc() any more, and that in fact you shouldn't for "good style" any more (because it will "hide the error of not including stdlib.h"; I don't know the details there though).

How old is your compiler?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
o_O

My understanding is that you're not supposed to need to cast returns from malloc() any more, and that in fact you shouldn't for "good style" any more (because it will "hide the error of not including stdlib.h"; I don't know the details there though).

How old is your compiler?


Zahlman, I'm using the new version of DevC++ (4.9.9.0) that comes with newer mingw.

Share this post


Link to post
Share on other sites
Aha. fallenang3l is right then. Use an actual C compiler for C code if you can; try not to be messing around with malloc() and free() in a C++ environment. Bad mojo.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!