Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > constant datatypes

Reply
Thread Tools

constant datatypes

 
 
mallikaarjuna24@gmail.com
Guest
Posts: n/a
 
      08-19-2007
why my const integer is strangely accepting an assignment statement or
an increment operation on it just with a warning ??!!
It should restrict me from doing that.. isn't it??

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      08-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> why my const integer is strangely accepting an assignment statement or
> an increment operation on it just with a warning ??!!
> It should restrict me from doing that.. isn't it??


Not in C. In C, the result of attempting to modify const qualified objects
is implementation dependent. IOW, don't do it.

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-19-2007
santosh wrote:
>
> (E-Mail Removed) wrote:
>
> > why my const integer is strangely
> > accepting an assignment statement or
> > an increment operation on it just with a warning ??!!
> > It should restrict me from doing that.. isn't it??

>
> Not in C. In C, the result of attempting to modify
> const qualified objects is implementation dependent.


No, it's undefined, rather than implementation dependent.
A correct C program may exhibit implementation defined behavior.
A correct C program may *not* contain undefined behavior.

N869
6.7.3 Type qualifiers

[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.

> IOW, don't do it.


--
pete
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-19-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> why my const integer is strangely accepting an assignment statement or
> an increment operation on it just with a warning ??!!
> It should restrict me from doing that.. isn't it??


Next time, please post a small program that exhibits the problem,
along with the warning message your compiler produces.
(Copy-and-paste both the program and the message; don't attempt to
re-type them.)

There are various ways you can bypass const qualification and
*attempt* to assign a value to a const-qualified object (for example,
by using a pointer cast). Any attempt to do so invokes undefined
behavior; the compiler is not required (and typically is not able) to
diagnose the problem, but any arbitrarily bad thing can happen when
you execute the code. In the best case, your program immediately
crashes. In the worst case, it silently "works". In the *really*
worst case, it works at first, but fails catastrophically at the most
inconvenient possible moment. So don't do that.

But since you mentioned a warning, that's probably not what happened
in your case. Somebody posted a possible example:

int main(void) {
const int i;
i = 0;
}

The assignment is invalid. This is an example of a class of errors
that the standard calls "constraint violations". The standard
requires a compiler to issue a "diagnostic message", but it says very
little about what a "diagnostic message" is. In particular, the
compiler is not required to reject your program; once it's issued a
diagnostic message, it's done its job. (It's also allowed to issue
diagnostic messages for things that aren't constraint violations.)

The lesson: Don't ignore warnings.

And if you had posted your actual code, we wouldn't have had to guess,
and our responses could probably have been shorter, less numerous, and
more to the point.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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
constant datatypes mallikaarjuna24@gmail.com C Programming 2 08-19-2007 04:43 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
Making Datatypes Constant and Emulating Const Correctness Veli-Pekka Tštilš Perl Misc 2 08-27-2005 08:04 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments