Often times, I'll have some malloc()'d data in a struct that need not
change throughout the lifetime of the instance of the struct.
Therefore, the field within the struct is declared a pointer to const
[something]. But then free() complains when I pass in the pointer
because free() is declared as void free(void*). So I have to cast.
Why is it not declared as void free(const void*), which would save me
these headaches?
-Peter 16 12273
Peter Ammon <pe*********@ro cketmail.com> writes: Why is it not declared as void free(const void*), which would save me these headaches?
Because that would make it easily possible to try to free data
that is actually defined as constant. You can always define your
own function for freeing "const" data:
void free_const(cons t void *p) { free((void *) p); }
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
In <cc*********@ne ws.apple.com> Peter Ammon <pe*********@ro cketmail.com> writes: Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to const [something]. But then free() complains when I pass in the pointer because free() is declared as void free(void*). So I have to cast.
Welcome to the world of const poisoning. Don't use const and your
troubles are gone.
Why is it not declared as void free(const void*), which would save me these headaches?
Because it would be semantically incorrect: this declaration promises
you that free() won't modify the data pointed to by its parameter. Or
the free() function is specified as *destroying* this data.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Dan Pop <Da*****@cern.c h> spoke thus: Welcome to the world of const poisoning. Don't use const and your troubles are gone.
But if things are really const, doesn't it make some sense from a
design standpoint to declare them as such?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
On Wed, 7 Jul 2004, Christopher Benson-Manica wrote: Dan Pop <Da*****@cern.c h> spoke thus: Welcome to the world of const poisoning. Don't use const and your troubles are gone.
But if things are really const, doesn't it make some sense from a design standpoint to declare them as such?
Of course. But Peter Ammon was declaring something as 'const' that
patently was *not* constant --- its target was being allocated and
de-allocated (changing the contents of the pointer variable as well
as the contents of the target) during its own lifetime.
"Const poisoning" is certainly an annoying problem when it arises,
but it *usually* indicates poor design, not a flaw in the language.
Certainly in this case it's a symptom of poor design --- if you're
malloc'ing and free'ing an object, it certainly shouldn't be 'const'!
-Arthur
On 7 Jul 2004 13:09:02 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c: In <cc*********@ne ws.apple.com> Peter Ammon <pe*********@ro cketmail.com> writes:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to const [something]. But then free() complains when I pass in the pointer because free() is declared as void free(void*). So I have to cast.
Welcome to the world of const poisoning. Don't use const and your troubles are gone.
Why is it not declared as void free(const void*), which would save me these headaches?
Because it would be semantically incorrect: this declaration promises you that free() won't modify the data pointed to by its parameter. Or the free() function is specified as *destroying* this data.
No, it is defined to do no such thing.
<quote>
7.20.3.2 The free function
Synopsis
1 #include <stdlib.h>
void free(void *ptr);
Description
2 The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If ptr is
a null pointer, no action occurs. Otherwise, if the argument does not
match a pointer earlier returned by the calloc, malloc, or realloc
function, or if the space has been deallocated by a call to free or
realloc, the behavior is undefined.
Returns
3 The free function returns no value.
<end quote>
Not one single word about destroying the data, or indeed any
specification at all about what happens to the contents of the memory.
In point of fact, a strictly conforming program cannot determine
whether the contents of the memory is modified in any way or not.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:pg******** *************** *********@4ax.c om... Not one single word about destroying the data, or indeed any specification at all about what happens to the contents of the memory. In point of fact, a strictly conforming program cannot determine whether the contents of the memory is modified in any way or not.
The contents of the memory might not be modified, but any use of the pointer
itself becomes undefined after calling free(), so the semantics of the
argument have changed even if the value might not have. Declaring free()'s
argument as const would be nonsensical as the pointer does not have the same
semantics after the call as it did before the call, and the value of the
pointed-to memory is undefined as well.
S
--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
In <pg************ *************** *****@4ax.com> Jack Klein <ja*******@spam cop.net> writes: On 7 Jul 2004 13:09:02 GMT, Da*****@cern.ch (Dan Pop) wrote in comp.lang.c:
In <cc*********@ne ws.apple.com> Peter Ammon <pe*********@ro cketmail.com> writes:
>Often times, I'll have some malloc()'d data in a struct that need not >change throughout the lifetime of the instance of the struct. >Therefore, the field within the struct is declared a pointer to const >[something]. But then free() complains when I pass in the pointer >because free() is declared as void free(void*). So I have to cast.
Welcome to the world of const poisoning. Don't use const and your troubles are gone.
>Why is it not declared as void free(const void*), which would save me >these headaches?
Because it would be semantically incorrect: this declaration promises you that free() won't modify the data pointed to by its parameter. Or the free() function is specified as *destroying* this data.
No, it is defined to do no such thing.
<quote> 7.20.3.2 The free function
Synopsis
1 #include <stdlib.h> void free(void *ptr);
Description
2 The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
Returns
3 The free function returns no value. <end quote>
Not one single word about destroying the data, or indeed any specificatio n at all about what happens to the contents of the memory. In point of fact, a strictly conforming program cannot determine whether the contents of the memory is modified in any way or not.
Learn to read, Jack, learn to read. If a block is deallocated, what
happens to the data it contained before deallocation? It becomes
irreversibly inaccessible, so *for all intents and purposes* it is
destroyed. No one cares whether it was actually touched by the execution
of free(), this is immaterial.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <Xn************ *************** @212.27.42.72> Emmanuel Delahaye <em**********@n oos.fr> writes: In 'comp.lang.c', Da*****@cern.ch (Dan Pop) wrote:
Because it would be semantically incorrect: this declaration promises you that free() won't modify the data pointed to by its parameter. Or ----------------------------------------------------------------------^ Frenchism ? Do you mean 'thus' or 'however' ? Just kidding!
^^^^^^^^^
I am not a native French speaker, so whatever mistakes I make are very
unlikely to be caused by my French ;-)
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex Vinokur |
last post by:
Various forms of argument passing
=================================
C/C++ Performance Tests
=======================
Using C/C++ Program Perfometer
http://sourceforge.net/projects/cpp-perfometer
http://alexvn.freeservers.com/s1/perfometer.html
|
by: BlindHorse |
last post by:
Help!!!
I need someone to tell me why I am getting the err msg
error C2440: '=' : cannot convert from 'char *' to 'char'
//====================
#include <iostream>
|
by: Colin Doig |
last post by:
Hi, I need to pass an array of strings to a function, but I can't get it
to work. This is what I wrote :
#include <stdio.h>
void a_function(char *blah) {
printf("%s %s %s", blah, blah, blah);
}
|
by: Goh, Yong Kwang |
last post by:
I'm trying to create a function that given a string, tokenize it and
put into a dynamically-sized array of char* which is in turn also
dynamically allocated based on the string token length.
I call the function using this code fragement in my main function:
---
char** arg_array;
arg_count = create_arg_array(command, argument, arg_array);
for(count = 0; count < arg_count; count++)
|
by: Enrico `Trippo' Porreca |
last post by:
Given:
typedef struct Node Node;
struct Node {
void *obj;
Node *next;
};
typedef struct Stack Stack;
struct Stack {
| |
by: george doubleu |
last post by:
hi,
i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
I get the two warnings you can see in the remarks. The second warning is
perfectly OK for me, but the first one I don't understand.
Isn't the "const int *" construct in the signature of func1 a hint for the
user, that func1 doesn't modify **arg? Why then is it dangerous to
pass an alterable argmument?
|
by: MackS |
last post by:
I've come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:
void fun(char **array_of_strings, int num_elements);
int main(void)
{
char static_array_of_strings;
fun ((char**)&static_array_of_strings, NUM_STRINGS);
|
by: Charles Sullivan |
last post by:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
|
by: wellingj |
last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from.
so here is the error I'm getting:
TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |