Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const problem

Reply
Thread Tools

const problem

 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-17-2008
On Tue, 15 Apr 2008 10:44:31 +0500, arnuld <(E-Mail Removed)>
wrote:


snip

>so, in case of #define or enum:
>
> a C compiler can replace a #define/enum with its value
> (at compile time), and can use that value (which
> it knows) where a compile time constant is required.


As far as #define is concerned, after the preprocessor gets done
substituting everything, the compiler will never see the #define name,
only the substituted value. Therefore, the compiler *must* use the
substituted value. If the substituted token is a self defining term,
then it can be used where a compile time constant is required.


Remove del for email
 
Reply With Quote
 
 
 
 
David Thompson
Guest
Posts: n/a
 
      04-28-2008
On Mon, 14 Apr 2008 09:04:49 -0700, Keith Thompson <(E-Mail Removed)>
wrote:
<snip>
> I don't think pointers would be a problem. For example, C++ did
> change the semantics of const. In essence, if an object is declared


including a 'static' (classwide) data member

> "const" and its initializer is a constant expression, then any


and of an integer or enumeration type (in C++ enum types are distinct,
not just aliases for integer types as they are in C)

> reference to the object is a constant expression. This allows <snip>


But most of the places C (or C++) requires a constant expression do
require integer (constant expression), including the example you gave
of a case label, and this feature is enough for those.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-28-2008
David Thompson wrote:
> On Mon, 14 Apr 2008 09:04:49 -0700, Keith Thompson <(E-Mail Removed)>
> wrote:
> <snip>
>> I don't think pointers would be a problem. For example, C++ did
>> change the semantics of const. In essence, if an object is declared

>
> including a 'static' (classwide) data member
>
>> "const" and its initializer is a constant expression, then any

>
> and of an integer or enumeration type (in C++ enum types are distinct,
> not just aliases for integer types as they are in C)
>
>> reference to the object is a constant expression. This allows <snip>

>
> But most of the places C (or C++) requires a constant expression do
> require integer (constant expression), including the example you gave
> of a case label, and this feature is enough for those.
>

Which feature?

--
Ian Collins.
 
Reply With Quote
 
pereges
Guest
Posts: n/a
 
      04-28-2008
I have always found that using const is only a choice rather than a
necessity. But this is just my opinion, I'm sure the experts here will
differ.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-28-2008
pereges wrote:

> I have always found that using const is only a choice rather than a
> necessity.


Yes. Most things in life are just choices.

> But this is just my opinion, I'm sure the experts here will
> differ.


The point of const (in C) is to help the compiler to do more checks on
your source. Consider this trivial program:

#include <stdio.h>

int main(void) {

long double pi = 3.1415926535897932384626433832795029L;
long double r;

puts("Enter radius of circle:");
scanf("%Lf", &r);
printf("Area of the circle is: %Lf\n", pi * (r * r));
return 0;
}

Now imagine this as a part of a much larger program or function.
Obviously 'pi' holds the value of a constant and should not be changed.
However if some other code in your program happens to modify 'pi' your
compiler will silently accept the modification and your program will
start giving you wrong or inaccurate results. This is where
qualifying 'pi' with const will start paying off. Now anywhere a direct
change is made to 'pi' the compiler will give you a diagnostic warning
you of it, so you can correct the code and prevent undefined behaviour.

As someone else said, const may appear rather meritless for small,
one-man programs, but it will start paying dividends when the program
grows larger and is developed by a team, even in spite the quirkiness
of C's const.

PS. You can still attempt to write to a const object and evade compiler
warnings by writing through a pointer, but that's far less likely to
happen in real code than direct in-advertant modification.

 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      05-12-2008
On Mon, 28 Apr 2008 15:52:17 +1200, Ian Collins <(E-Mail Removed)>
wrote:

> David Thompson wrote:
> > On Mon, 14 Apr 2008 09:04:49 -0700, Keith Thompson <(E-Mail Removed)>
> > wrote:
> > <snip>
> >> I don't think pointers would be a problem. For example, C++ did
> >> change the semantics of const. In essence, if an object is declared

> >
> > including a 'static' (classwide) data member
> >
> >> "const" and its initializer is a constant expression, then any

> >
> > and of an integer or enumeration type (in C++ enum types are distinct,
> > not just aliases for integer types as they are in C)
> >
> >> reference to the object is a constant expression. This allows <snip>

> >
> > But most of the places C (or C++) requires a constant expression do
> > require integer (constant expression), including the example you gave
> > of a case label, and this feature is enough for those.
> >

> Which feature?


The feature of C++ that Keith described and I slightly corrected, that
a const variable(!) of integer or enum type initialized by a constant
expression is acceptable in a constant expression. (And the which, or
something like it, the upthread discussion suggested for C.)

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments