Jump to page: 1 2 3
Thread overview
D newb
Aug 17, 2008
Tim H
Aug 17, 2008
BCS
Aug 17, 2008
Tim H
Aug 17, 2008
BCS
Aug 17, 2008
Robert Fraser
Aug 18, 2008
bearophile
Aug 18, 2008
JAnderson
Aug 18, 2008
bearophile
Aug 19, 2008
JAnderson
Aug 19, 2008
JAnderson
Aug 18, 2008
JAnderson
Aug 18, 2008
bearophile
Aug 18, 2008
Tim H
Aug 18, 2008
bearophile
Aug 18, 2008
Lutger
Aug 18, 2008
BLS
Aug 18, 2008
Jesse Phillips
August 17, 2008
Hi, I am a D newbie.  Why does this program segfault?

import std.stdio;
import std.c.stdlib;

class Foo
{
        public int m_int = 0;
}

int
main(string[] args)
{
        Foo f;
        printf("foo %d\n", f.m_int);
        return EXIT_SUCCESS;
}

August 17, 2008
Reply to Tim,

> Hi, I am a D newbie.  Why does this program segfault?
> 
> import std.stdio;
> import std.c.stdlib;
> class Foo
> {
> public int m_int = 0;
> }
> int
> main(string[] args)
> {
> Foo f;
> printf("foo %d\n", f.m_int);
> return EXIT_SUCCESS;
> }

The default value for any object Reference (f in this case) is null. create a new object to use (Foo f = new F();)


August 17, 2008
BCS Wrote:

> Reply to Tim,
> 
> > Hi, I am a D newbie.  Why does this program segfault?
> > 
> > import std.stdio;
> > import std.c.stdlib;
> > class Foo
> > {
> > public int m_int = 0;
> > }
> > int
> > main(string[] args)
> > {
> > Foo f;
> > printf("foo %d\n", f.m_int);
> > return EXIT_SUCCESS;
> > }
> 
> The default value for any object Reference (f in this case) is null. create a new object to use (Foo f = new F();)

Doh, can you guess what language I am coming from? :)

Is there any way to do C++ style on-stack local object ?

August 17, 2008
Reply to Tim,

> BCS Wrote:
> 
>> Reply to Tim,
>> 
>>> Hi, I am a D newbie.  Why does this program segfault?
>>> 
>>> import std.stdio;
>>> import std.c.stdlib;
>>> class Foo
>>> {
>>> public int m_int = 0;
>>> }
>>> int
>>> main(string[] args)
>>> {
>>> Foo f;
>>> printf("foo %d\n", f.m_int);
>>> return EXIT_SUCCESS;
>>> }
>> The default value for any object Reference (f in this case) is null.
>> create a new object to use (Foo f = new F();)
>> 
> Doh, can you guess what language I am coming from? :)
> 
> Is there any way to do C++ style on-stack local object ?
> 

"scope" look it up in the classes spec page (I have not used it myself)


August 17, 2008
Tim H wrote:
> BCS Wrote:
> 
>> Reply to Tim,
>>
>>> Hi, I am a D newbie.  Why does this program segfault?
>>>
>>> import std.stdio;
>>> import std.c.stdlib;
>>> class Foo
>>> {
>>> public int m_int = 0;
>>> }
>>> int
>>> main(string[] args)
>>> {
>>> Foo f;
>>> printf("foo %d\n", f.m_int);
>>> return EXIT_SUCCESS;
>>> }
>> The default value for any object Reference (f in this case) is null. create a new object to use (Foo f = new F();)
> 
> Doh, can you guess what language I am coming from? :)
> 
> Is there any way to do C++ style on-stack local object ?

scope Foo f = new Foo();

The "scope" keyword in the declaration says to the compiler "this will not escape the function" thus creating the data on the stack. "f" is still a reference type in this case, however (although it may not be implemented as a reference -- that's up o the compiler).
August 18, 2008
Tim H., others have already answered your main question. Regarding your code:

Add the list of names you want to use, importing just the names you need, avoiding namespace pollution: import std.c.stdlib: writefln;


>         public int m_int = 0;
This is enough:
        public int m_int;
Because variables are initialized to .init by default (and there's a way to avoid that), and int.init is 0.


> int main(string[] args)

You can use this, because the return value is automatic:
void main()


To print writef/writefln is generally better, because typesafe (but it makes your executable fatter):
writefln(foo %d", f.m_int);

Bye,
bearophile
August 18, 2008
Robert Fraser:
> scope Foo f = new Foo();
> 
> The "scope" keyword in the declaration says to the compiler "this will not escape the function" thus creating the data on the stack. "f" is still a reference type in this case, however (although it may not be implemented as a reference -- that's up o the compiler).

But the 'scope' works only in some situations, the original poster can take a look at the docs about 'scope'.

My question: does the compiler raise a compilation error if the 'scope' keyword is used where the compiler can't actually allocate the class on the stack? (if not, then I think it may be better for the compiler to raise such error, avoiding the illusion of efficiency).

Bye,
bearophile
August 18, 2008
"bearophile" wrote
> Robert Fraser:
>> scope Foo f = new Foo();
>>
>> The "scope" keyword in the declaration says to the compiler "this will not escape the function" thus creating the data on the stack. "f" is still a reference type in this case, however (although it may not be implemented as a reference -- that's up o the compiler).
>
> But the 'scope' works only in some situations, the original poster can take a look at the docs about 'scope'.
>
> My question: does the compiler raise a compilation error if the 'scope' keyword is used where the compiler can't actually allocate the class on the stack? (if not, then I think it may be better for the compiler to raise such error, avoiding the illusion of efficiency).

In what situations does it not work?

As long as the class is not too large for the stack (which is very unlikely), it should be able to allocate.

-Steve


August 18, 2008
Steven Schveighoffer wrote:
> "bearophile" wrote
>> Robert Fraser:
>>> scope Foo f = new Foo();
>>>
>>> The "scope" keyword in the declaration says to the compiler "this will
>>> not escape the function" thus creating the data on the stack. "f" is
>>> still a reference type in this case, however (although it may not be
>>> implemented as a reference -- that's up o the compiler).
>> But the 'scope' works only in some situations, the original poster can take a look at the docs about 'scope'.
>>
>> My question: does the compiler raise a compilation error if the 'scope' keyword is used where the compiler can't actually allocate the class on the stack? (if not, then I think it may be better for the compiler to raise such error, avoiding the illusion of efficiency).
> 
> In what situations does it not work?
> 
> As long as the class is not too large for the stack (which is very unlikely), it should be able to allocate.
> 
> -Steve 
> 
> 

If the class has a component sub-class the component may be heap allocated.  I don't agree with making that an error though.

-Joel
August 18, 2008
Tim H wrote:
> 
> Doh, can you guess what language I am coming from? :)

This page might be helpful then:

http://prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers

-Joel
« First   ‹ Prev
1 2 3