[英]Allocating a dynamic array inside a struct
I'm setting up a struct that defines a polynome, that is it contains two variables: 我正在建立一个定义一个多项式的结构,即它包含两个变量:
- int degree
that contains the degree of the polynome -包含多项式的度的int degree
- int * coeff = (int*) malloc (degree * sizeof(int))
that holds all the coefficients -包含所有系数的int * coeff = (int*) malloc (degree * sizeof(int))
Also I have defined a function new_polynome()
that takes in a degree and returns a pointer to a struct that holds a polynome of that degree with all its coefficients set to 1; 我还定义了一个函数new_polynome()
,它接受一个度数,并返回一个指向该结构的指针,该结构保存该度数的多项式并将其所有系数都设置为1;
#include <stdio.h>
#include <stdlib.h>
// 1
typedef struct
{
int degree;
int * coeff = (int *) malloc (degree * sizeof(int));
} polynome;
// 2
polynome * new_polynome(int n)
{
polynome * newest_polynome = (polynome *) malloc (sizeof(polynome));
for(int i = 0; i < n; i++)
newest_polynome->coeff[i] = 1;
return newest_polynome;
}
int main(void)
{
polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
new_polynome = new_polynome(5);
for(int i = 0; i < 5; i++)
printf("%d", new_polynome->coeff[i]);
free(new_polynome->coeff);
return 0;
}
However when I try and print its coefficients I get the following error. 但是,当我尝试打印其系数时,出现以下错误。 Is there a right way to do this? 有正确的方法吗? I don't understand the error message. 我不明白该错误信息。 How do I print its coefficients? 如何打印其系数? My error is as follows: 我的错误如下:
TD_polynomes.c:9:17: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token int * coeff = (int *) malloc (degree * sizeof(int)); ^ TD_polynomes.c: In function 'new_polynome': TD_polynomes.c:19:24: error: 'polynome {aka struct <anonymous>}' has no member named 'coeff' newest_polynome->coeff[i] = 1; ^~ TD_polynomes.c: In function 'main': TD_polynomes.c:27:20: error: called object 'new_polynome' is not a function or function pointer new_polynome = new_polynome(5); ^~~~~~~~~~~~ TD_polynomes.c:26:16: note: declared here polynome * new_polynome = (polynome *) malloc (sizeof(polynome)); ^~~~~~~~~~~~ TD_polynomes.c:30:34: error: 'polynome {aka struct <anonymous>}' has no member named 'coeff' printf("%d", new_polynome->coeff[i]); ^~ TD_polynomes.c:32:22: error: 'polynome {aka struct <anonymous>}' has no member named 'coeff' free(new_polynome->coeff); ^~
It is common that latter errors is just a consequence of earlier, and this is the case here. 通常,后面的错误只是更早的结果,在这里就是这种情况。 You have a faulty definition of polynome. 您对多义词的定义有误。 This has the consequence that it does not have a member called coeff. 结果是它没有名为coeff的成员。
You cannot initialize a struct the way you do. 您不能以这种方式初始化结构。 Remove the malloc call in the struct, and you'll see that a few other errors will magically disappear. 删除结构中的malloc调用,您将看到一些其他错误将神奇地消失。
And this line: new_polynome = new_polynome(5);
这行代码: new_polynome = new_polynome(5);
does not make sense. 没有道理。 It seems like you're trying to allocate space for 5 coeffs, but then you're completely wrong. 似乎您正在尝试为5个coeff分配空间,但是那完全是错误的。 You should do like this `new_polynome->coeff = malloc(5*sizeof(*(new_polynome->coeff)) 你应该这样做`new_polynome-> coeff = malloc(5 * sizeof(*(new_polynome-> coeff))
Also, don't cast malloc 另外, 不要强制转换malloc
There is some code you should move, like allocation of coeffs. 您应该移动一些代码,例如分配coeffs。 That should go inside the new_polynome function, which I renamed to create_polynome. 那应该放在new_polynome函数中,我将其重命名为create_polynome。
Working (and fixed) code: 工作(和固定)代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int degree;
int *coeff; // No init here. You're just defining a type here.
} polynome;
polynome *create_polynome(int n)
{
// A polynomial of degree 0 is a constant
const int N = n+1;
// You need to set the degree
new_polynome->degree = n;
polynome *new_polynome = malloc(sizeof(*new_polynome));
new_polynome->coeff = malloc(N * sizeof(*(new_polynome->coeff)));
for(int i = 0; i < N; i++)
new_polynome->coeff[i] = 1;
return new_polynome;
}
int main(void)
{
polynome *new_polynome = create_polynome(5);
// Looping to degree makes much more sense
for(int i = 0; i < new_polynome->degree+1; i++)
printf("%d", new_polynome->coeff[i]);
free(new_polynome->coeff);
// Not necessary to free in the end, but if you want to do it,
// do it properly and free everything. One malloc for each free and
// vice versa
free(new_polynome);
// No need to return 0 since that's the default in main
}
I also made some good (subjectively speaking) modifications like not having a space between the dereferencer * and the identifier. 我还做了一些很好的(主观地说)修改,例如在取消引用*和标识符之间没有空格。 Also removed space between functions and parethesis. 还消除了功能和感觉之间的空间。 Also, I used the size of the object instead of the type. 另外,我使用对象的大小而不是类型。
And as an extra bonus, a function that derivates: 作为一个额外的好处,该函数派生如下:
polynome *derivate_polynom(polynome *pol)
{
polynome * derivate = create_polynome(pol->degree - 1);
for(int i = 0; i < derivate->degree + 1; i++)
derivate->coeff[i] = (i+1) * pol->coeff[i+1];
return derivate;
}
1) int * coeff = (int *) malloc (degree * sizeof(int));
1) int * coeff = (int *) malloc (degree * sizeof(int));
is invalid syntax inside your struct definition: 结构定义中的语法无效:
typedef struct
{
int degree;
int * coeff = (int *) malloc (degree * sizeof(int));
} polynome;
Should just be: 应该只是:
typedef struct
{
int degree;
int * coeff;
} polynome;
2) This is wrong: 2)这是错误的:
polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
new_polynome = new_polynome(5);
You are assigning the result of malloc
to new_polynome
and then immediately overwriting it with the return value of new_polynome(5).
您正在将malloc
的结果分配给new_polynome
,然后立即用new_polynome(5).
的返回值覆盖它new_polynome(5).
This is a memory leak. 这是内存泄漏。
3) I think you might want to allocate space for N+1
integers instead of N
. 3)我想您可能想为N+1
整数而不是N
分配空间。 A zero-degree polynomial is a constant. 零级多项式是一个常数。
It is invalid syntax; 这是无效的语法; you can write 你可以写
typedef struct
{
int degree;
int coeff[];
} polynome;
polynome *new_polynome(int n)
{
polynome *p;
p = malloc(sizeof *p + n * sizeof p->coeff[0]);
for(int i = 0; i < n; i++)
p->coeff[i] = 1;
return p;
}
instead of. 代替。 Perhaps, check for overflows when n
is untrusted user input. 也许,当n
是不受信任的用户输入时,检查是否溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.