Reputation: 215
Is there any way to force gcc to put
char* str = "Hello";
not in the .rodata without change this statement in
char str[] = "Hello!";
?
Ok, so better way to do this is modify the statement to char str[]
. Thanks to all.
Upvotes: 0
Views: 1923
Reputation: 88711
How about using strdup if your platform has it, or implementing it yourself if it doesn't?
char *str = strdup("hello world");
This will allocate memory (at runtime) and copy the string literal into an appropriately sized chunk of memory which you can quite legitimately write to and modify later.
Don't forget to free()
after use though.
You might be able to force GCC to put somethings in specific sections of your choosing using the __attribute__ ((section ("my_section")))
attribute, but you'll still have to modify the original source to do that so you'd be much better off doing it a "normal" way.
Upvotes: 0
Reputation: 263058
Why? Trying to change string literals leads to undefined behavior. It's evil. Consider this program:
"hello"[0] = 'y'; // Welcome to Undefined Behavior Land. Enjoy yor stay!
std::cout << "hello" << std::endl; // Could very will print "yello" now!
Upvotes: 4