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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:08:43+00:00 2026-06-01T14:08:43+00:00

I don’t understand how to Dynamic allocate memory for an array of structs within

  • 0

I don’t understand how to Dynamic allocate memory for an array of structs within another struct. As in, here is my problem… I have a project.c file that contains the main, I have another polynomial.c file that handles all the poly operations like adding terms, multiplying a polynomial by a number ect..

This is header file for polynomials.h

typedef struct term{
    int coeff;
    int expo;
} TERM;

typedef struct polynomial {

int size;
// This needs to be changed to calloc.. not sure how within a struct
TERM terms[20];

} POLYNOMIAL;
...
...

I Also have this within my project.c file which Dynamically allocates memory for the poly array.

POLYNOMIAL *polynomials = (POLYNOMIAL *)malloc(sizeof(POLYNOMIAL) * 8);
// 8 being the max number of polynomials I wan to store

I have two questions here, when and how should I dynamic allocate the memory for the terms array? I was thinking maybe to do a pointer to a pointer who holds the calloc memory for an empty array of terms. This would be done at the program start, but after the polynomial allocation (I think).

Another Question, Now when I go to free the memory should this be done at the end of the program before it exits and the order in which i free should be bottom up, right? In other words, free the terms array then the polynomial array.

At this point any hints or guidance would be helpful. Thanks!

  • 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-01T14:08:44+00:00Added an answer on June 1, 2026 at 2:08 pm

    You can simply allocate it with

    TERM *terms = calloc(20, sizeof(TERM));
    

    You can’t do it directly within the struct declaration so what you are going to do is something like

    POLYNOMIAL *polynomials = calloc(size, sizeof(POLYNOMIAL));
    
    for (int i = 0; i < size; ++i)
      polynomials[i].terms = calloc(20, sizeof(TERM));
    

    And yes, you will have to free memory bottom up, first you free all the terms, then you free the array of POLYNOMIALS.

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

Sidebar

Related Questions

Don't be scared of the extensive code. The problem is general. I just provided
Don't know if this has been asked before, so point me to another question
Don't think that I'm mad, I understand how php works! That being said. I
Don't they both have to convert to machine code at some point to execute
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't overthink this - there's a very commonly used term and I ... have
Don't know what's wrong here, when I run the application it says Specified method
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of
Don't ask why but I have the requirement to draw a border around certain

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.