www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is the array literal in a loop stack or heap allocated?

reply mw <mw g.c> writes:
Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?

void main() {

int[2] a;
int[] b;

int i;
While(++i <=100) {

   a = [i, i+1];  // array literal
   b = [i, i+1];

}

}


Thanks.
Oct 10 2023
next sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Wed, Oct 11, 2023 at 02:54:53AM +0000, mw via Digitalmars-d-learn wrote:
 Hi,
 
 I want to confirm: in the following loop, is the array literal `a` vs.
 `b` stack or heap allocated? and how many times?
 
 void main() {
 
 int[2] a;
This is stack-allocated. Once per call to the function.
 int[] b;
This is an empty slice. It can refer to either stack or heap memory, depending on what's assigned to it.
 int i;
 While(++i <=100) {
 
   a = [i, i+1];  // array literal
`a` is overwritten in-place once per loop.
   b = [i, i+1];
[...] A new array consisting of 2 elements is allocated, once per loop, and assigned to b each time. Any arrays from previous iterations will be collected by the GC eventually. T -- They pretend to pay us, and we pretend to work. -- Russian saying
Oct 10 2023
parent mw <mw g.c> writes:
On Wednesday, 11 October 2023 at 03:15:30 UTC, H. S. Teoh wrote:
 On Wed, Oct 11, 2023 at 02:54:53AM +0000, mw via 
 Digitalmars-d-learn wrote:
 Hi,
 
 I want to confirm: in the following loop, is the array literal 
 `a` vs. `b` stack or heap allocated? and how many times?
 
 void main() {
 
 int[2] a;
This is stack-allocated. Once per call to the function.
 int[] b;
This is an empty slice. It can refer to either stack or heap memory, depending on what's assigned to it.
 int i;
 While(++i <=100) {
 
   a = [i, i+1];  // array literal
`a` is overwritten in-place once per loop.
How about the temporary array literal on the right hand side? It's stack / heap allocated? Or it's not in the language specification, but up to the (optimizing) compiler to decide?
   b = [i, i+1];
[...] A new array consisting of 2 elements is allocated, once per loop, and assigned to b each time. Any arrays from previous iterations will be collected by the GC eventually. T
Oct 10 2023
prev sibling next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote:
 Hi,

 I want to confirm: in the following loop, is the array literal 
 `a` vs. `b` stack or heap allocated? and how many times?

 void main() {

 int[2] a;
 int[] b;

 int i;
 While(++i <=100) {

   a = [i, i+1];  // array literal
   b = [i, i+1];

 }

 }


 Thanks.
a is a static array, therefore it won't allocate any, it's a memcpy b will be heap allocated, and it'll do an allocate at each iteration ```D void test_b() { int[] a; int i; while (++i <= 100) { a = [i, i + 1]; printf("%p\n", a.ptr); } } ``` You can run this, and it'll print a different address each time If you add `[]` it'll do range based copy (memcpy), but since the array is not initialized, it has a length of 0, so you only need to allocate once (either with GC or with malloc) ```D void test_b() { int[] a; int i; a.length = 2; // initialize the heap allocated array here // or with malloc: // auto ptr = malloc(int.sizeof * 2); // a = cast(int[]) ptr[0 .. int.sizeof * 2]; while (++i <= 100) { a[] = [i, i + 1]; printf("%p\n", a.ptr); } } ``` Otherwise you'd get: ``core.exception.RangeError onlineapp.d(18): Range violation`` I don't use D with the GC, so my memory about it is probably foggy, but i'm pretty sure what i said is right, please anyone correct me if i'm wrong
Oct 10 2023
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote:
 Hi,

 I want to confirm: in the following loop, is the array literal 
 `a` vs. `b` stack or heap allocated? and how many times?

 void main() {

 int[2] a;
 int[] b;

 int i;
 While(++i <=100) {

   a = [i, i+1];  // array literal
   b = [i, i+1];

 }

 }


 Thanks.
profile=gc
Oct 10 2023
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/10/23 10:54 PM, mw wrote:
 Hi,
 
 I want to confirm: in the following loop, is the array literal `a` vs. 
 `b` stack or heap allocated? and how many times?
ask the compiler: ```d void main() nogc { int[2] a; int[] b; int i; while(++i <=100) { a = [i, i+1]; // array literal //b = [i, i+1]; // yes, this allocates, had to comment it out } } ``` -Steve
Oct 12 2023