473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generalized function for deallocating and setting pointer to NULL

I've been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn't, of deallocating and setting
it's value to NULL regardless of the pointer's type. I thought "void
** " should do the trick, but I kept getting the following warning
message from gcc:

warning: passing argument 1 of 'free_var' from incompatible pointer
type

which obviously could be solved by doing an explicit cast to (void **)
in the argument list when calling the fuction. That however would put
an extra burden on the programmer, so I invented the following
solution which works quite well apparently. Please give me your
opinions on this issue.

void free_var(void *vptr)
{
void **ptr = (void **) vptr;

if (ptr != NULL) {
if (*ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
}

Thank you.
Nov 29 '07
27 2224
On Thu, 29 Nov 2007 09:27:09 GMT, Tomás Ó hÉilidhe <to*@lavabit.co m>
wrote:
>rocco.rossi:
>I've been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn't, of deallocating and setting it's
value to NULL regardless of the pointer's type. I thought "void ** "
should do the trick, but I kept getting the following warning message
from gcc:


void DeallocateAndNu llify(void **const pp)
{
free (*pp);

*pp = 0;
}
How would I pass an int* (or its address) to this function and avoid
the undefined behavior when sizeof(void*) != sizeof(int*)?
Remove del for email
Dec 1 '07 #11
Barry Schwarz wrote:
Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
>rocco.rossi:
>>I've been trying to write a function capable of checking if a
pointer value is set to NULL, and if it isn't, of deallocating
and setting it's value to NULL regardless of the pointer's type.
I thought "void ** " should do the trick, but I kept getting the
following warning message from gcc:

void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}

How would I pass an int* (or its address) to this function and
avoid the undefined behavior when sizeof(void*) != sizeof(int*)?
....
int *p;
....
DeallocAndNulli fy(&p);

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #12
On Sat, 01 Dec 2007 02:41:31 -0500, CBFalconer wrote:
Barry Schwarz wrote:
>Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
>>void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}

How would I pass an int* (or its address) to this function and avoid
the undefined behavior when sizeof(void*) != sizeof(int*)?

....
int *p;
....
DeallocAndNulli fy(&p);
Well yes, if your code won't compile, you won't have undefined behaviour,
but I doubt that's what Barry Schwarz meant.

There is no implicit conversion from int ** to void **. There is no point
in an implicit conversion from int ** to void ** either, since the
representations of int * and void * can be completely different.
Dec 1 '07 #13
CBFalconer <cb********@yah oo.comwrites:
Barry Schwarz wrote:
>Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
>>rocco.rossi :
I've been trying to write a function capable of checking if a
pointer value is set to NULL, and if it isn't, of deallocating
and setting it's value to NULL regardless of the pointer's type.
I thought "void ** " should do the trick, but I kept getting the
following warning message from gcc:

void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}

How would I pass an int* (or its address) to this function and
avoid the undefined behavior when sizeof(void*) != sizeof(int*)?

....
int *p;
....
DeallocAndNulli fy(&p);
[...]

Nope. &p is of type int**; the parameter is of type void**. There is
no implicit conversion from int** to void**.

Also, your call is to DeallocAndNulli fy rather than
DeallocateAndNu llify. (Perhaps you tried compiling your code, and the
misspelling caused the compiler not to know what the function
expects.)

[Chuck: aioe.org is free, doesn't add its own signature, and doesn't
even require signing up; you just have to set $NNTPSERVER. See their
web page for details. I've been using it myself while rr.com is under
a UDP. Please consider it.]

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 1 '07 #14
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
>Barry Schwarz wrote:
>>Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
rocco.ross i:
I've been trying to write a function capable of checking if a
pointer value is set to NULL, and if it isn't, of deallocating
and setting it's value to NULL regardless of the pointer's type.
I thought "void ** " should do the trick, but I kept getting the
following warning message from gcc:
void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}
How would I pass an int* (or its address) to this function and
avoid the undefined behavior when sizeof(void*) != sizeof(int*)?
....
int *p;
....
DeallocAndNulli fy(&p);
[...]

Nope. &p is of type int**; the parameter is of type void**. There is
no implicit conversion from int** to void**.
But int* and void* are compatible.(?) Therefore..
DeallocAndNulli fy((void*)&p);
...should work.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 1 '07 #15
Joe Wright wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>Barry Schwarz wrote:
Tomás � h�ilidhe <to*@lavabit.co mwrote:
rocco.rossi :
>I've been trying to write a function capable of checking if a
>pointer value is set to NULL, and if it isn't, of deallocating
>and setting it's value to NULL regardless of the pointer's type.
>I thought "void ** " should do the trick, but I kept getting the
>followin g warning message from gcc:
void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}
How would I pass an int* (or its address) to this function and
avoid the undefined behavior when sizeof(void*) != sizeof(int*)?
....
int *p;
....
DeallocAndNulli fy(&p);
[...]

Nope. &p is of type int**; the parameter is of type void**. There
is no implicit conversion from int** to void**.

But int* and void* are compatible.(?) Therefore..
DeallocAndNulli fy((void*)&p);
..should work.
Pass by value semantics mean that the functions becomes useless.

Dec 1 '07 #16
On Sat, 01 Dec 2007 10:42:02 -0500, Joe Wright wrote:
But int* and void* are compatible.(?) Therefore..
DeallocAndNulli fy((void*)&p);
..should work.
int* and void* are not compatible any more than int and double are. There
are implicit conversions between the two, but portable code can't
reinterpret the bit pattern of one as the other.
Dec 1 '07 #17
Joe Wright wrote, On 01/12/07 15:42:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>Barry Schwarz wrote:
Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
rocco.rossi :
>I've been trying to write a function capable of checking if a
>pointer value is set to NULL, and if it isn't, of deallocating
>and setting it's value to NULL regardless of the pointer's type.
>I thought "void ** " should do the trick, but I kept getting the
>followin g warning message from gcc:
void DeallocateAndNu llify(void **const pp) {
free (*pp);
*pp = 0;
}
How would I pass an int* (or its address) to this function and
avoid the undefined behavior when sizeof(void*) != sizeof(int*)?
....
int *p;
....
DeallocAndNulli fy(&p);
[...]

Nope. &p is of type int**; the parameter is of type void**. There is
no implicit conversion from int** to void**.

But int* and void* are compatible.(?)
There is an implicit conversion between them which is not quite the same
thing.
Therefore..
DeallocAndNulli fy((void*)&p);
..should work.
No, see the comp.lang.c FAQ question 4.9 and http://c-faq.com/
I'm surprised not of the regulars have posted this reference yet.
--
Flash Gordon
Dec 1 '07 #18
Joe Wright wrote:
....
But int* and void* are compatible.(?)
What made you think that?
Dec 1 '07 #19
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
.... snip ...
>>
....
int *p;
....
DeallocAndNulli fy(&p);
[...]

Nope. &p is of type int**; the parameter is of type void**.
There is no implicit conversion from int** to void**.

Also, your call is to DeallocAndNulli fy rather than
DeallocateAndNu llify. (Perhaps you tried compiling your code, and
the misspelling caused the compiler not to know what the function
expects.)
Consider the following test (marked as quotation for linewrap):
[1] c:\c\junk>cat junk.c
#include <stdio.h>
#include <stdlib.h>

void release(void* *p) {
puts("Was Alive");
free(*p);
*p = NULL;
} /* release */

/* ------------------- */

int main(void) {
int *ptr;

if (ptr = malloc(sizeof *ptr)) release(&ptr);
if (ptr) puts("Alive");
else puts("Dead");

return 0;
} /* main, fcopylns */

[1] c:\c\junk>cc junk.c
junk.c: In function `main':
junk.c:15: warning: suggest parentheses around assignment used as truth value
junk.c:15: warning: passing arg 1 of `release' from incompatible pointer type

[1] c:\c\junk>a
Was Alive
Dead
Now I maintain the error is really a chimera. In release the void*
is never dereferenced, but is simply passed to free (which knows
what to do with it) and is set to NULL before exiting. The
parameter is simply a pointer to a void*.

Of course it would not do to ignore the error in passing the
parameter without analyzing the use in release, and that is
probably excessive detail.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #20

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

Similar topics

6
1748
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only have one and make the data representation (a std::valarray) mutable instead. My question is, how bad is that? Am I running the risk of undefined behaviour, or is the worst case scenario simply const violation? -- Christopher Diggins...
2
11527
by: Devrobcom | last post by:
Hi I know that this is not the Lint forum, but I get some Lint warnings when setting my pointers to NULL. And the problem is I don't know howto cast a function ptr the right way. typedef int FP(char*, char*); FP *pfp; pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to void/nonvoid))
13
2370
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
23
7823
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
5
3091
by: Jean-Guillaume Pyraksos | last post by:
I want to work with "generalized lists" as in Lisp, say : ((23 () . 2) () ((10))) ; only pointers and integers So the basic element is a node, a struct with two fields car and cdr. Each of these fields can contain either a pointer (NULL or a pointer to another node), or an int. I want to define the functions cons, car and cdr of Lisp.
78
4981
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work...
5
2263
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were the contents of the malloc object in main driver module is copied. Which would mean that if I free the object_copy it should not effect
27
3134
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
18
2106
by: WaterWalk | last post by:
Hello. Suppose there is an implementation of C++, in which when a class object is allocated, its member functions are also allocated in addition to its data members. So that every class object has a copy of all of its member functions. When a member function is called, it's the object's copy that When a class object is deallocated, the corresponding member functions are also deallocated. Don't consider optimization or performance, 1....
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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
5494
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
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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.