简体   繁体   English

为什么我们需要转换malloc返回的内容?

[英]Why do we need to cast what malloc returns?

    int length = strlen(src);
    char *structSpace = malloc(sizeof(String) + length + 1);
    String *string = (String*) structSpace;    
    int *string = (int*) structSpace;

*I created a struct called String *我创建了一个名为String的结构

You don't. 你没有。 void* will implicitly cast to whatever you need in C. See also the C FAQ on why you would want to explicitly avoid casting malloc's return in C. @Sinan's answer further illustrates why this has been followed inconsistently. void*将隐式地转换为C中您需要的任何内容。另请参阅C常见问题解答 ,了解为什么您希望明确避免在C中转换malloc的返回。@ Sinan的答案进一步说明了为什么不一致地遵循它。

Because malloc returns a pointer to void, ie, it is simply allocating chunks of memory with no regard as to the data that will be stored there. 因为malloc返回一个指向void的指针,即它只是分配内存块而不考虑将存储在那里的数据。 In C++ your returned void* will not be implicitly cast to the pointer of your type. 在C ++中,您返回的void *不会隐式地转换为您的类型的指针。 In your example, you have not cast what malloc has returned. 在您的示例中,您还没有转换malloc返回的内容。 Malloc returned a void* which was implicitly cast to a char*, but on the next line you... ok, it doesn't make much sense anymore. Malloc返回了一个void *,它被隐式地转换为char *,但是在下一行你......好吧,它再也没有多大意义了。

This is one of the few issues that makes the statement "C++ is a superset of C" not completely true. 这是使“C ++是C的超集”这一陈述的少数问题之一并非完全正确。 In C, a void pointer can be implicitly cast to any other type of pointer. 在C中, void指针可以隐式地转换为任何其他类型的指针。 However, C++ is a bit more strict with type safety, so you need to explicitly cast the return value of malloc to the appropriate type. 但是,C ++对类型安全性要严格一些,因此需要将malloc的返回值显式转换为适当的类型。 Usually, this isn't much of an issue, because C++ code tends to use new rather than malloc , which doesn't require typecasting. 通常,这不是什么大问题,因为C ++代码倾向于使用new而不是malloc ,这不需要进行类型转换。

In C, casting the result from malloc is unnecessary and should not be done. 在C中,从malloc转换结果是不必要的,不应该这样做。 Doing so can, for example, cover up the error of having failed to #include <stdlib.h> , so you don't have a prototype for malloc in scope. 例如,这样做可以掩盖错误#include <stdlib.h>的错误,因此您没有范围内的malloc原型。 This, in turn, can lead to other errors and lack of portability (though the worst offenders in that respect are now mostly obsolete). 反过来,这可能会导致其他错误和缺乏可移植性(尽管这方面最严重的违规者现在已经过时了)。

In C++, you must cast the result of malloc to assign it to a pointer to any type other than void. 在C ++中,必须转换malloc的结果,将其分配给指向void以外的任何类型的指针。 Unless you really need to write code that can be compiled as either C or C++, however, you should generally avoid using malloc in C++ at all and allocate memory using new . 但是,除非您确实需要编写可以编译为C或C ++的代码,否则通常应该避免在C ++中使用malloc并使用new分配内存。

You tend to see this kind of C code from novices (or C++ coders :-) ): 你倾向于从新手(或C ++编码器:-))看到这种C代码:

int main() {
    int len = 40;
    char *my_string = (char *) malloc(sizeof(char)*len);
    return 0;
}

This is unnecessary and evil, you can avoid the useless cast via including stdlib.h 这是不必要和邪恶的,你可以通过包括stdlib.h来避免无用的stdlib.h

#include <stdlib.h>
int main() {
    int len = 40;
    char *my_string = malloc(sizeof(char)*len);
    return 0;
}

You should strongly consider casting after using the malloc command because it provides for greater portability and greater compatibility with other parts of your program. 在使用malloc命令之后,您应该强烈考虑进行强制转换,因为它提供了更高的可移植性以及与程序其他部分的更好兼容性。 If you do not do so, you may run the risk of incompatible data types which could result in errors. 如果不这样做,您可能会遇到可能导致错误的不兼容数据类型的风险。

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

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