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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:11:48+00:00 2026-06-04T07:11:48+00:00

While mentioning the size of a structure to a malloc call, is it better

  • 0

While mentioning the size of a structure to a malloc call, is it better to mention the name of the structure or the dereferenced structure pointer variable?

Example:

struct ExampleStruct
{
  int x;
  char arr[40];
};

int main()
{

  struct ExampleStruct *Y = NULL;

  Y = malloc(sizeof(struct ExampleStruct)); //Is this better?

  Y = malloc(sizeof(*Y)); //Is this better?  

}

I personally prefer sizeof(struct ExampleStruct) as I have seen many developers making the mistake missing the ‘*’ in the second method, i.e. they erroneously type it as malloc(sizeof(Y)) and in this case, the memory allocated will be only 4 bytes. But, I have seen it being used commonly also.

  • 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-06-04T07:11:49+00:00Added an answer on June 4, 2026 at 7:11 am

    I looked in K&R 2nd Edition, and found 3 relevant examples of using malloc():

    • Page 142: return (struct tnode *) malloc(sizeof(struct tnode));

    • Page 145: np = (struct nlist *) malloc(sizeof(*np));

    • Page 146: return (Treeptr) malloc(sizeof(Treenode));

    There was no particular discussion of the different forms. There’s a note in the preface ‘We used Bjarne Stroustrup’s C++ translator extensively for local testing of our programs’. This was written just before the 1989 C Standard was finalized (my copy has ‘Based on the Draft Proposed ANSI C’ on the cover, so there were no standard compilers at the time), and this may explain the explicit casts on every malloc() call — those are necessary in C++.

    So, the ‘founders’ used both forms.

    Modern style is to use the sizeof(*variable) notation (and, in C, to omit the cast) so that even if the type of the variable changes, you won’t have to change this code. Add the cast, and that benefit goes away.

    Most of my older code tends to use the sizeof(type) notation, in part in emulation of the style in K&R C. Most of my newer code now uses the sizeof(*variable) notation. When I expect to compile the code with C++ as well as C, I put the cast in too.

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

Sidebar

Related Questions

As far as I know about data types in C/C++, while declaring a variable,
While surfing for a solution to call Matlab from Java, I came to know
While reading the answers to this SO question , I learned that out-of-bounds pointer
I spent a while looking through Facebook's polices but couldn't find anything mentioning this
I'll preface this question by mentioning that while I'm far from a regular expressions
While debugging and keep pressing F5, if the source code does not exist, eclipse
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While deveoping a site (using Forms authentication and InProc sessionstate) a frequently run into
While developing an application using gwt in ecliplse crashed. Now the server is running
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I

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.