How can a change a const value?

Started by
12 comments, last by zipless 22 years, 2 months ago
Ah OK, casting the value works in debug config but not in release so that idea's gone out of the window. I had assumed that it would work because there is no ROM it's only a section of memory marked as read only, i thought that accessing it through it's address would by-pass and checking. I assume that if i wanted to i could do it using assembler?

I'll use mutatable and i'll probably end up making the member private as well. For the record the class is going to cache a file from disc into memory, pointed to by char *cache, the end of the file will be pointed to by char *chacheend. I definately don't want these values changing noramlly so i though const's would be a good way to go. I'll need to change cacheend if the file is altered, so thats why i wanted to know.

cheers once again guys.

By the way, was i right, could i have used assembler to change the const value? I'd assume so becouse there would be no checking performed, right?



/* Ignorance is bliss, then you go and spoil it by learning stuff */
EDIT: changed a load of grammer and spelling errors, i need sleep

Edited by - zipless on February 17, 2002 9:09:00 AM
/* Ignorance is bliss, then you go and spoil it by learning stuff */
Advertisement
quote:Original post by zipless
By the way, was i right, could i have used assembler to change the const value? I''d assume so becouse there would be no checking performed, right?


If you were hell-bent on changing a const value, sure you could find a way. However, the simple answer is to not declare variables which will change as const. Any other approach just makes life hard for yourself.

--
Very simple ideas lie within the reach only of complex minds.
Just remember that if something is said to invoke undefined behaviour, anything can happen. Even if it worked in release build as well, you were just lucky.

If you have to start playing about with the assembly, then you are writing really non-protable code. You need to ask yourself why you are going to these extremes and find a defined, cross-platform alterative.
The code is going to be used with DirectX stuffs so i''m pretty much stuck with one platform anyway so using assembler won''t really make any difference. Don''t get me wrong though, i''m not thinking about using it i just wondered if i could cos i''m about to re-learn some ASM. I''ll use mutatable once i''ve played about with it for a while.

I know that a lot of people might consider changing the value in a memory address dangerous because there is no type checking etc, theres no safety net, but as long as the code is designed well there shouldn''t be any problems.

I can see why using a pointer with the const was a bad idea though, like i said at the start, i''m not familiar with them and i wasn''t sure how the compiler was supposed to react never mind how it would actually react. At least i won''t be making that mistake again.
/* Ignorance is bliss, then you go and spoil it by learning stuff */

This topic is closed to new replies.

Advertisement