7

As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*-to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place.

Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the same location in memory, with a different type, much like type-punning is undefined where not punning back to an object's actual type.

So while malloc clearly returns the address of suitable memory and so on, there does not seem to be any way to actually make use of it, portably, as far as I have seen.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
ben
  • 1,994
  • 12
  • 20
  • 1
    A related question with a good accepted answer: http://stackoverflow.com/questions/1863069/casting-via-void-instead-of-using-reinterpret-cast – James McNellis Apr 10 '10 at 18:19
  • I was hoping for a more explicit answer that involves less interpretation and handwaving. `static_cast` ought to be less convoluted than reasoning about `reinterpret_cast`, after all. – ben Apr 10 '10 at 18:38
  • 3
    i doubt that there is an answer with less "handwaving" and interpretation (not sure what you mean by "handwaving"). The Stadnard is rare on absolute guarantees on purpose at times. It's implied that `static_cast(voidptr)` just works and yields the original value, without explicit wording saying that. There is no reason for an implementation to change the value, but the Standard doesn't explicitly forbid it either. An implementation that wants to behave weird is already allowed to crash on the simplest things (exceeding of implementation limits). – Johannes Schaub - litb Apr 10 '10 at 19:01
  • @JohannesSchaub-litb "_just works and yields the original value, without explicit wording saying that_" "A value of type pointer to object converted to “pointer to cv void” and back, possibly with different cv-qualification, shall have its original value." – curiousguy Dec 13 '11 at 06:03

3 Answers3

3

C++0x standard draft has in 5.2.9/13:

An rvalue of type “pointer to cv1 void” can be converted to an rvalue of type “pointer to cv2 T,” where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. The null pointer value is converted to the null pointer value of the destination type. A value of type pointer to object converted to “pointer to cv void” and back, possibly with different cv-qualification, shall have its original value.

But also note that the cast doesn't necessarily result in a valid object:

 std::string* p = static_cast<std::string*>(malloc(sizeof(*p)));
 //*p not a valid object 
UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • 2
    `malloc` doesn't convert a value of type pointer-to-object to a pointer-to-void, though; it just gives you a pointer of type pointer-to-void. Hence, I don't think the last sentence of 5.2.9/13 applies. – James McNellis Apr 10 '10 at 18:43
  • Me either. It was my intent to refer to that sentence in my question and to point out that I do not believe that it helps in this case. Also I am not interested in the part where I dereference the pointer but more in acquiring it in the first place. – ben Apr 10 '10 at 18:50
  • 3
    The last sentence doesn't apply, because there is no back and forth conversion and no original value. I don't see why the rest shouldn't apply. - Doesn't it simply say: a `void*` can be statically cast to `T*`; if the pointer is NULL, it's OK; if the `void*` was originally a `T*` it's just super. – UncleBens Apr 10 '10 at 19:01
  • 1
    I agree, and I don't think there is any better wording in the standard. I think @ben is looking for a guarantee _like_ that last sentence contains, but I don't think there is one. – James McNellis Apr 10 '10 at 19:04
  • 1
    however it doesn't say what the resulting value is. It says so explicitly for other conversions, like "An rvalue of type float can be converted to an rvalue of type double. The value is unchanged" – Johannes Schaub - litb Apr 10 '10 at 19:08
2

C++03, §20.4.6p2

The contents are the same as the Standard C library header <stdlib.h>, with the following changes: [list of changes that don't apply here]

C99, §7.20.3.3p2-3

(Though C++03 is based on C89, I only have C99 to quote. However, I believe this section is semantically unchanged. §7.20.3p1 may also be useful.)

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

The malloc function returns either a null pointer or a pointer to the allocated space.

From these two quotes, malloc allocates an uninitialized object and returns a pointer to it, or returns a null pointer. A pointer to an object which you have as a void pointer can be converted to a pointer to that object (first sentence of C++03 §5.2.9p13, mentioned in the previous answer).


This should be less "handwaving", which you complained of, but someone might argue I'm "interpreting" C's definition of malloc as I wish, by, for example, noticing C says "to the allocated space" rather than "to the allocated object". To those people: first realize that "space" and "object" are synonyms in C, and second please file a defect report with the standard committees, because not even I am pedantic enough to continue. :)

I'll give you the benefit of the doubt and believe you got tripped up in the cross-references, cross-interpretation, and sometimes-confused integration between the standards, rather than "space" vs "object".

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
1

Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer,

Yes, indeed.

So while malloc clearly returns the address of suitable memory and so on, there does not seem to be any way to actually make use of it, portably, as far as I have seen.

Of course there is:

void *vp = malloc (1);
char *cp;
memcpy (&cp, &vb, sizeof cp);
*cp = ' ';

There is one tiny problem : it does not work for any other type. :(

curiousguy
  • 8,038
  • 2
  • 40
  • 58
  • 1
    IRC you can use placement new. – Dan M. Nov 07 '18 at 22:54
  • @DanM. Yes, of course you could use a C library function with a specifically C++ syntax. Then the whole point of having access to C library with a C/C++ syntax is lost, and the whole C/C++ compatibility *for non trivial code*. Being able to use reasonable C code with few or no changes was explicitly listed as a mission statement of the initial design of C++ as well as C++ standard committee. I know that some people now despise the subset of C programs that are valid in C++, and treat of any attempt at preserving the C/C++ subset as a waste of time, backward thinking, etc. They are very wrong. – curiousguy Nov 07 '18 at 23:26