Is there any way to declare array that has unknown size? C allows this:
struct foobar {
int len;
char data[];
};
In system.nim there is a definition:
NimStringDesc {.compilerproc, final.} = object of TGenericSeq
data: array[0..100_000_000, char]
I found this problematic when compiling Nimrod generated code with Emscripten (btw "hello world" works with --gc:none, --cpu:i386 and small tweak to system.nim) - it ran out of memory during translation.
I replaced 100_000_000 with 1 and it appears to work. I think that both values are equally undefined according to C89/C99 standard. Ideal solution would involve creating flexiblearray[T] type, but it seems that it would be hard to integrate into current C generator, because you can't typedef flexible array:
typedef int TY123[]; // error
FlexibleArray type would also be useful when writing low-level code, as it could be a substitute for pointer arithmetic. What do you think of that? Is replacing 100_000_000 with 1 acceptable, or would it introduce subtle bugs?
Yeah, there is a feature request for array [char] {.unchecked.}, and the current way also doesn't work well with debuggers. And no, it's not hard to integrate into the current C code generator. ;-)
Is replacing 100_000_000 with 1 acceptable?
I think so, but it's better to really fix it.
The compiler now supports unchecked. It only works with named types, but surely that's good enough for now. system.nim has been changed to use it:
# 'nimunion' is defined when the compiler supports the new 'union' and 'unchecked' pragmas.
# If it is not defined we define it to the empty pragma here, so system.nim keeps working with old compilers.
when not defined(nimunion):
{.pragma: unchecked.}
type
UncheckedCharArray {.unchecked.} = array[0..100_000_000, char]
# len and space without counting the terminating zero:
NimStringDesc {.compilerproc, final.} = object of TGenericSeq
data: UncheckedCharArray