Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:14:51+00:00 2026-05-16T07:14:51+00:00

I have always been curious about this – why do in C++ I have

  • 0

I have always been curious about this – why do in C++ I have to cast return value from malloc but not in C?

Here is the example in C++ that works:

int *int_ptr = (int *)malloc(sizeof(int*));

And here is the example in C++ that doesn’t work (no cast):

int *int_ptr = malloc(sizeof(int*));

I heard that in C, in fact, casting an output from malloc() is a mistake.

Can anyone comment on this topic?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:14:52+00:00Added an answer on May 16, 2026 at 7:14 am

    Several points:

    C allows void pointers to be implicitly converted to any other object pointer type. C++ does not.

    Casting the result of malloc() in C will supress a useful diagnostic if you forget to include stdlib.h or otherwise don’t have a declaration for malloc() in scope. Remember that if C sees a function call without a prior declaration, it will assume that the function returns int. If you don’t have a declaration for malloc() and you leave off the cast, you’ll get a diagnostic to the effect that you’re trying to assign incompatible types (int to pointer). If you cast the result, you supress the diagnostic and will potentially have runtime issues, since it’s not guaranteed that converting a pointer value to an int and back to a pointer again will give you a useful result.

    If you’re writing C++, you should be using new and delete instead of malloc() and free(). Yeah, yeah, yeah, I’ve heard all the reasons why people want their code to compile as both C and C++, but the benefits of using the right memory management tool for the language outweigh the cost of maintaining two versions IMO.

    Note: the void * type was added in the C89 standard; earlier versions of C had malloc() return char *, so in those versions the cast was required if you were assigning the result to a different pointer type. Almost everybody supports at least the C89 standard though, so the odds of you running into one of those older implementations is very, very low.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've always been curious about audio conversion software, but I have never seen a
Here's something I've always been curious about, but never thought to ask. Is it
I come across this a lot, and have always been curious if there is
I'm a solo developer and always have been, but I've been learning lately about
There have been a few questions on SO about the pimpl idiom , but
I have always been taught to have one exit from functions and write code
I have always been challenged by the way lists work and am confused about
I have a question about Subversion. I have always been an end user so
I have been working with the DevExpress Filter TreeList code and am curious about
I have always been for documenting code, but when it comes to AJAX +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.