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 152305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:35:58+00:00 2026-05-11T09:35:58+00:00

In this question , someone suggested in a comment that I should not cast

  • 0

In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this:

int *sieve = malloc(sizeof(*sieve) * length); 

rather than:

int *sieve = (int *) malloc(sizeof(*sieve) * length); 

Why would this be the case?

  • 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-11T09:35:58+00:00Added an answer on May 11, 2026 at 9:35 am

    No; you shouldn’t cast the result, since:

    • It is unnecessary, as void * is automatically and safely promoted to any other pointer type in this case.
    • It adds clutter to the code, casts are not very easy to read (especially if the pointer type is long).
    • It makes you repeat yourself, which is generally bad.
    • It can hide an error if you forgot to include <stdlib.h>. This can cause crashes (or, worse, not cause a crash until way later in some totally different part of the code). Consider what happens if pointers and integers are differently sized; then you’re hiding a warning by casting and might lose bits of your returned address. Note: as of C99 implicit functions are gone from C, and this point is no longer relevant since there’s no automatic assumption that undeclared functions return int.

    As a clarification, note that I said “you shouldn’t cast”, not “you don’t need to cast”. In my opinion, it’s a blunder to include the cast, even if you got it right. There are simply no benefits to doing it, but a bunch of potential risks, and including the cast indicates that you don’t know about the risks.

    Also note, as commentators point out, that the above talks about straight C, not C++. I very firmly believe in C and C++ as separate languages.


    To add further, your code needlessly repeats the type information (int) which can cause errors. It’s better to de-reference the pointer being used to store the return value, to "lock" the two together:

    int *sieve = malloc(length * sizeof *sieve); 

    Some may say: “Well, previously the type was repeated, and now the variable name is repeated; isn’t this just as repetitive as before? How is that any better?” The difference is that if you one day change the type of the variable and forget to change the type under the sizeof to match, you will silently get an allocation of the wrong size and no warning about it; but if you change the name of the variable, but forget to change the name under sizeof to match, it is more probable that the old name no longer resolves to anything, so your code will stop compiling, prompting you to fix the mistake.

    This also moves the length to the front for increased visibility, and drops the redundant parentheses with sizeof; they are only needed when the argument is a type name. Many people seem to not know (or ignore) this, which makes their code more verbose. Remember: sizeof is not a function! 🙂

    While moving length to the front may increase visibility in some rare cases, one should also pay attention that in the general case, it should be better to write the expression as:

    int *sieve = malloc(sizeof *sieve * length); 

    Since keeping the sizeof first, in this case, ensures multiplication is done with at least size_t math.

    Compare: malloc(sizeof *sieve * length * width) vs. malloc(length * width * sizeof *sieve) the second may overflow the length * width when width and length are smaller types than size_t.

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

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • 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
  • Editorial Team
    Editorial Team added an answer If you can make them all implement an interface, that… May 11, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer That's bizarre. I always like to break a problem down… May 11, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer So, you have: Function A, which updates the hidden INPUT.… May 11, 2026 at 4:08 pm

Related Questions

I have just received and bypassed a problem with LightWindow and IE7 where, on
I'm working on an open source project. The original project contains comments in russian
Ok, so it's almost as easy as pie already. But it really should be
I have a page in my application that refreshes some content (a list of

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.