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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:49:18+00:00 2026-05-13T14:49:18+00:00

I asked a question earlier on defining a structure using malloc. This was the

  • 0

I asked a question earlier on defining a structure using malloc.
This was the answer I was given by the majority:

struct retValue* st = malloc(sizeof(*st));

I was showing a friend my code, and we came to a stumbling block.
Could someone please explain why this code works?
From my viewpoint, *st hasn’t been defined when you malloc it, so there could be any kind of garbage in there. It should be malloc(sizeof(struct retValue))

Thanks for any help

  • 1 1 Answer
  • 1 View
  • 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-13T14:49:18+00:00Added an answer on May 13, 2026 at 2:49 pm

    Sizeof looks at the type of the expression given to it, it does not evaluate the expression. Thus, you only need to make sure that the variables used in the expression are declared so that the compiler can deduce their type.

    In your example, st is already declared as pointer-to-struct-retValue. Consequently the compiler is able to deduce the type of the expression “*st”.

    Although it doesn’t look like it is already declared in your code, the compiler has already taken care of it for you. All declarations in your code are moved to the beginning of the block in which they occur by the compiler. Suppose you write

    One way to illustrate the knowledge that is available to the compiler is to look at the intermediate output it generates. Consider this example code…

    struct retValue {long int a, long int b};
    ...
    printf("Hello World!\n");
    struct retValue* st = malloc(sizeof(*st));
    

    Using gcc as an example and teh above code in the main() function of test.c, let’s look at the intermediate output by running…

    gcc -fdump-tree-cfg test.c
    

    The compiler will generate the file test.c.022t.cfg – Look at it and you’ll see

    [ ... removed internal stuff ...]
    ;; Function main (main)
    
    Merging blocks 2 and 3
    main (argc, argv)
    {
      struct retValue * st;
      int D.3097;
      void * D.3096;
    
      # BLOCK 2
      # PRED: ENTRY (fallthru)
      __builtin_puts (&"Hello World!"[0]);
      D.3096 = malloc (16);
      st = (struct retValue *) D.3096;
      D.3097 = 0;
      return D.3097;
      # SUCC: EXIT
    
    }
    

    Note how the declaration was moved to the beginning of the block and the argument to malloc is already replaced with the actual value denoting the size of the type the expression evaluated to. As pointed out in the comments, the fact that the declaration was moved to the top of the block is an implementation detail of the compiler. However, the fact that the compiler is able to do this and also to insert the correct size into the malloc all shows that the compiler was able to deduce the necessary information from the input.

    I personally prefer to give the actual type name as a parameter to sizeof, but that is probably a question of coding-style where I’d say that consistency trumps personal-preference.

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

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • 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 You can use the Capabilities class. import flash.system.Capabilities; switch (Capabilities.playerType)… May 14, 2026 at 6:10 am
  • Editorial Team
    Editorial Team added an answer We do something similar where customers login to a website… May 14, 2026 at 6:10 am
  • Editorial Team
    Editorial Team added an answer -(IBAction)clickedOpenMovie:(void)sender That's wrong. void means no type, and you cannot… May 14, 2026 at 6:10 am

Related Questions

I asked a question earlier which elicited some great responses. Here's the earlier question
I asked a question earlier about why left joins in Linq can't use defined
I asked a question earlier about which language to use for an AI prototype.
I asked a question earlier , and got an excellent response, but, being a
Earlier I asked a question about why I see so many examples use the

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.