Through my knowledge, I know that by declaring a variable as 'const', its stored in read only memory and at the same time I found that a const variable's value can be changed using pointers. How is it possible?

Recommended Answers

All 9 Replies

Depends...Where are you declaring this const variable? Is it local, on the read/write stack?

>I know that by declaring a variable as 'const', its stored in read only memory
Incorrect. There's no rule that says const qualified objects must be stored in read-only memory.

>I found that a const variable's value can be changed using pointers.
Maybe, maybe not. Attempting to modify the value of a const qualified object invokes undefined behavior. There's also no rule that says const qualified objects cannot be stored in read-only memory.

Depends...Where are you declaring this const variable? Is it local, on the read/write stack?

Yes, it is a local variable.

>I know that by declaring a variable as 'const', its stored in read only memory
Incorrect. There's no rule that says const qualified objects must be stored in read-only memory.

>I found that a const variable's value can be changed using pointers.
Maybe, maybe not. Attempting to modify the value of a const qualified object invokes undefined behavior. There's also no rule that says const qualified objects cannot be stored in read-only memory.

By undefined behavior, do you mean that sometimes it can be changed and sometimes cannot be?

By undefined behavior, do you mean that sometimes it can be changed and sometimes cannot be?

By undefined, I think she means not define within the C language so the compiler designer(s) are free to implement it any way they want....ie, don't expect consistent implementation.

Well, I can say one one thing that if using const keyword, why try to change it.
By undefined behavior, Narue may be trying to tell you that its state and result cannot be predicted.
Also, it is compiler defined, which can give error message or if it is not defined by compiler ( not implemented in compiler design ) then as you said only God knows the result.
Manoj

May it will help full for you............

#include<conio.h>
#include<stdio.h>
main()
{
#define a 10

printf("\n%d",a);
#undef a
#define a 15
printf("\n%d",a);
getch();
}

>By undefined behavior, do you mean that sometimes it can be changed and sometimes cannot be?
By undefined behavior I mean the C standard makes no guarantees at all about what might happen when your program runs. It's completely unpredictable, even on different runs of the same build. Worse yet, the undefined behavior isn't restricted to that one operation. Once you invoke undefined behavior, your entire program's behavior becomes undefined.

>By undefined behavior, do you mean that sometimes it can be changed and sometimes cannot be?
By undefined behavior I mean the C standard makes no guarantees at all about what might happen when your program runs. It's completely unpredictable, even on different runs of the same build. Worse yet, the undefined behavior isn't restricted to that one operation. Once you invoke undefined behavior, your entire program's behavior becomes undefined.

No, you are more accurate and precice here. Now, C++ compilers are themselves programs, made by humans. One thing that if coder of the compiler has not implemented a features ( standardized or any additional one ) in compiler design, then we can safely say unpredicted for that compiler ( if want to be more precise ).
Just leave that to query's sending, if he want to take pain to learn new/ latest compiler specification or modify his code according to his compiler specification.
I can just give advice not use any unpredictable behaving feature.
All Older C++ Compiler have this problem in one or other way.
-Manoj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.