473,725 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const-incorrect practice

A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?
How much do you need to fiddle with "const_cast " because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?

It seems that there exist strong different opinions when this technique
becomes a valuable tool for the detection of security flaws and the
improvement of the overall source code quality. Does the experience
change over time to achieve a common sense?
Do developers and programmers need key/success stories like compiler
optimisations and error avoidance to give "correctnes s" a try and to
let it become a common practice?
Do you want to show that the maintenance effort is worth for the
implementation way of immutable data structures?
http://en.wikipedia.org/wiki/Const_correctness

Regards,
Markus

Jul 23 '05 #1
11 1820
Ma************@ web.de writes:
[...]
How much do you need to fiddle with "const_cast " because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?


C has no "const_cast ". Followups redirected.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #2
Ma************@ web.de wrote:
A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?
Not difficult at all:
Just define that unless needed all member functions are const,
and, with very few exceptions, do not allow the use of const_cast
and/or mutable.

There are a few situations how the const-correctness can be violated,
all of them returning non-const references or pointers
from const functions. E.g.,

class Class {
// constructor, copy, assigment, destructor ommitted.
public:
int& getInt() const
{
return *i_;
}
private:
int* i_;
};

void foo(const Class& c)
{
c.getInt() = 5;
}

will compile happily on most compilers.
How much do you need to fiddle with "const_cast " because the key word
"const" was forgotten for type specifiers in important cases by an API
designer?
I don't remember any case where I needed to const_cast because of an
API.

It seems that there exist strong different opinions when this technique
becomes a valuable tool for the detection of security flaws and the
improvement of the overall source code quality. Does the experience
change over time to achieve a common sense?
Do developers and programmers need key/success stories like compiler
optimisations and error avoidance to give "correctnes s" a try and to
let it become a common practice?
Do you want to show that the maintenance effort is worth for the
implementation way of immutable data structures?
http://en.wikipedia.org/wiki/Const_correctness
Not so much the maintenance effort. The big benefit comes when it is
about
problem hunting: If a function takes a const argument, there's no need
to follow the "path" of the argument through the function, provided
const_cast is not used.

Regards,
Markus


Jul 23 '05 #3
Stephan Brönnimann wrote:
Ma************@ web.de wrote:
A couple of software provides const-incorrect programming interfaces. I
guess that it is possible to develop const-correct APIs/SDKs from the
beginning if a few basic design rules and patterns would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?


Not difficult at all:
Just define that unless needed all member functions are const,
and, with very few exceptions, do not allow the use of const_cast
and/or mutable.

There are a few situations how the const-correctness can be violated,
all of them returning non-const references or pointers
from const functions. E.g.,

class Class {
// constructor, copy, assigment, destructor ommitted.
public:
int& getInt() const
{
return *i_;
}
private:
int* i_;


Please do not post this off-topic stuff to c.l.c.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Jul 23 '05 #4
> C has no "const_cast ". Followups redirected.

How often do you need to cast constness away?

Jul 23 '05 #5
> Please do not post this off-topic stuff to c.l.c.

Are any use cases for the demonstration of "const correctness" really
"off-topic" to this discussion?

Jul 23 '05 #6
Ma************@ web.de wrote:

How often do you need to cast constness away?


Fairly frequently when writing encapsulated data
types in C. I'll have a struct like

struct s {
const char *name;
...
};

.... and a "constructo r" (I guess "factory method" might
be a better term for what we have in C) that does

struct s *sp = malloc(sizeof *sp);
sp->name = malloc(strlen(t he_name) + 1);
strcpy((char*)( sp->name), the_name);
...

Now, one doesn't actually "need" the const-removing
cast for this. A cast-less alternative can be written as

struct s *sp = malloc(sizeof *sp);
char *name = malloc(strlen(t he_name) + 1);
strcpy(name, the_name);
sp->name = name;
...

However, I see no way to avoid the cast in the
matching "destructor :"

void destroy_s(struc t s *sp) {
...
free((char*)(sp->name));
free(sp);
}

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 23 '05 #7
Ma************@ web.de wrote:
C has no "const_cast ". Followups redirected.


How often do you need to cast constness away?


Why did you override the follow-up? Are you deliberately trying to
annoy the readers of c.l.c? Why did you remove the attributions?
You are showing many signs of being a troll.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Jul 23 '05 #8
Ma************@ web.de wrote:
Please do not post this off-topic stuff to c.l.c.


Are any use cases for the demonstration of "const correctness"
really "off-topic" to this discussion?


Again deliberately overriding the preset follow-ups. Definitely a
troll. PLONK.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Jul 23 '05 #9
> Why did you override the follow-up? Are you deliberately trying to
annoy the readers of c.l.c? Why did you remove the attributions?


I assumed that somebody from c.l.c++ want to share experiences for the
discussion.
Is there an updated consideration on topics like "What Value Does Const
Correctness Offer" from both groups possible?

Regards,
Markus

Jul 23 '05 #10

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

Similar topics

8
2587
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
2502
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
6
2420
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
7
3802
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above. And why would one choose one over the other. moreover, I thought there was a difference between the two below where one would not let the value change whereas the other one would not let the
3
2235
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
15
4183
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef MY_HEADER #define MY_HEADER const int FOO = 42;
4
2749
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
2788
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1874
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
17
2441
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
0
8888
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
9401
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
9113
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
8097
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
6702
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
6011
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
4519
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...
1
3221
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
2635
muto222
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.