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.
Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 11 is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Hi, I want to confirm: in the following loop, is the array literal void main() { int[2] a; int i; a = [i, i+1]; // array literal } } Thanks. |
October 10 Re: is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | 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 |
October 11 Re: is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | 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 void main() { int[2] a; int i; a = [i, i+1]; // array literal } } 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
You can run this, and it'll print a different address each time If you add
Otherwise you'd get: 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 |
October 11 Re: is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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 |
October 11 Re: is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | 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 void main() { int[2] a; int i; a = [i, i+1]; // array literal } } Thanks. profile=gc |
October 12 Re: is the array literal in a loop stack or heap allocated? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 10/10/23 10:54 PM, mw wrote: >Hi, I want to confirm: in the following loop, is the array literal ask the compiler:
-Steve |