Why does c++ forbid implicit conversion of void*?
This question already has an answer here:
In C++, unlike in C, you have to cast the result of malloc. Your code can be tweaked into working order with a simple cast.
int *a = (int *)malloc(sizeof(int));
A great article about this mandatory cast and the reasons behind it can be found here.
An additional link for reference can be found here.
Edit: As suggested in the comments, the use of malloc()
should not be commonplace. The closest alternative is using new
to allocate.
int *a = new int[15];
Additional Edit: As again suggested in the comments, if you have to use malloc()
, at least use a C++ cast.
int *a = static_cast<int*>malloc(sizeof(int)); // shout out to @edheal, @mgetz
You can do the conversion in C++, but it requires a cast. C++ aims to be a more type-safe language than C, so it tries to close some "holes in the type system" that C allows.
In C, this will be accepted without any diagnostics being required:
int x;
void *p = &x;
double *q = p;
*q = 0.0;
Type safety has been violated without any casts being present in the source code.
This is a C++ FAQ answered by B. Stroustrup, inventor of C++.
C++ doesn't ban the conversion; it just wants it to be documented by some kind of blurb in the source code, namely a cast which at least suggests, if not proves, that it's being done on purpose.
About the origin of void *
, Stroustrup writes this (bolding mine):
Late in its history, C with Classes* began to support the notion of a pointer to ''raw memory, void *
. The origin of void *
is shrouded in some mystery. I vaguely remember inventing it together with Larry Rosler and Steve Johnson. However, Dave Prosser remembers first having suggested it based on something used "somewhere in Australia." Possibly both versions are correct because Dave worked closely with Larry at the time. In either case, void *
was introduced into both languages more or less at the same time . The earliest mention of void *
that I can find is in a memo dated January 1, 1983, about the memory management mechanisms provided by my C++ compiler, Cfront, so the origins of void *
in C++ must go back at least to mid-1982. The earliest written record of void *
in the context of ANSI C is a proposal by Mike Meissner dated "12 Oct 83," which presented void *
essentially as it was accepted into ANSI C in June 1984 [Prosser,2001]
So C++ inherits the void *
concept as Stroustrup originally envisioned it, while around the same time, the C people had a slightly different idea that was more loose in type safety.
In the case of malloc
, of course there are dangers; but those are already flagged by the presence of the well-known identifier malloc
; the cast doesn't bring in anything more, but not requiring it specifically for malloc
, while requiring it everywhere else would be an awkward exception to the rules.
* "C with Classes" is the predecessor to C++
链接地址: http://www.djcxy.com/p/28428.html