Do strings with enum allocate at usage point?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 17 11:25:00 PDT 2015
On 03/17/2015 11:21 AM, "岩倉 澪" wrote:
> I often hear it advised to avoid using enum with arrays because they
> will allocate at the usage point, but does this also apply to strings?
> strings are arrays, so naively it seems that they would, but that seems
> odd to me.
> I would imagine string literals end up in the data segment as they are
> immutable.
>
> As a continuation of this question, I know that string literals have an
> implicit null delimiter, so it should be correct to pass a "literal".ptr
> to a function taking a C-string, and presumably this still applies when
> using enum.
> However, if enum implies allocation at the usage point for strings, one
> would be better served with static, meaning one would need to be
> explicit: static foo = "bar\0"?
Strings are fine and fortunately it is very easy to test:
enum arr = [ 1, 2 ];
enum s = "hello";
void main()
{
assert(arr.ptr !is arr.ptr);
assert(s.ptr is s.ptr);
}
Ali
More information about the Digitalmars-d-learn
mailing list