Thread overview
main return value, struct padding
Jan 20, 2009
bearophile
Jan 20, 2009
Bill Baxter
Jan 20, 2009
bearophile
Jan 20, 2009
Denis Koroskin
January 20, 2009
I post this here, but if later I see it fit I may post something in the main D group too.

D allows to have the main() function of type void too. In such case I'd like the program return 0 by default. If people agrees, this can become a feature request.

A question: when D automatically adds some padding to a struct, is such unused space guaranteed to be initialized to a default constant value, like zero?

Bye,
bearophile
January 20, 2009
On Tue, Jan 20, 2009 at 11:15 PM, bearophile <bearophileHUGS@lycos.com> wrote:
> I post this here, but if later I see it fit I may post something in the main D group too.
>
> D allows to have the main() function of type void too. In such case I'd like the program return 0 by default. If people agrees, this can become a feature request.

What does it return now?  I always assumed returned zero for void main.

--bb
January 20, 2009
Bill Baxter:

> What does it return now?

A random int, I presume.


>I always assumed returned zero for void main.<

Me too.

Bye,
bearophile
January 20, 2009
On Tue, Jan 20, 2009 at 10:25 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> Bill Baxter:
>
>> What does it return now?
>
> A random int, I presume.
>
>
>>I always assumed returned zero for void main.<
>
> Me too.

I've only ever seen void mains return 0.  I think they used to return random ints but that was fixed long ago.  Maybe it's different on Linux?
January 20, 2009
"bearophile" wrote
>I post this here, but if later I see it fit I may post something in the main D group too.
>
> D allows to have the main() function of type void too. In such case I'd like the program return 0 by default. If people agrees, this can become a feature request.

A test:
[steves@localhost testing]$ cat testmainreturn.d
void main()
{
    return 1;
}
[steves@localhost testing]$ cat testmainreturn2.d
void main()
{
}
[steves@localhost testing]$ dmd testmainreturn.d
[steves@localhost testing]$ dmd testmainreturn2.d
[steves@localhost testing]$ ./testmainreturn
[steves@localhost testing]$ echo $?
1
[steves@localhost testing]$ ./testmainreturn2
[steves@localhost testing]$ echo $?
0
[steves@localhost testing]$ dmd | grep "Digital Mars D"
Digital Mars D Compiler v1.038
[steves@localhost testing]$

disassembly:
[steves@localhost testing]$ obj2asm testmainreturn.o
...
_Dmain:
                push    EBP
                mov     EBP,ESP
                mov     EAX,1
                pop     EBP
                ret
.text._Dmain    ends
...
[steves@localhost testing]$ obj2asm testmainreturn2.o
...
_Dmain:
                push    EBP
                mov     EBP,ESP
                xor     EAX,EAX
                pop     EBP
                ret
.text._Dmain    ends
...

So it appears it returns 0 unless you return something else (!)

-Steve


January 20, 2009
On Tue, Jan 20, 2009 at 12:21 PM, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> So it appears it returns 0 unless you return something else (!)

Ha!
January 20, 2009
Jarrett Billingsley Wrote:

> On Tue, Jan 20, 2009 at 12:21 PM, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> > So it appears it returns 0 unless you return something else (!)
> 
> Ha!

Yes (but not always), even though specs clearly say that void functions evaluate return values and discard them.

IIRC, this one returns 0:

void main() {
    return new Object();
}

while this one returns an object address:

void foo() { return new Object(); }

void main() {
    return foo();
}