Re: UB ??
From: Alwyn (alwyn_at_blueyonder.co.uk)
Date: 02/17/05
- Next message: Clark S. Cox III: "Re: UB ??"
- Previous message: Martijn Mulder: "Re: UB ??"
- In reply to: ranjeet: "UB ??"
- Next in thread: Clark S. Cox III: "Re: UB ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 17 Feb 2005 13:01:53 +0000
On Thu, 17 Feb 2005 03:58:00 -0800, ranjeet wrote:
>
> Below are my some observation which did not follwed my excpectation
> as it was stated in the FAQS,
>
>
> int main () {
>
> const int i = 2; // Means it is not writable i.e it is in Read only memory.
> int *i_ptr; // Declare the poiter of int type.
> i_ptr = &i; // Assingning the address of the i.
My C compiler gives a warning for this. Didn't you get one?
If you really need to do this, you should use a cast.
> *i_ptr = 3;
It is illegal to alter a 'const' variable. If you declare something
'const', you are promising that you won't change it. Unfortunately, a
compiler is not always able to enforce this rule.
> printf("%u, %u, %d, %d", &i, ptr, i, *ptr);
<snip>
> Now how this is possible that same address is having the two diffrent
> values ?? at the same time.
If you do something illegal, as above, your program is not guaranteed to
work as expected. It's what 'undefined behaviour' means. There is a lot of
undefined behaviour in C that the compiler cannot prevent. Then the user
has to do his or her best to keep to the rules, which is not always easy.
Alwyn
- Next message: Clark S. Cox III: "Re: UB ??"
- Previous message: Martijn Mulder: "Re: UB ??"
- In reply to: ranjeet: "UB ??"
- Next in thread: Clark S. Cox III: "Re: UB ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|