| Thread overview | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 17, 2008 D newb | ||||
|---|---|---|---|---|
| ||||
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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim H | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim H | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim H | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim H | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Robert Fraser | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | "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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: D newb | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim H | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply