C++ 使用 malloc() 和 sizeof() 在堆上创建结构体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4519834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using malloc() and sizeof() to create a struct on the heap
提问by Ordo
I'm trying to use malloc() and sizeof() to create a struct on the heap. Here is my code:
我正在尝试使用 malloc() 和 sizeof() 在堆上创建一个结构。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee
{
char first[21];
char last[21];
char title[21];
int salary;
};
struct Employee* createEmployee(char* first, char* last, char* title, int salary) // Creates a struct Employee object on the heap.
{
struct Employee* p = malloc(sizeof(struct Employee));
if (p != NULL)
{
strcpy(p->first, first);
strcpy(p->last, last);
strcpy(p->title, title);
p->salary, salary;
}
return p;
}
No my compiler (Visual C++) tells me for the line struct Employee* p = malloc(sizeof(struct Employee));
that the type "void *" can't be converted to the type "Employee *". I don't know what is wrong here. Seems as if struct Employee is a void but i don't understand why...
没有,我的编译器(Visual C++)告诉我struct Employee* p = malloc(sizeof(struct Employee));
“void *”类型不能转换为“Employee *”类型。我不知道这里出了什么问题。好像 struct Employee 是空的,但我不明白为什么......
回答by user470379
In C++ (since you are using Visual C++ to compile), you have to explicitly cast the pointer returned by malloc
:
在 C++ 中(因为您使用 Visual C++ 进行编译),您必须显式转换由 返回的指针malloc
:
struct Employee* p = (struct Employee*) malloc(sizeof(struct Employee));
回答by R.. GitHub STOP HELPING ICE
Best practices for using malloc
:
使用的最佳实践malloc
:
struct Employee *p = malloc(sizeof *p);
You also need to fix your IDE/compiler to tell it you're writing C and not C++, since it's too broken to figure this out on its own...
您还需要修复您的 IDE/编译器以告诉它您正在编写 C 而不是 C++,因为它太坏了,无法自己解决这个问题...
Since some people seem unhappy with this answer (and with my disapproval of the other answers), I think I should explain why working around the problem is not good.
由于有些人似乎对这个答案不满意(并且我不赞成其他答案),我想我应该解释为什么解决这个问题不好。
In C, casting the return value of malloc is harmful because it hides warnings if you forgot to include stdlib.h or otherwise prototype malloc. It also makes your code harder to maintain; all the answers with a cast require making 3 changes if the type of p needs to be changed, while my answer requires a change only in one place. Finally, new C programmers should not get in the bad habit of using casts whenever they see a compiler warning or error. This usually just buries bugs. Correct code almost never requires casts, and their use should be seen as a code smell
在 C 中,转换 malloc 的返回值是有害的,因为如果您忘记包含 stdlib.h 或其他原型 malloc,它会隐藏警告。它还使您的代码更难维护;如果需要更改 p 的类型,则所有带有演员表的答案都需要进行 3 次更改,而我的答案只需要在一处更改。最后,新的 C 程序员不应该养成在看到编译器警告或错误时使用强制转换的坏习惯。这通常只是埋葬错误。正确的代码几乎不需要强制转换,它们的使用应该被视为代码异味
回答by aschepler
If you are compiling as C code, that line should be valid.
如果您编译为 C 代码,该行应该是有效的。
But in C++, the conversion from void*
to Employee*
is invalid.
但是在 C++ 中,从void*
to的转换Employee*
是无效的。
Is the file named something.c? Are there compiler options you can change?
文件名为something.c 吗?是否有您可以更改的编译器选项?
If you must use a C++ compiler, you can fix this by adding an explicit cast:
如果必须使用 C++ 编译器,可以通过添加显式强制转换来解决此问题:
struct Employee* p = (struct Employee*)malloc(sizeof(struct Employee));
回答by Chad La Guardia
You should cast the result of malloc to the type of pointer you are using.
您应该将 malloc 的结果转换为您正在使用的指针类型。
struct Employee* p = (struct Employee*)malloc(sizeof(struct Employee));
malloc
will always return a chunck of memory as a (void *)
. Its up to you to tell the compiler what type of chunck that memory is.
malloc
将始终返回一大块内存作为(void *)
. 由您来告诉编译器该内存是什么类型的块。
回答by Jon
The value returned from malloc
is a void*
. The value you want to assign to p
must be of type Employee*
. In C++ (in contrast to C), there is no implicit conversion from void*
to another pointer type. Therefore, you need to perform the conversion explicitly:
从返回的值malloc
是 a void*
。要分配给的值p
必须是 类型Employee*
。在 C++ 中(与 C 相比),没有从void*
到另一个指针类型的隐式转换。因此,您需要显式执行转换:
struct Employee* p = reinterpret_cast<Employee*>(malloc(sizeof(struct Employee)));