473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting (void*)&foo

Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jun 20 '06 #1
9 2683
Alex Vinokur wrote:
Is this approach safe?


Absolutely, for peculiar meanings of "safe". Some good rules of thumb:

* A C-style cast is never safe.
* A C-style cast to a void* is especially never safe.
* A C-style cast that disregards a const qualifier is ridiculously
especially never safe.
* And a C-style cast that tries to...

This approach is almost gratuitously unsafe. Please don't use this in
any real-world code. There are certainly some specific instances where
this approach is harmless, but in the main, this approach is just full
of all different kinds of things to avoid.

Jun 20 '06 #2
Alex Vinokur wrote:
Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}


void func3 (const Foo * const p_foo)
{
func1 ((void*)p_foo);
}

Is function func3() more safe than func2()?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jun 20 '06 #3
> void func3 (const Foo * const p_foo)
{
func1 ((void*)p_foo);
}

Is function func3() more safe than func2()?


Skydiving while drunk might be more safe than playing Russian roulette,
but I wouldn't want to do either.

Is it a C-style cast? Yep, check. Bad sign.

Is it a cast to void*? Yep, check. Bad sign.

Is it casting away constness? Yep, check. Bad sign.

Don't do this. If you're doing this in real code, please stop now.

Jun 20 '06 #4

Alex Vinokur wrote:
Alex Vinokur wrote:
Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}


void func3 (const Foo * const p_foo)
{
func1 ((void*)p_foo);
}

Is function func3() more safe than func2()?


No, equally unsafe.

But why would you func1 have void* as parameter? That is the source of
your corruption. Any function calling func1 would be dubious.

Change func1 to receive a specific type, else unsafe - period.

Regards,

Werner

Jun 20 '06 #5
Alex Vinokur wrote:
Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}


Since func1 actually dose nothing, despite the comment <g>, this code is
perfectly safe. If func1 is changed to do something that isn't safe,
then the code is not safe.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 20 '06 #6
Alex Vinokur posted:
Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}


Make an full program out of it first of all:
class Foo {};

void Func1( void* ) {}

void Func2( const Foo& foo )
{
Func1( (void*)&foo );
}

int main()
{
Foo obj;

Func2(obj);
}
The above program is well-formed and absent of undefined behaviour.

Style, on the other hand, is a different animal.

--

Frederick Gotham
Jun 20 '06 #7
Robert J. Hansen posted:
Alex Vinokur wrote:
Is this approach safe?
Absolutely, for peculiar meanings of "safe". Some good rules of thumb:

* A C-style cast is never safe.

Plenty of places where it's safe:

unsigned Func( unsigned long const val1, unsigned long const val2 )
{
return (unsigned)(val1 / val2);
}

* A C-style cast to a void* is especially never safe.

void *p;
void StoreAddress( void * const arg )
{
p = arg;
}
void AlterObject()
{
*static_cast<int *>(p) = 43;
}
int main()
{
int i;

StoreAddress( (void*)i );

/* Unneeded cast, but safe nonetheless *?

AlterObject();
}

* A C-style cast that disregards a const qualifier is ridiculously
especially never safe.

Again, it can be used to reinterpret_cast and const_cast in one foul
swoop.
The "new style" casts may be preferable over the "old style C casts", but
the old style C casts are as fully-fledged a feature of C++ as are the
"new style casts".
The one major advantage of C style casts is they're not as laborious to
type out:

(int)val

Versus:

static_cast<int>(val)
--

Frederick Gotham
Jun 20 '06 #8
In article <1150786821.704997.267180
@g10g2000cwb.googlegroups.com>,
al****@users.sourceforge.net says...
Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}


Is it safe to:

1) strip somebody naked
2) search them to ensure they have no weapons
3) paint a few targets on them
4) parachute them into the middle of a pitched battle

?

Technically, these steps may not cause death by
themselves. Nonetheless, you've done just about
everything you can to ensure that poor "foo" isn't going
to survive this experience...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 21 '06 #9

Frederick Gotham wrote:

The one major advantage of C style casts is they're not as laborious to
type out:

(int)val

Versus:

static_cast<int>(val)
Hmmm, given. One major advantage of C++ style casts is that they're
easy to find when looking for the source of your mistakes. Furthermore,
they concise. They have responsibility. They give certain guarantees.
An erroneous static_cast would fail to compile. C style casts compile
regardless of human error, and all humans make errors. For that reason,
I'll rather type .25 secs longer than look for a hard to find bug.

Regards,

Werner


--

Frederick Gotham


Jun 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian Byrne | last post by:
I've recently developed a minor interest in Expression Templates and, in attempt to further my understanding of template metaprogramming, have been trying to create my own implementation. It seems...
1
by: Jeff | last post by:
Hello everybody, I have a question concerning function declarations. When exactly do you have to declare functions if you want to use them? I have two functions main() and foo(), respectively...
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
2
by: Ray Tayek | last post by:
hi, trying to make an array of function pointers to make delegates with. but the compiler does not like: void (Foo::^p)()=&Foo::bar; i did find an article that showed how to convert a delegate to...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
1
by: Let_Me_Be | last post by:
Hi all, I'm developing a small defensive programming toolkit for my own projects. So, here are my questions. 1) Is it possible to move from something like this: SAFECALL(foo();) to __safecall...
2
by: Alex Vinokur | last post by:
classes that have virtual methods hold pointer to virtual table as additional implicit data member. So, sizeof of such classes is sizeof of all data (as struct-POD) plus 4. It seems that use of...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
18
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.