Thread: constant strings

  1. #1
    Anon.
    Guest

    constant strings

    I know there is a potential problem with doing the following due to read only memory:
    Code:
    char * szString = "Test";
    szString[2] = 'd';
    What about the following?:
    Code:
    void Func(char * szString) {
       szString[2] = 'd'
    }
    
    //elsewhere
    char * szString = "Test";
    Func(szString);
    Func("Test");
    Does the compiler make sure the string is not put into read only memory in this scenario or do I have to copy the string in the function. This works without problems at the moment but I thought I would check with the experts.

    Thanks!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's still a problem. You need to make sure your program writes to memory it's allowed to, the compiler won't do that for you in this case.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to declare constant strings in a class?
    By bling in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2008, 05:56 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM