Up vote 0 down vote favorite share g+ share fb share tw.
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 terms20; } 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! C homework struct dynamic-memory-allocation array-initialization link|improve this question asked Apr 5 at 22:16Hall931.
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.
3 That qualifies as doing his homework for him. Wouldn't it be better not to solve it but to lead him to a solution without doing it for him? – Warren P Apr 5 at 22:20 When and where should the *terms calloc be called?
– Hall9 Apr 5 at 22:21 The main thing is that the polynomials you are using point to valid pointers, otherwise you can't alloc their members. – Jack Apr 5 at 22:22 @WarrenP: the nature of the question is not to find an algorithm or a solution but to understand how this works. And you explain it with examples as in every C manual you can find around.
I think there is big difference between asking "how can I solve this problem" and "how this part of the language works". This kind of question falls in the second partition IMHO. He asked how to allocate a pointer inside a struct, not an implementation of quicksort.
– Jack Apr 5 at 22:23.
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.
Since your question is tagged homework, I won't tell you exactly. TERM terms20 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 xn 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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.