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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:19:12+00:00 2026-05-29T10:19:12+00:00

In the following code, will freeing Dairy free Yogurt as well? Both point to

  • 0

In the following code, will freeing Dairy free Yogurt as well?
Both point to the same address as far as I know.

Also, is this style of coding a bad practice? Say if I only kept pointers to the Dairy and indirectly freed the Yogurt and Cheese as well?

#include <stdlib.h>

typedef struct {
    int calcium;
    int protein;
} Dairy;

typedef struct {
    Dairy dairy;
    int sugar;
    int color;
} Yogurt;

int main () {
    Yogurt* yogurt = malloc(sizeof(Yogurt));

    Dairy* dairy = &yogurt->dairy;

    free(dairy); // Will this free yogurt?
}
  • 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-29T10:19:13+00:00Added an answer on May 29, 2026 at 10:19 am

    Yes the behavior is well-defined.

    As per the C Standard You need to pass the exact same address to deallocation function free as was returned by an malloc or other allocation functions.

    Reference:

    7.22.3.3 The free function

    Synopsis:

    §1 #include <stdlib.h> void free(void *ptr);

    Description:

    §2 The free function causes the space pointed to by ptr to be deallocated, that is, made
    available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by a memory management
    function, or if the space has been deallocated by a call to free or realloc, the
    behavior is undefined
    .

    So, In this case the behavior is guaranteed to be well defined if and only if pointer to the structure is same as pointer to it’s first member.
    And that is guaranteed by the standard in:

    6.7.2.1 Structure and union specifiers
    §15

    Within a structure object, the non-bit-field members and the units in which bit-fields
    reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed
    padding within a structure object, but not at its beginning.

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

Sidebar

Related Questions

I know the following code will move an object in a straight line. How
I know that the following code will delete the record with id = 5
The following code will search for the user within the domain controller, but I
The following code will add the categories selector widget to the WordPress Page editor
The following code will log in my application to a server. That server will
Does the following code will increase the allocated memory continuously as it is called
Why the following code will return obvious , surprise! (and lastly how come? ).
level: beginner the following code will print 'False' def function(x): if len(x) == 5:
I have following code in my Application. Comments in my code will specify My
Hi can someone explain if the in the following code the synchronized code will

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.