Reputation: 43
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!
Upvotes: 1
Views: 5704
Reputation: 69102
Since your question is tagged homework, I won't tell you exactly.
TERM terms[20]
is a literal in-place array. If you declared a variable like that in a function it would reserve exactly space on the stack for that number of array elements. If you did it inside a structure it would leave space inside the structure itself. So you've been asked to change something from X x[n]
to the equivalent pointer syntax, which is also used for array syntax.
You already have written POLYNOMIAL * polynomials
so you know that this is both (a) a pointer to a single polynomial, or (b) a pointer to an array of polynomials, and that you can initialize it using a malloc
expression.
If you use what you already know from the question, surely you can see what you are being asked to intuit for yourself; That you can rewrite the field term
in such a way that it could point to one, or multiple TERM
structs.
Upvotes: 0
Reputation: 18522
For a start your polynomial
struct should look like:
typedef struct polynomial {
int size;
TERM *terms;
} POLYNOMIAL;
Then for each polynomial
struct you have:
p.terms = calloc(size, sizeof(*terms));
You will need to free the memory pointed to by terms
before you free the polynomial
structs, since you wouldn't be allowed to access the terms
member otherwise.
Upvotes: 1
Reputation: 133639
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
.
Upvotes: 2