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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:03:58+00:00 2026-05-31T19:03:58+00:00

I am dynamically allocating a struct which has a different struct as a member:

  • 0

I am dynamically allocating a struct which has a different struct as a member:

struct a {
   // other members
   struct b;
}

struct b basically holds a pointer to another struct b, so think of struct b as a linked list.

If I dynamically allocate struct a, then that would also make a new struct b within it. However, what is the difference between doing that or having struct a hold a pointer to struct b, and dynamically allocate struct b within struct a? What is the difference in implementation?

  • 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-31T19:03:59+00:00Added an answer on May 31, 2026 at 7:03 pm

    If you dynamically allocate (malloc) struct a as in

    struct a *temp = (struct a *)malloc(sizeof(struct a));
    

    you malloc space for a pointer to struct b (assuming that’s what is in struct a) but you don’t malloc space for struct b. That means later you’ll have to do

    temp->b = (struct b *)malloc(sizeof(struct b));
    

    before you try and use struct b.

    If you don’t store a pointer to struct b but rather struct b directly then you’ll get the automatic allocation when you define struct a.

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

Sidebar

Related Questions

How would I go about dynamically allocating a multi-dimensional array?
I don't truly understand some basic things in C like dynamically allocating array of
I was following this: Dynamically allocating memory inside __device/global__ CUDA kernel But it still
My program (a text-mode web browser) is dynamically allocating memory. I do free unneeded
I'm having some issues in allocating memory for an array dynamically in C++ within
I am allocating memory to a object dynamically and then if I call delete
I'm a little confused about dynamically allocating a 3d array. Right now, I'm just
I want to use std::vector for dynamically allocating memory. The scenario is: int neededLength
Can we place dynamically allocated objects at some other place than heap?? How will
I am writing an application in c in which I'm allocating the memory needed

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.