I get this error when I'm trying to malloc some memory for my struct of ints.
当我试图对ints的结构进行malloc的一些内存时,我就会得到这个错误。
typedef struct _values {
random ints;
} values;
I have tried the lines below but my compiler doesn't like it. How do I fix the error?
我试过下面的行,但我的编译器不喜欢。我如何修正错误?
values v;
v = malloc(sizeof(values));
3 个解决方案
#1
4
You forgot to add the asterisk (*) after the values
and before the v
to mark it as a pointer: values *v;
您忘记在值和v之前添加星号(*),将其标记为指针:values *v;
The way you set it now, the v (without the asterisk) is defined as a stack variable and would be allocated on the stack and discarded once the function ends. It's type will be simply values
. malloc
is used to allocate memory on the heap and returns a pointer to the memory. Sine the function has no way of knowing the type it returns it as a void *
type - Which gives you your error - you're attempting to assign a void *
type into a struct
type, which the compiler can't do, nor can the compiler find a legitimate cast that could resolve the problem.
现在设置它的方式是,v(没有星号)被定义为一个堆栈变量,并在函数结束时被分配到堆栈上并丢弃。它的类型就是简单的值。malloc用于在堆上分配内存,并返回一个指向内存的指针。正弦函数没有办法知道它返回类型为void *类型——给你你的错误——你试图分配一个void *类型到一个结构体类型,编译器不能做,编译器也不能找一个合法的可以解决问题。
#2
3
You don't need to malloc
memory in this case; the line values v;
already allocated the memory ("on the stack"). It will automatically be freed when you leave the current scope.
在这种情况下,您不需要malloc内存;行值v;已经分配了内存(“在堆栈上”)。当您离开当前的范围时,它将自动释放。
If you really wanted to allocate the memory "on the heap", so that it persists past the current scope, you need v
to be a pointer; that is:
如果您真的想要将内存分配到“堆”上,那么它就会在当前范围内存在,您需要v作为指针;那就是:
values *v;
v = malloc(sizeof(values));
Remember to free
it when you're finished.
记住当你完成的时候把它释放出来。
#3
2
values v;
值v;
this is an instance of the structure that's allocated on the stack.
这是在堆栈上分配的结构的实例。
v = malloc(sizeof(values));
v = malloc(sizeof(值));
malloc returns a void*
pointer and compiler won't allow you to assign a pointer to an instance.
malloc返回一个void*指针,编译器不允许你分配一个指向实例的指针。
you need to declare a pointer and then assign the malloc return pointer to it.
您需要声明一个指针,然后分配malloc返回指针。
something of the sort
类似的意思
values *v = NULL;
值* v =零;
v = malloc(sizeof(values));
#1
4
You forgot to add the asterisk (*) after the values
and before the v
to mark it as a pointer: values *v;
您忘记在值和v之前添加星号(*),将其标记为指针:values *v;
The way you set it now, the v (without the asterisk) is defined as a stack variable and would be allocated on the stack and discarded once the function ends. It's type will be simply values
. malloc
is used to allocate memory on the heap and returns a pointer to the memory. Sine the function has no way of knowing the type it returns it as a void *
type - Which gives you your error - you're attempting to assign a void *
type into a struct
type, which the compiler can't do, nor can the compiler find a legitimate cast that could resolve the problem.
现在设置它的方式是,v(没有星号)被定义为一个堆栈变量,并在函数结束时被分配到堆栈上并丢弃。它的类型就是简单的值。malloc用于在堆上分配内存,并返回一个指向内存的指针。正弦函数没有办法知道它返回类型为void *类型——给你你的错误——你试图分配一个void *类型到一个结构体类型,编译器不能做,编译器也不能找一个合法的可以解决问题。
#2
3
You don't need to malloc
memory in this case; the line values v;
already allocated the memory ("on the stack"). It will automatically be freed when you leave the current scope.
在这种情况下,您不需要malloc内存;行值v;已经分配了内存(“在堆栈上”)。当您离开当前的范围时,它将自动释放。
If you really wanted to allocate the memory "on the heap", so that it persists past the current scope, you need v
to be a pointer; that is:
如果您真的想要将内存分配到“堆”上,那么它就会在当前范围内存在,您需要v作为指针;那就是:
values *v;
v = malloc(sizeof(values));
Remember to free
it when you're finished.
记住当你完成的时候把它释放出来。
#3
2
values v;
值v;
this is an instance of the structure that's allocated on the stack.
这是在堆栈上分配的结构的实例。
v = malloc(sizeof(values));
v = malloc(sizeof(值));
malloc returns a void*
pointer and compiler won't allow you to assign a pointer to an instance.
malloc返回一个void*指针,编译器不允许你分配一个指向实例的指针。
you need to declare a pointer and then assign the malloc return pointer to it.
您需要声明一个指针,然后分配malloc返回指针。
something of the sort
类似的意思
values *v = NULL;
值* v =零;
v = malloc(sizeof(values));