PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > What is the use of qualifier const
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What is the use of qualifier const

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 04h27   #1
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What is the use of qualifier const

It seems I never need to use it. It only made a long line longer, and
made you type five more characters. I feel so strange why people are
talking about it.
  Réponse avec citation
Vieux 12/04/2008, 04h33   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

istillshine@gmail.com wrote:
> It seems I never need to use it. It only made a long line longer, and
> made you type five more characters. I feel so strange why people are
> talking about it.


Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

--
Ian Collins.
  Réponse avec citation
Vieux 12/04/2008, 04h41   #3
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.com> wrote:

> Don't you ever create read only variables or string literals or pass
> pointers to read only data to functions?


I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.

Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?

I could not under why people do that. When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.
  Réponse avec citation
Vieux 12/04/2008, 04h49   #4
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

istillshine@gmail.com wrote:
> On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>> Don't you ever create read only variables or string literals or pass
>> pointers to read only data to functions?

>
> I do. But I never used const. I just took care not to modify them. In
> fact, I never needed to modify them.
>

Then you enjoy skating on thin ice, or never make mistakes.

> Who will put a qualifier before something and modify this thing
> later? And if he dose not modify this thing later, why dose he put a
> qualifier before it?
>

So the compiler will tell him if he accidentally does attempt to modify it.

> I could not under why people do that.


Because they want to write safe, well behaved programs. They want their
constant data stored somewhere it can't be modified. Many embedded
systems have more FLASH than RAM, so it makes sense to store constant
data in FLASH.

> When I saw some old C code, I
> found people never used const. Their programs are still robust enough
> without const.


That's because cost was introduced later. It wouldn't have been
introduced if there wasn't some thing to fix or a perceived need to
differentiate between constant and volatile data.

--
Ian Collins.
  Réponse avec citation
Vieux 12/04/2008, 07h21   #5
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

On Apr 11, 8:41pm, istillsh...@gmail.com wrote:
> On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
> > Don't you ever create read only variables or string literals or pass
> > pointers to read only data to functions?

>
> I do. But I never used const. I just took care not to modify them. In
> fact, I never needed to modify them.


It's a great way to do it. Just like grenades. Don't bother with
putting pins in them, just hold the handles down and they are safe.

> Who will put a qualifier before something and modify this thing
> later?


The second programmer, who never saw the qualifier put there.

> And if he dose not modify this thing later, why dose he put a
> qualifier before it?


To give him an error message to tell him he did something stupid.

> I could not under why people do that. When I saw some old C code, I
> found people never used const. Their programs are still robust enough
> without const.


If you substitute the word "fragile" with "robust" then I can agree
with you.
Old code often won't even compile with modern compilers, and the older
it is the less likely it will compile.
If it does succeed in compiling, chances are good it won't run.
If it does run, it is likely to be packed with exploits like gets()
because old time programmers did not have to deal with a hostile
audience.
Old numerical stuff is sometimes villianously bad.
  Réponse avec citation
Vieux 12/04/2008, 14h04   #6
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

istillshine@gmail.com wrote:
> On Apr 11, 11:33 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>> Don't you ever create read only variables or string literals or pass
>> pointers to read only data to functions?

>
> I do. But I never used const. I just took care not to modify them. In
> fact, I never needed to modify them.
>
> Who will put a qualifier before something and modify this thing
> later? And if he dose not modify this thing later, why dose he put a
> qualifier before it?


Accidents happen. It's unlikely that a programmer who
doesn't intend to modify something will write `*ptr = 42',
but he may well write foo(ptr) without realizing the foo
makes a modification. The prototypical example of this is
perhaps strtok(ptr, " \t\n"), forgetting for a moment that
strtok() might modify what its first argument points at.

Or perhaps at the time the programmer writes foo(ptr)
all is well, because foo() doesn't touch anything. But in
the run-up to the 1.2 release, some other programmer changes
foo() without being aware of its "contract," and ...

If the programmers who work on foo() and on its callers
are scrupulous about their use of `const', errors of this
kind get caught during compilation instead of during the
Big Demo For The Customer.

> I could not under why people do that. When I saw some old C code, I
> found people never used const. Their programs are still robust enough
> without const.


Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 12/04/2008, 14h16   #7
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:

> Those old programs were also robust enough without `void'
> and without function prototypes, so what's your point?


I think void and function prototypes are useful additions. But for
const, I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
  Réponse avec citation
Vieux 12/04/2008, 15h40   #8
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

istillshine@gmail.com wrote:
> On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>
>> Those old programs were also robust enough without `void'
>> and without function prototypes, so what's your point?

>
> I think void and function prototypes are useful additions. But for
> const, I rarely use it largely because I don't want to type five
> extra characters,
> 'c', 'o', 'n', 's', and 't'. Between pounding five more characters
> and taking care not to modify the constant variable, I prefer the
> latter.


The first time you made this mistake I let it pass as
trivial, but now that you've made it three times: Your
keystroke count is wrong. (So are your priorities.)

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 13/04/2008, 02h14   #9
Michael
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

On Sat, 12 Apr 2008 06:16:55 -0700, istillshine wrote:

> On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>
>> Those old programs were also robust enough without `void'
>> and without function prototypes, so what's your point?

>
> I think void and function prototypes are useful additions. But for
> const, I rarely use it largely because I don't want to type five
> extra characters,
> 'c', 'o', 'n', 's', and 't'. Between pounding five more characters
> and taking care not to modify the constant variable, I prefer the
> latter.


If you program alone and your code will never be used by anyone but
you then you may have a point. Not a good point but a point.

However if your code is ever used by others then not using const
will cause unnecessary difficulties for these others programmers.

Also if you yourself ever find it necessary to come back 6 months
or a year later to maintain your own code you may find that you
wish you had typed those characters a few times.

Regards
Mike
  Réponse avec citation
Vieux 14/04/2008, 15h24   #10
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is the use of qualifier const

On 12 Apr, 14:16, istillsh...@gmail.com wrote:
> On Apr 12, 9:04 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:


> > Those old programs were also robust enough without `void'
> > and without function prototypes, so what's your point?

>
> I think void and function prototypes are useful additions. But for
> const, I rarely use it largely because I don't want to type five
> extra characters,
> 'c', 'o', 'n', 's', and 't'. Between pounding five more characters
> and taking care not to modify the constant variable, I prefer the
> latter.


http://en.wikipedia.org/wiki/APL_(programming_language)

--
Nick Keighley
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16669 seconds with 18 queries