Change value of a CONST 
Author Message
 Change value of a CONST

I tried to change the value of a CONST char string variable and got an
Illegal Operation while the program was running.  It compiled but did
not work.  Any suggestions?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 25 Feb 2003 21:44:13 GMT  
 Change value of a CONST

Greets,

    String literals, while they can be legally assigned to char* objects,
should really be treated as char const* values.  (It's basically there for
backward 'C' compatibility from my understanding.)  It may be due to
optimizations, however, it appears that VC++ puts the string literal pool
into memory whose pages are protected from modification and are read only.
An access violation will occur modifying a string literal through a pointer
to such.  If you really wish to modify the string, make a copy of said
string into your own buffer and work with it there.  You can do some arcane
tricks to modify the string literal (and I would strongly recommend that one
does *not* do such) using VirtualProtect(), et al.

Regards,

Joe


Quote:
> I tried to change the value of a CONST char string variable and got an
> Illegal Operation while the program was running.  It compiled but did
> not work.  Any suggestions?

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 25 Feb 2003 21:58:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. changing the value of const

2. Help: inline char const* const& max(char const* const &a, char const* const &b)

3. Is const for value references obsolete in mc++?

4. Problems assigning const fields in a class to an initial value

5. return value of strlen / const size_t

6. const struct ** x = (initial value);

7. Q: Assigning values to const struct*

8. Exporting const values

9. Exporting Const value to .tli/tlh files with #import directive

10. MS compiler seems to mal function/setting const value to template function

11. const declared char changed

12. legally CHANGE a const?

 

 
Powered by phpBB® Forum Software