1

I made a function which change string, see the following code.

void Test(char* str, char c) {
    str[1] = c;
}

int main(){
    Test("Hi", '2');
}

I notice it made some run time error. I know how to prevent the error.

char buff[3] = "Hi";
Test(buff,'2');

but I don't know why the first example made run time error. I guess, if I pass string directly, it becomes const char. Does anyone explain what happened exactly?

ps. what if I use char* str = "hi", then pass it into the argument?

char* buff = "Hi";
Test(buff,'2');

like this. Can I modify buff?

5
  • Read something about const char*. Commented Jan 15, 2014 at 8:04
  • This isn't legal C++ any more (as in shouldn't compile) and has never been right. Commented Jan 15, 2014 at 8:07
  • The last one being new, I presume it's supposed to be char *buff, but still no. Using a pointer to non-constant data to point to constant data will never work regardless of how you try to get around it. Commented Jan 15, 2014 at 8:12
  • See my edit about char* str = "hi". Commented Jan 15, 2014 at 8:15
  • sorry, chris. i'd like to write char* buff. Commented Jan 15, 2014 at 8:15

5 Answers 5

3

Because "Hi" is string literal and it's not allowed to be modified, they are read-only (the type of string literal is const char[n]).

Modifying it is undefined behavior.


Regarding your edit: char* str = "hi" is invalid, it should be const char* str = "hi". Which is pointer to const char. Again, modifying it is disallowed.

Sign up to request clarification or add additional context in comments.

11 Comments

True if he had declared char* buff = "Hi" (which would place buff in the read-only code section instead of in the stack).
@barak OP did (implicitly) declare it like that. This answer is correct.
Note: const char[N], though.
@barakmanos; you need to double check the question, it's the first case which is giving him problems
@Kiril Kirov: Try for yourself: When char buff[3] = "Hi", buff is placed in the stack of the calling function, or in the data-section of the program (if declared globally). Both the stack and the data-section are Read-Write sections. When 'const char buff[3] = "Hi"` or char* buff = "Hi", `buff is placed in the code-section of the program, which is a Read-Only section.
|
1

When you don't explicitly allocate memory for strings, compiler stores them in read-only memory. So, any modification to such strings result in run time error.

Test("Hi", '2');  

Here in the above case "Hi" string is stored in read-only memory.

 char *buff = "Hi";  
 Test(buff,'2');

Here also "Hi" is stored in the read-only memory and the starting address is returned to buff character pointer, which is same as above. You can overcome such errors by allocating memory for the string and then pass that reference. Like

 char buff[3] = "Hi";  
 Test(buff,'2');

or

char *buff = (char *)malloc(SIZE);  
strcpy(buff, "Hi");
Test(buff,'2');

Please refer to this link http://www.geeksforgeeks.org/memory-layout-of-c-program/

Comments

0

Often string constants are in read-only memory, causing a runtime error when you attempt to modify it.

In your second example, you put the string into a buffer on the stack, so it can be updated without error.

Comments

0

Literal strings are not modifiable. When I compile your code with GCC I get the warning:

testptr.cpp:6: warning: deprecated conversion from string constant to 'char*'

Comments

0

Runtime Error:

char* buff = "Hi"; // buff points to an address in the code-section, which is a Read-Only section
buff[1] = 'x';     // Illegal memory access violation

Compilation Error:

const char* buff = "Hi"; // This is a correct declaration, which will prevent the runtime error above
buff[1] = 'x';           // The compiler will not allow this

All Good:

char buff[] = "Hi"; // buff points to an address in the stack or the data-section, which are both Read-Write sections
buff[1] = 'x';      // Works OK

Notes:

  1. In all cases, a string "Hi" is placed in the code-section of the program.

  2. In the last example, the contents of that string are copied into the buff array.

  3. In the last example, the buff array is located in the stack if buff is a non-static local variable, and in the data-section of the program otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.