Thread overview
Newbie questions on memory allocation
Jul 24, 2010
Deokjae Lee
Jul 24, 2010
BCS
Jul 24, 2010
Simen kjaeraas
Jul 24, 2010
bearophile
Jul 24, 2010
Simen kjaeraas
Jul 24, 2010
bearophile
July 24, 2010
Hi there, I have some questions on the following code.

import std.stdio;

struct S {
	int x;
}

void main() {
	int[3] a = new int[3];//A
	S* b = new S();//B
	delete b;//C
}

What's the meaning of the line A?
Is the array allocated on heap? or stack?
Is it dynamic or static?

I think the line B is not a good style in D, but anyway I have a question.
Does the garbage collector concern the object constructed at line B?
Is the line C safe?
July 24, 2010
Hello Deokjae,

> Hi there, I have some questions on the following code.
> 
> import std.stdio;
> 
> struct S {
> int x;
> }
> void main() {
> int[3] a = new int[3];//A
> S* b = new S();//B
> delete b;//C
> }

> What's the meaning of the line A?
> Is the array allocated on heap? or stack?

IITC new give you something on the heap in all cases.

-- 
... <IXOYE><



July 24, 2010
Deokjae Lee <asitdepends@gmail.com> wrote:

> Hi there, I have some questions on the following code.
>
> import std.stdio;
>
> struct S {
> 	int x;
> }
>
> void main() {
> 	int[3] a = new int[3];//A
> 	S* b = new S();//B
> 	delete b;//C
> }
>
> What's the meaning of the line A?

Create a static array on the stack, and initialize it with these values
I just got off the heap.


> Is the array allocated on heap? or stack?

a is allocated on the stack. (new int[3]) is allocated on the heap. (and
is no longer referenced, thus will be collected on the next GC cycle.)


> Is it dynamic or static?

a is static, (new int[3]) is dynamic.


> I think the line B is not a good style in D, but anyway I have a question.
> Does the garbage collector concern the object constructed at line B?

Yes. If you want to allocate stuff that the GC is not to know about, you
need to use std.c.stdlib.malloc.


> Is the line C safe?

I'd say no, but the compiler seems to disagree with me. Delete is
however scheduled for deprecation, so shouldn't be used. (instead, use
clear, which clears the state of the object, and collects it on the
next GC cycle.)

-- 
Simen
July 24, 2010
Deokjae Lee:

> What's the meaning of the line A?

It creates on the stack a 2-word structure, puts unsigned 3 in one word and in the other word puts a pointer to a newly allocated area on the GC-managed heap, that can contain 3 integers (plus one bookkeeping byte), so this heap area is probably 4 words long or longer.


> Is the array allocated on heap? or stack?

GC heap.


> Is it dynamic or static?

Dynamic.


> I think the line B is not a good style in D,

There are many situations where you want to use heap allocated structs.


> Does the garbage collector concern the object constructed at line B?

The struct gets allocated on the heap managed by the GC.


> Is the line C safe?

Kind of. But not with future garbage collectors. So recently Andrei has deprecated the delete statement. Don't use it any more.

Bye,
bearophile
July 24, 2010
bearophile <bearophileHUGS@lycos.com> wrote:

> Deokjae Lee:
>
>> What's the meaning of the line A?
>
> It creates on the stack a 2-word structure, puts unsigned 3 in one word and in the other word puts a pointer to a newly allocated area on the GC-managed heap, that can contain 3 integers (plus one bookkeeping byte), so this heap area is probably 4 words long or longer.

Check the code again. int[3] is stack-allocated. There is a temporary on
the heap, but it is thrown away after being used to initialize a.

-- 
Simen
July 24, 2010
Simen kjaeraas:
> Check the code again. int[3] is stack-allocated. There is a temporary on the heap, but it is thrown away after being used to initialize a.

I didn't see the 3.
I am very sorry Deokjae Lee... -.-'