View mode: basic / threaded / horizontal-split · Log in · Help
May 20, 2004
D bug? or user error?
hi, i'm getting seg faults with the following program code.
i'm using Slack 9 linux with dmd v0.89.
I type dmd bug.d, it compiles, and when i run the program ./bug
I get the following output...

"bug: before crash
Segmentation fault"

the things is, I can't possibly understand what I'm doing wrong. 
If I am doing something wrong, can someone please point it out to me? 
or have I discovered a D bug? 

thx. 
- clayasaurus

/////////////////////////////////////////////////////////////////////////////
// bug.d - is this my error? or some D bug?

class cNew
{
public:
void set(float num)
{
m_x = m_y = m_z = num;
assert(m_x == m_y == m_z == num);
}


private:
float m_x, m_y, m_z;
}

class cCrash
{
public:
this()
{
m_new.set(0); // is this so harmful?
}

private:
cNew m_new;
}

int main(char[][] argv)
{

printf("bug: before crash\n");
cCrash crash = new cCrash(); // <-- it must crash in here...
printf("bug: after crash\n"); // note: it Seg faults before it gets here on
Slack 9 linux

return 0;
}

// end
/////////////////////////////////////////////////////////////////////////////
May 21, 2004
Re: D bug? or user error?
clayasaurus wrote:

> class cNew
> {
> public:
> void set(float num)
> {
> m_x = m_y = m_z = num;
> assert(m_x == m_y == m_z == num);
> }
> 
> 
> private:
> float m_x, m_y, m_z;
> }
> 
> class cCrash
> {
> public:
> this()
> {
m_new is a null reference.  You need to allocate a cNew first.
> m_new.set(0); // is this so harmful?
> }
> 
> private:
> cNew m_new;
> }

Maybe if this problem pops up on the newsgroup enough, Walter will cave 
and make the compiler check. :)

 -- andy
May 21, 2004
Re: D bug? or user error?
oh ok, hehe. Well, that's cool, simple, elegant D at it again *doh!* 

In article <c8jj0r$plo$1@digitaldaemon.com>, Andy Friesen says...
>clayasaurus wrote:
>> 
>> class cCrash
>> {
>> public:
>> this()
>> {
>m_new is a null reference.  You need to allocate a cNew first.
>> m_new.set(0); // is this so harmful?
>> }
>> 
>> private:
>> cNew m_new;
>> }
>
>Maybe if this problem pops up on the newsgroup enough, Walter will cave 
>and make the compiler check. :)
>
>  -- andy
May 21, 2004
Re: D bug? or user error?
A compiler check would certainly make it easier for C++ guys to adopt D,
Walter. :)

On Thu, 20 May 2004 17:41:07 -0700, Andy Friesen wrote:

> clayasaurus wrote:
> 
>> class cNew
>> {
>> public:
>> void set(float num)
>> {
>> m_x = m_y = m_z = num;
>> assert(m_x == m_y == m_z == num);
>> }
>> }
>> 
>> private:
>> float m_x, m_y, m_z;
>> }
>> }
>> class cCrash
>> {
>> public:
>> this()
>> {
> m_new is a null reference.  You need to allocate a cNew first.
>> m_new.set(0); // is this so harmful?
>> }
>> }
>> private:
>> cNew m_new;
>> }
>> }
> Maybe if this problem pops up on the newsgroup enough, Walter will cave
> and make the compiler check. :)
> 
>   -- andy
May 21, 2004
Re: D bug? or user error?
Andy Friesen wrote:

> clayasaurus wrote:
> 
>> class cNew
>> {
>> public:
>> void set(float num)
>> {
>> m_x = m_y = m_z = num;
>> assert(m_x == m_y == m_z == num);
>> }
>>
>>
>> private:
>> float m_x, m_y, m_z;
>> }
>>
>> class cCrash
>> {
>> public:
>> this()
>> {
> 
> m_new is a null reference.  You need to allocate a cNew first.
> 
>> m_new.set(0); // is this so harmful?
>> }
>>
>> private:
>> cNew m_new;
>> }
> 
> 
> Maybe if this problem pops up on the newsgroup enough, Walter will cave 
> and make the compiler check. :)
> 
>  -- andy

For what it's worth (probably not much), I've already put this error on 
my list of common errors:

http://www.wikiservice.at/d/wiki.cgi?ErrorMessages#Un-initializedObject


-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
Top | Discussion index | About this forum | D home