What is the meaning of static arrays

I don't have a background in C or C ++, so static arrays puzzled me a bit. What are they for? Why are they allocated on the stack?

I imagine a performance advantage. Stacking is faster and there is no need for garbage collection. But why should the length be known at compile time? Could you create an array of a fixed size at runtime and push it onto the stack?

Dynamic arays or slices in D are represented by a structure containing a pointer and a length property. Is the same true for static arrays? How are they presented?

If you pass them functions, they will be copied completely (if you are not using ref), what is the reason for this?

I understand that dynamic arrays and fragments are much more significant in D than static arrays, so the documentation doesn’t dwell on them very long, but I still would like to get a little more background. I believe that the features of static arrays are related to how stack distribution works.

+4
source share
2 answers

static arrays are welcomed in C, where calls to (slow) alloc were diverted to the extreme due to memory leaks (when you forgot to free the allocated array), double free (as a result ...), dangling pointers (all the dangers of manual memory management that can be avoided with GC)

this meant that constructions such as

 int foo(char* inp){ char[80] buff; strcpy(inp,buff);//don't do this this is a invite for a buffer overflow //... return 1; } 

were distributed instead of alloc / free calls, where you need to make sure that everything you allocated was released exactly once during the program

technically, you CAN dynamically allocate on the stack (using the assembly, if you want), however, this can cause some problems with the code, since the length will be known only at runtime and will reduce the possible optimization that the compiler can apply (expand iteration over it, for example)

static arrays are mainly used for buffers due to fast stack allocation

 ubyte[1024] buff=void;//assigning void avoids the initializer for each element cause we are writing to it first thing anyway ubyte[] b; while((b=f.rawRead(buff[])).length>0){ //... } 

they can implicitly convert to an array slice (or explicitly using the slice [] operator), so you can use them almost interchangeably with normal dynamic arrays

+6
source

Static value arrays in D2. Without static arrays, there would be no easy way to have 100 elements in a structure that are actually stored in the structure.

Static arrays carry their size as part of their type. This allows you, for example, to declare: alias ubyte[16] IPv6Address;

Unlike C, D2, static arrays are value types through and. This means that they are passed by value to functions such as structures. Static arrays usually behave like structures with N members as memory is allocated and copied.

By the way, you can use alloca to allocate a variable amount of memory on the stack. C also has variable length arrays .

+5
source

Source: https://habr.com/ru/post/1395990/


All Articles