Is "const" more than syntactic sugar?

M

Markus.Elfring

How much effort do you invest to develop a const-correct design?
Did a later addition of the type modifier "const" for a function
interface uncover any real errors and bugs in your source code at
compile time?

Regards,
Markus
 
M

Malcolm

How much effort do you invest to develop a const-correct design?
Did a later addition of the type modifier "const" for a function
interface uncover any real errors and bugs in your source code at
compile time?
I can't say it has for me personally.
For practical purposes const is usually just visual noise. However there is
an argument that when one parameter is input and the other is output, it
helps to tell the calling programmer which is which.
 
J

Jack Klein

How much effort do you invest to develop a const-correct design?
Did a later addition of the type modifier "const" for a function
interface uncover any real errors and bugs in your source code at
compile time?

I spend a lot of effort on const-correct design, although of course it
depends on what you mean by const-correct. Every pointer passed to a
function is const-qualified if the pointed-to object is not to be
modified. On the other hand, I do not bother with const qualification
at all for non-pointer value parameters.

Data that is truly constant is defined const. In small embedded
systems, where I do a lot of my work, non-constant initialized objects
take up space in the non-volatile program storage (ROM, EEPROM,
flash), and additional space in RAM, plus extra time at program start
up as they must be copied from non-volatile storage to RAM.

On higher performance systems, where code is copied from relatively
slow flash to high speed DRAM for execution, and on desk top systems
where code is copied from disk image to RAM, many platforms allow for
constant data to be in a memory area that will generate an exception
if the program tries to write to it. Turns up code defects quickly.
 
P

Peter Pichler

How much effort do you invest to develop a const-correct design?

A lot. Being as paranoid as I am, I assume the paradigm that everything
is const unless there is a specific reason for it not to be.
Did a later addition of the type modifier "const" for a function
interface uncover any real errors and bugs in your source code at
compile time?

Sometimes. Changing a value to const may reveal that a 6th level of
function call somewhere in some other file has a 3rd parameter declared
as not const. These "bugs" are usually easy to fix, though.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top