[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: RES: C++ Typecast - so far newbie question
- From: Rici Lake <lua@...>
- Date: Mon, 2 Jul 2007 16:21:08 -0500
On 2-Jul-07, at 4:08 PM, Patrick Donnelly wrote:
> On 2-Jul-07, at 2:40 PM, SosCpdTerra wrote:
>
> > Did someone know a better way to typecast a const char * (that is
about
> > lua_tostring(L, n)) into a char * then vector or c_str()? I'm
fighting
> > with
> > that...
>
> Strings returned by lua_tostring *must not be modified*.
>
> So don't cast them to char* or evil things will happen.
I'm curious about the technical reasons/consequences for this.
Naturally lua strings are meant to be immutable. I'm betting this has
something to do with their hash value changing, and thus their index
to the global environment (or whatever table they're in) being lost
because the hash changes? I assume this would cause not only losing
your string in the table but some segmentation faults if one attempts
to access them? Would the garbage collector lose sight of these
strings, or collect it almost immediately?
Lua interns strings; there is only one instance of each string. So if
you changed, say, "print" to "prbnt", you'd end up changing every usage
of "print" in the lua environment, including constants in programs and
keys in hash tables.
After that, it gets really confusing. But it won't segfault afaik.