Reputation: 13
I have a mini C++ program as below.
#include <iostream>
using namespace std;
void main() {
char* userInput = new char[7];
userInput = "Hello";
*(userInput + 6) = 'W';
cout << userInput << "\n";
}
I wished, throughout such program, to confirm that the 'W' will not be outputted, since a NULL character is right after "Hello". However, it shows the following run-time error.
Unhandled exception thrown: write access violation. userInput was 0xBD9BD8. occurred
Any advice? Thanks.
Upvotes: 1
Views: 698
Reputation: 4972
When you do userInput = "Hello";
you actually reassign your variable userInput
to the string "Hello"
.
But the string "Hello"
is not copied within your buffer new char[7]
, variable userInput
is reassigned.
So your buffer stays untouched.
Because string literals (strings written in your code with quotes "
) are stored in a read-only portion of the program, you cannot rewrite any character of them: this is why you have your error.
And here, worst: you are trying to write out of bound of the string in the read-only memory. This is not the cause of the error (as stated by some others) because there are other padding data there, the cause being the region is write protected.
Use a function such as strcpy()
to perform a copy from a string to another buffer.
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char* userInput = new char[7];
strcpy(userInput, "Hello");
*(userInput + 6) = 'W';
cout << userInput << "\n";
}
Upvotes: 1
Reputation: 311078
There are several problems with the program.
For starters there is a memory leak.
char* userInput = new char[7];
userInput = "Hello";
at first the pointer userInput
is initialized by the address of the allocated memory extent and then the pointer is reassigned with the address of the first character of the string literal "Hello"
.
Though the program tries to write in the memory beyond the string literal nevertheless it should be mentioned that according to the C++ Standard (2.13.5 String literals)
16 Evaluating a string-literal results in a string literal object with static storage duration, initialized from the given characters as specified above. Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined. —end note ]
To demonstrate your concept there is no need to allocate the memory dynamically. You could just write
char userInput[7] = "Hello";
*(userInput + 6) = 'W';
Or using dynamic memory allocation you could write
#include <cstring>
//...
char* userInput = new char[7];
std::strcpy( userInput, "Hello" );
*(userInput + 6) = 'W';
//...
delete [] userInput;
Pay attention to that though some compilers (as far as I know the MS VC++ allows to do this) allows to declare the function main as having the return type void nevertheless according to the C++ Standard the function main shall have the return type int.
int main()
Upvotes: 0
Reputation: 637
When you write that :
userInput = "Hello";
the pointer userInput
will be assigned point to the "Hello" string, which is 6 bytes long, the 5 character + a null terminator. ( and making your new char[7]
useless, by the way )
And here :
*(userInput + 6) = 'W';
You write AFTER the null terminator, in memory which have not been allocated. What happen when you read/write in not allocated memory is undefined behavior, it can for example result in a seg fault, which you had
Upvotes: 0