473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are implicit casts to void*& illegal?

I searched around but couldn't find any previous discussion on this, so...

I have a macro that implements a common memory disposal function, e.g.:

#define safe_free(void* pv) if(pv) { ::free(pv); (pv) = NULL; }

(Yes, its free() instead of operator delete, but I don't think using the
newer memory semantics matters in this case.)

Since another project might have an entity of the same name, and macros
are in the global namespace, to avoid namespace collision I thought an
inline function might be better:

namespace myspace
{
inline void safe_free(void* & pv) { ... }
}

(Having pv as a void*& lets pv be assigned to NULL).
However, calling code can no longer do this:

using namespace myspace;
char* p = (char*)::malloc (100);
safe_free(p); // error: char* cannot be
// converted to void*&.
Doing

safe_free((void *)p);

fails also (can't convert void* to void*&), which seems odd because

void* pv = (void*)p;
safe_free(pv);

works fine.

So to use the function, I wound up doing

safe_free((void *&)p);

which is not the end of the world but tragically inelegant considering
that references usually increase elegance. Implicit casts to void* are
legal, but not void*& -- strange.

This is happening in MSVC 6. I was wondering if others had the same
problem in other compilers or other versions of MSVC.

This template would work:

template <type T> void safe_free(T*& p)
{ ... }

but it seems like overkill to have to instantiate a template function
for all possible pointer types when the function only needs the
pointer's value, not the type of object being pointed to.

Ray
Apr 19 '06 #1
3 2442
Ray Gardener wrote:
I searched around but couldn't find any previous discussion on this,
so...
I have a macro that implements a common memory disposal function,
e.g.:
#define safe_free(void* pv) if(pv) { ::free(pv); (pv) = NULL; }
Actually, you need to drop the "void*" part. And there is no need to
check for its null-ness. 'free' does nothing if you pass a null pointer
to it.
(Yes, its free() instead of operator delete, but I don't think using
the newer memory semantics matters in this case.)
They may not, but only if the pointer is obtained from 'malloc' or
'realloc'. If you get the pointer from 'new', you can't 'free' it.
Since another project might have an entity of the same name, and
macros are in the global namespace, to avoid namespace collision I
thought an inline function might be better:

namespace myspace
{
inline void safe_free(void* & pv) { ... }
}

(Having pv as a void*& lets pv be assigned to NULL).
OK.
However, calling code can no longer do this:

using namespace myspace;
char* p = (char*)::malloc (100);
safe_free(p); // error: char* cannot be
// converted to void*&.
Make your 'safe_free' a template.
Doing

safe_free((void *)p);

fails also (can't convert void* to void*&), which seems odd because

void* pv = (void*)p;
safe_free(pv);

works fine.

So to use the function, I wound up doing

safe_free((void *&)p);

which is not the end of the world but tragically inelegant considering
that references usually increase elegance. Implicit casts to void* are
legal, but not void*& -- strange.
Yes, and it should be dropped.
This is happening in MSVC 6. I was wondering if others had the same
problem in other compilers or other versions of MSVC.

This template would work:

template <type T> void safe_free(T*& p)
{ ... }
That's right! It is perfect.
but it seems like overkill to have to instantiate a template function
for all possible pointer types when the function only needs the
pointer's value, not the type of object being pointed to.


It's not overkill. Declare it 'inline' and forget about it.

V
--
Please remove capital As from my address when replying by mail
Apr 19 '06 #2
Yes, you're right; I forgot that this is an inline function anyway.
Sigh... got so paranoid about template instantation bloat I've let it
get the better of me. :)

Thanks,
Ray
Victor Bazarov wrote:
Ray Gardener wrote:
I searched around but couldn't find any previous discussion on this,
so...
I have a macro that implements a common memory disposal function,
e.g.:
#define safe_free(void* pv) if(pv) { ::free(pv); (pv) = NULL; }


Actually, you need to drop the "void*" part. And there is no need to
check for its null-ness. 'free' does nothing if you pass a null pointer
to it.
(Yes, its free() instead of operator delete, but I don't think using
the newer memory semantics matters in this case.)


They may not, but only if the pointer is obtained from 'malloc' or
'realloc'. If you get the pointer from 'new', you can't 'free' it.
Since another project might have an entity of the same name, and
macros are in the global namespace, to avoid namespace collision I
thought an inline function might be better:

namespace myspace
{
inline void safe_free(void* & pv) { ... }
}

(Having pv as a void*& lets pv be assigned to NULL).


OK.
However, calling code can no longer do this:

using namespace myspace;
char* p = (char*)::malloc (100);
safe_free(p); // error: char* cannot be
// converted to void*&.


Make your 'safe_free' a template.
Doing

safe_free((void *)p);

fails also (can't convert void* to void*&), which seems odd because

void* pv = (void*)p;
safe_free(pv);

works fine.

So to use the function, I wound up doing

safe_free((void *&)p);

which is not the end of the world but tragically inelegant considering
that references usually increase elegance. Implicit casts to void* are
legal, but not void*& -- strange.


Yes, and it should be dropped.
This is happening in MSVC 6. I was wondering if others had the same
problem in other compilers or other versions of MSVC.

This template would work:

template <type T> void safe_free(T*& p)
{ ... }


That's right! It is perfect.
but it seems like overkill to have to instantiate a template function
for all possible pointer types when the function only needs the
pointer's value, not the type of object being pointed to.


It's not overkill. Declare it 'inline' and forget about it.

V

Apr 19 '06 #3

Ray Gardener wrote:
Yes, you're right; I forgot that this is an inline function anyway.
Sigh... got so paranoid about template instantation bloat I've let it
get the better of me. :)

Thanks,
Ray

[snip]

Actually i do not believe there is any reason to worry about template
bloat these days. Improvements in the linker and compiler have come a
far way - to an extent that code from different templates will be
merged if the code is the same. This happens for e.g.
std::vector::pu sh_back which is the same code for int and unsigned int.
So even if your code was not inline, most probably all instantations
would fold to the same code.

/Peter

Apr 19 '06 #4

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

Similar topics

3
1637
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 I've ran into a problem that, while there are ways around it (causing way for code duplication), I'm not too sure if a direct solution exists. Given: template< typename T > class A { ... }; template< typename T >
22
448
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a pointer to a pointer in the .c: extern struct example_struct **pointer; and somewhere in the code i tried: pointer = &ex_ptr;
44
2206
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
5
2035
by: Matt | last post by:
I am trying to cast an ostream reference to void* and back again. The code below shows the problem isolated from a more complex program. It compiles quietly but seg faults upon execution. // =================================================================== #include <iostream> using namespace std; void myprint(void *s) { (ostream&)s << "hello, world" << endl;
9
2695
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
1
1891
by: askcq | last post by:
what is the difference between these (void*)(&abc) and (void *)(abc) where abc is a local varaible inside a function
4
1712
by: Oliver Block | last post by:
Hello, sometimes you can read that a function requiring an argument of tpye void ** is submitted something like the following: mystruct **ppval; myfunc((void **)&ppval, ...); I would expect that one would instead use
0
1810
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome.
82
2795
by: arnuld | last post by:
PURPOSE :: see statement in comments GOT: Segmentation Fault I guess the segfault is sourced in the compile-time warning but I am giving a char* to the function already.
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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 we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.