Re: warning C4238 : cast reference in void*

Tech-Archive recommends: Fix windows errors by optimizing your registry




Eric wrote:
Ulrich Eckhardt wrote:
Eric wrote:
void foo(void* param)[...]

foo((void*) &szWord); //give C4238 warning : nonstandard extension
used : class rvalue used as lvalue

1. For your own sake, forget about the fact that C-style casts like the
above exist. I haven't found the need for one in over 1M lines of code and
you surely don't have such a case here.
Conversion from a typed pointer to a void pointer is implicitly done by the
compiler, so a cast isn't necessary. If you absolutely want, use a
static_cast, which is also the right cast to convert to a typed pointer
again.
2. Igor already mentioned it, this can't be the code you tried to compile,
please show the real code for an analysis of its problems.

Uli

Thanks all, sorry I didn't give you the good code.

CString foo2(CString aString)
{
return aString;
}
void* foo(CString &aString)
{
return (void*)&foo2(aString); //give C4238 warning

Thats like shooting yourself in the foot: twice. foo2 returns a
temporary and foo returns a typeless pointer to a local temporary. Its
so unfortunate that the compiler generates only a warning in this case
although the programmer should see the issue instantly. Void pointers
are deceptive enough but a void pointer to garbage is brain dead logic
(sorry).
Please use a static_cast instead of prehistoric casting mechanism. The
above 2 functions can be replaced by:

CString s;
void* p = static_cast<void*>(&s);

There is no way i can explain how inherently dangerous and deeply
fallacious those 2 functions are.
They both ignore scope and lifetimes flagrantly. They are plain evil.
Black Holes(c).

}
int main()
{
CString aString;
void* ptr = foo(aString);

Is basicly *guarenteed* to point to nothing.

}

Eric

.



Relevant Pages

  • Re: Noob Q: How to properly type cast in this case
    ... typedef signed char Small_Int; ... to a 'Small_Int') to a void pointer as if you had written explicitely ... But you can't cast the left hand side. ... int x = 13; ...
    (comp.lang.c)
  • Re: Can I check the type of a void pointer
    ... > cast it back to exactly what it was before. ... > from a common base class and use that rather than void*. ... > the base class is polymorphic (has a virutal function), ... if you /need/ a void pointer (say for some C-ish ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Can I check the type of a void pointer
    ... > cast it back to exactly what it was before. ... > from a common base class and use that rather than void*. ... > the base class is polymorphic (has a virutal function), ... if you /need/ a void pointer (say for some C-ish ...
    (comp.lang.cpp)
  • Re: Type-casting void pointers?
    ... Is there a good reason the void pointer ... args are cast to byte just to assign their ... The addresses are all unsigned int. ...
    (comp.lang.c)