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

  • Home
  • SEARCH
  • 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 42715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:22:53+00:00 2026-05-10T15:22:53+00:00

I got a comment to my answer on this thread: Malloc inside a function

  • 0

I got a comment to my answer on this thread:

Malloc inside a function call appears to be getting freed on return?

In short I had code like this:

int * somefunc (void) {   int * temp = (int*) malloc (sizeof (int));   temp[0] = 0;   return temp; } 

I got this comment:

Can I just say, please don’t cast the return value of malloc? It is not required and can hide errors.

I agree that the cast is not required in C. It is mandatory in C++, so I usually add them just in case I have to port the code in C++ one day.

However, I wonder how casts like this can hide errors. Any ideas?

Edit:

Seems like there are very good and valid arguments on both sides. Thanks for posting, folks.

  • 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. 2026-05-10T15:22:54+00:00Added an answer on May 10, 2026 at 3:22 pm

    It seems fitting I post an answer, since I left the comment 😛

    Basically, if you forget to include stdlib.h the compiler will assume malloc returns an int. Without casting, you will get a warning. With casting you won’t.

    So by casting you get nothing, and run the risk of suppressing legitimate warnings.

    Much is written about this, a quick google search will turn up more detailed explanations.

    edit

    It has been argued that

    TYPE * p; p = (TYPE *)malloc(n*sizeof(TYPE)); 

    makes it obvious when you accidentally don’t allocate enough memory because say, you thought p was TYPe not TYPE, and thus we should cast malloc because the advantage of this method overrides the smaller cost of accidentally suppressing compiler warnings.

    I would like to point out 2 things:

    1. you should write p = malloc(sizeof(*p)*n); to always ensure you malloc the right amount of space
    2. with the above approach, you need to make changes in 3 places if you ever change the type of p: once in the declaration, once in the malloc, and once in the cast.

    In short, I still personally believe there is no need for casting the return value of malloc and it is certainly not best practice.

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Generically, a covariant type parameter is one which is allowed… May 11, 2026 at 11:41 am
  • added an answer If this is a fixed list (which it seems it… May 11, 2026 at 11:41 am
  • added an answer How to use pdb with mod_wsgi is documented on the… May 11, 2026 at 11:41 am

Related Questions

I got a comment to my answer on this thread: Malloc inside a function
I've got a CAkePHP 1.2 site. I've got three related Models/tables: A Comment has
I got a webserver with a running application. There's a webpage with a form:
I got a little curious after reading this /. article over hijacking HTTPS cookies.
I got a call from a tester about a machine that was failing our
I got a program that writes some data to a file using a method
I got a Function that returns a Collection<string> , and that calls itself recursively
I got a simple page with a HtmlInputHidden field. I use a javascript to
I got a bunch of servlet context listeners in my Java webapp, each of
I got a text file with a couple of lines and I am looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.