in D, string literals don't allocate. (in C as well but they decay to pointers
so let's leave these aside from this discussion)
D:
// D20180718T163602
import std.stdio;
extern(C) void fun(const(char)* str){
printf("std:{%s}\n", str);
}
void main(){
string a = "foo";
// a.ptr points to ROM to a block of size 3+1 (last entry is 0, to help
implicit conversion without allocation to C strings)
assert(a.length == 3);
assert(a.ptr[3] == 0);
fun(a.ptr);
auto a2 = new char[](a.length);
a2[]=a;
assert(a.ptr[3] == 0); // probably fails
fun(a2.ptr); // undefined behavior, since it's not null terminated
}
Run
Seems like Nim could use these 2 tricks as well:
* avoiding allocation for var a = "foo" * allocate in ROM an extra byte to
hold `0 for null termination