简体   繁体   English

结构内数组的动态内存分配,

[英]Dynamic-memory-allocation of an array within a struct,

I don't understand how to Dynamic allocate memory for an array of structs within another struct.我不明白如何为另一个结构中的结构数组动态分配 memory。 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..正如,这是我的问题...我有一个包含主要内容的 project.c 文件,我有另一个 polynomial.c 文件,它处理所有多项式操作,例如添加项、将多项式乘以数字等。

This is header file for polynomials.h这是 polynomials.h 的 header 文件

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.我在我的 project.c 文件中也有这个,它为 poly 数组动态分配 memory。

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?我在这里有两个问题,我应该何时以及如何为 terms 数组动态分配 memory? I was thinking maybe to do a pointer to a pointer who holds the calloc memory for an empty array of terms.我在想也许可以做一个指向一个指针的指针,该指针持有一个空的术语数组的 calloc memory。 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?另一个问题,现在当我 go 释放 memory 时,应该在程序结束前在它退出之前完成吗?我释放的顺序应该是自下而上,对吧? In other words, free the terms array then the polynomial array.换句话说,先释放项数组,然后释放多项式数组。

At this point any hints or guidance would be helpful.在这一点上,任何提示或指导都会有所帮助。 Thanks!谢谢!

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 .是的,您必须自下而上释放 memory,首先释放所有项,然后释放POLYNOMIALS数组。

For a start your polynomial struct should look like:首先,您的polynomial结构应该如下所示:

typedef struct polynomial {
   int size;
   TERM *terms;
} POLYNOMIAL;

Then for each polynomial struct you have:然后对于每个polynomial结构你有:

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.在释放polynomial结构之前,您将需要释放terms指向的 memory,否则将不允许您访问terms成员。

Since your question is tagged homework, I won't tell you exactly.由于您的问题已标记为家庭作业,因此我不会确切地告诉您。

TERM terms[20] is a literal in-place array. TERM terms[20]是一个文字就地数组。 If you declared a variable like that in a function it would reserve exactly space on the stack for that number of array elements.如果您在 function 中声明了一个变量,它将在堆栈上为该数量的数组元素保留准确的空间。 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.因此,您被要求将某些内容从X x[n]更改为等效的指针语法,该语法也用于数组语法。

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.您已经编写了POLYNOMIAL * polynomials ,因此您知道这既是 (a) 指向单个多项式的指针,或 (b) 指向多项式数组的指针,并且您可以使用malloc表达式对其进行初始化。

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.您可以重写字段term ,使其指向一个或多个TERM结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM