View mode: basic / threaded / horizontal-split · Log in · Help
August 14, 2007
Why does this simple program segfault?
Hi, 

I wrote this very simple class, but the call to fun.testIt() segfaults.
If I put the function outside of the class it works just fine.
What am I missing?
I have tried this with dmd v1.015 and the tango dmd v1.018, same thing.

Thank you very much.

-------------------
import std.stdio;

class Tester
{
   void testIt() {}
}

void main()
{
 Tester fun;
 fun.testIt();
 writefln("I made it!");
}
August 14, 2007
Re: Why does this simple program segfault?
Reply to convert,

> Hi,
> 
> I wrote this very simple class, but the call to fun.testIt()
> segfaults.
> If I put the function outside of the class it works just fine.
> What am I missing?
> I have tried this with dmd v1.015 and the tango dmd v1.018, same
> thing.
> Thank you very much.
> 
> -------------------
> import std.stdio;
> class Tester
> {
> void testIt() {}
> }
> void main()
> {
> Tester fun;
> fun.testIt();
> writefln("I made it!");
> }

you don't new the Tester

> Tester fun = new Tester();
August 14, 2007
Re: Why does this simple program segfault?
> you don't new the Tester
> 
> > Tester fun = new Tester();

Argh. Thanks a lot!
August 14, 2007
Re: Why does this simple program segfault?
BCS wrote:
> Reply to convert,
> 
>> Hi,
>>
>> I wrote this very simple class, but the call to fun.testIt()
>> segfaults.
>> If I put the function outside of the class it works just fine.
>> What am I missing?
>> I have tried this with dmd v1.015 and the tango dmd v1.018, same
>> thing.
>> Thank you very much.
>>
>> -------------------
>> import std.stdio;
>> class Tester
>> {
>> void testIt() {}
>> }
>> void main()
>> {
>> Tester fun;
>> fun.testIt();
>> writefln("I made it!");
>> }
> 
> you don't new the Tester
> 
>> Tester fun = new Tester();
> 
> 

Isn't there something we can do about this?  I do this about once a day 
when in a heavy D using phase.  It bites newbies and not-so-newbies 
alike.  I almost think it should be made so that no initializer calls 
the default constructor, and if you really want it to be null then you 
should initialize with null:

   Tester fun;  // creates a new Tester
   Tester nofun = null;  // doesn't create anything

But then there's all sorts of questions that crop up, like what should 
"new Tester[5]" do?  Or "Tester m_fun;" as a class/struct member.

I guess the best we can hope for is some kind of better error message 
than just a generic segfault, or perhaps a compiler warning if you 
forget to initialize a class instance.

--bb
August 14, 2007
Re: Why does this simple program segfault?
Bill Baxter escribi├│:
> BCS wrote:
>> Reply to convert,
>>
>>> Hi,
>>>
>>> I wrote this very simple class, but the call to fun.testIt()
>>> segfaults.
>>> If I put the function outside of the class it works just fine.
>>> What am I missing?
>>> I have tried this with dmd v1.015 and the tango dmd v1.018, same
>>> thing.
>>> Thank you very much.
>>>
>>> -------------------
>>> import std.stdio;
>>> class Tester
>>> {
>>> void testIt() {}
>>> }
>>> void main()
>>> {
>>> Tester fun;

The compiler should do the following:

Since fun wasn't assigned to something, any access to it before an 
assignment to fun should result in a compiler error.

>>> fun.testIt();
>>> writefln("I made it!");
>>> }
>>
>> you don't new the Tester
>>
>>> Tester fun = new Tester();
>>
>>
> 
> Isn't there something we can do about this?  I do this about once a day 
> when in a heavy D using phase.  It bites newbies and not-so-newbies 
> alike.  I almost think it should be made so that no initializer calls 
> the default constructor, and if you really want it to be null then you 
> should initialize with null:
> 
>    Tester fun;  // creates a new Tester
>    Tester nofun = null;  // doesn't create anything
> 
> But then there's all sorts of questions that crop up, like what should 
> "new Tester[5]" do?  Or "Tester m_fun;" as a class/struct member.

Any access to Tester[i] before Tester[i] is assigned something should 
result in a compiler error.

> 
> I guess the best we can hope for is some kind of better error message 
> than just a generic segfault, or perhaps a compiler warning if you 
> forget to initialize a class instance.
> 
> --bb
August 14, 2007
Re: Why does this simple program segfault?
Reply to Bill,

> Isn't there something we can do about this?  I do this about once a
> day when in a heavy D using phase.  It bites newbies and
> not-so-newbies alike.  I almost think it should be made so that no
> initializer calls the default constructor, and if you really want it
> to be null then you should initialize with null:
>

we could change the seg-v message to "Did you forget to new somthing?"
August 14, 2007
Re: Why does this simple program segfault?
Ary Manzana wrote:
> Bill Baxter escribi├│:
>> BCS wrote:
>>> Reply to convert,
>>>
>>>> Hi,
>>>>
>>>> I wrote this very simple class, but the call to fun.testIt()
>>>> segfaults.
>>>> If I put the function outside of the class it works just fine.
>>>> What am I missing?
>>>> I have tried this with dmd v1.015 and the tango dmd v1.018, same
>>>> thing.
>>>> Thank you very much.
>>>>
>>>> -------------------
>>>> import std.stdio;
>>>> class Tester
>>>> {
>>>> void testIt() {}
>>>> }
>>>> void main()
>>>> {
>>>> Tester fun;
> 
> The compiler should do the following:
> 
> Since fun wasn't assigned to something, any access to it before an 
> assignment to fun should result in a compiler error.
> 
>>>> fun.testIt();
>>>> writefln("I made it!");
>>>> }
>>>
>>> you don't new the Tester
>>>
>>>> Tester fun = new Tester();
>>>
>>>
>>
>> Isn't there something we can do about this?  I do this about once a 
>> day when in a heavy D using phase.  It bites newbies and 
>> not-so-newbies alike.  I almost think it should be made so that no 
>> initializer calls the default constructor, and if you really want it 
>> to be null then you should initialize with null:
>>
>>    Tester fun;  // creates a new Tester
>>    Tester nofun = null;  // doesn't create anything
>>
>> But then there's all sorts of questions that crop up, like what should 
>> "new Tester[5]" do?  Or "Tester m_fun;" as a class/struct member.
> 
> Any access to Tester[i] before Tester[i] is assigned something should 
> result in a compiler error.

I don't think that's something that could be realistically enforced 
using static analysis.  How would you propose implementing that?

--bb
August 14, 2007
Re: Why does this simple program segfault?
Bill Baxter wrote:
> Isn't there something we can do about this?  I do this about once a day 
> when in a heavy D using phase.  It bites newbies and not-so-newbies 
> alike.  I almost think it should be made so that no initializer calls 
> the default constructor, and if you really want it to be null then you 
> should initialize with null:
> 
>    Tester fun;  // creates a new Tester
>    Tester nofun = null;  // doesn't create anything
> 
> But then there's all sorts of questions that crop up, like what should 
> "new Tester[5]" do?  Or "Tester m_fun;" as a class/struct member.
> 
> I guess the best we can hope for is some kind of better error message 
> than just a generic segfault, or perhaps a compiler warning if you 
> forget to initialize a class instance.
> 
> --bb

it would be nice to have a way to specify the default initializer for 
any type.
August 15, 2007
Re: Why does this simple program segfault?
Bill Baxter Wrote:

> Isn't there something we can do about this?  I do this about once a day 
> when in a heavy D using phase.  It bites newbies and not-so-newbies 
> alike.  I almost think it should be made so that no initializer calls 
> the default constructor, and if you really want it to be null then you 
> should initialize with null:
> 
>     Tester fun;  // creates a new Tester
>     Tester nofun = null;  // doesn't create anything

But then I (coming from a Java background) would be bitten by it about once a day.

I think the best way is to make a compiler warning for every local variable that's used before it's explicitly assigned, a la Java (actually, it's an error in Java).
August 15, 2007
Re: Why does this simple program segfault?
Robert Fraser wrote:
> Bill Baxter Wrote:
> 
>> Isn't there something we can do about this?  I do this about once a day 
>> when in a heavy D using phase.  It bites newbies and not-so-newbies 
>> alike.  I almost think it should be made so that no initializer calls 
>> the default constructor, and if you really want it to be null then you 
>> should initialize with null:
>>
>>     Tester fun;  // creates a new Tester
>>     Tester nofun = null;  // doesn't create anything
> 
> But then I (coming from a Java background) would be bitten by it about once a day.
> 
> I think the best way is to make a compiler warning for every local variable that's used before it's explicitly assigned, a la Java (actually, it's an error in Java).

Assuming it applied only to classes in D, that seems like a good idea.

--bb
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home