Re: (too much) allocated memory

From: TenThumbs (tenthumbs_at_cybernex.net)
Date: 05/28/04


Date: Fri, 28 May 2004 17:50:48 +0000 (UTC)

Tim Roberts wrote:
> [omitted]
>
> Because the behavior is seemingly non-deterministic. I can't trust a
> compiler if I can't predict how it will translate a given piece of code.

I see what upsets you but I think I can explain part of it. Consider this
code:

extern int /* __attribute__ ((regparm(3))) */
  bar (char *, char *, char *);
int foo (char *s)
{
        char f1[8];
        char f2[8];
        char f3[8];
        f1[0] = s[0];
        f2[0] = s[1];
        f3[0] = s[2];
        return bar (f1, f2, f3);
}
int goo (char *s)
{
        char f4[8];
        char f5[16];
        f4[0] = s[0];
        f5[0] = s[1];
        return bar (f4, f5, (char *)0);
}

You have to use the variables or gcc will optimize them away. Using
  gcc-3.4.0 -S -fverbose-asm -O2
I get

        .text
        .p2align 4,,15
..globl foo
        .type foo, @function
foo:
        pushl %ebp #
        movl %esp, %ebp #,
        subl $40, %esp #,
        movl 8(%ebp), %edx # s, s
        movzbl (%edx), %eax #* s, tmp66
        movb %al, -24(%ebp) # tmp66, f1
        movzbl 1(%edx), %eax #, tmp67
        movb %al, -16(%ebp) # tmp67, f2
        movzbl 2(%edx), %eax #, tmp68
        movb %al, -8(%ebp) # tmp68, f3
        leal -8(%ebp), %eax #, tmp70
        movl %eax, 8(%esp) # tmp70,
        leal -16(%ebp), %eax #, tmp71
        movl %eax, 4(%esp) # tmp71,
        leal -24(%ebp), %eax #, tmp72
        movl %eax, (%esp) # tmp72,
        call bar #
        leave
        ret
        .size foo, .-foo
        .p2align 4,,15
..globl goo
        .type goo, @function
goo:
        pushl %ebp #
        movl %esp, %ebp #,
        subl $56, %esp #,
        movl 8(%ebp), %edx # s, s
        movzbl (%edx), %eax #* s, tmp62
        movb %al, -32(%ebp) # tmp62, f4
        movzbl 1(%edx), %eax #, tmp63
        movb %al, -24(%ebp) # tmp63, f5
        xorl %eax, %eax #
        movl %eax, 8(%esp) #,
        leal -24(%ebp), %eax #, tmp65
        movl %eax, 4(%esp) # tmp65,
        leal -32(%ebp), %eax #, tmp66
        movl %eax, (%esp) # tmp66,
        call bar #
        leave
        ret
        .size goo, .-goo

In the first function {f1, f2, f3} = ebp - {24,16,8} but in the second
{f4, f5} = ebp - {32,24}. Putting the arrays 8 bytes lower triggers the
additional 16 byte alignment. Using the -d? options I see that something
happens during global register allocation but I have no idea exactly what.
Could be a bug or a feature. You'd have to ask the gcc folks. You can also use
-Os.

-- 
Mercury
    2004-05-28 08:53:32.057 UTC (JD 2453153.870510)
    X  =   0.349565508, Y  =  -0.145894088, Z  =  -0.113696023
    X' =   0.007336451, Y' =   0.023564389, Z' =   0.011826050


Relevant Pages

  • Re: Virtual function and multiple inheritance
    ... method in Derived which is not in Foo and Goo. ... virtual int myFunc() ... I'll make a guess that these two vtables are specific for Derived. ...
    (microsoft.public.vc.language)
  • Re: Virtual function and multiple inheritance
    ... containing two __vfptr is for the simple reason to maintain the ... It could be used in place of either a Foo or a Goo object, ... I'll make a guess that these two vtables are specific for Derived. ...
    (microsoft.public.vc.language)
  • Re: Virtual function and multiple inheritance
    ... __vfptr for Foo contains Derived overridden virtual methods for Foo, ... itself's virtual methods implementation, and __vfptr for Goo contains Derived ... Here pF is a pointer to an object with the same memory layout as a Foo ...
    (microsoft.public.vc.language)
  • Re: Virtual function and multiple inheritance
    ... virtual int func(); ... class Derived: Foo, Goo { ... Here pF is a pointer to an object with the same memory layout as a Foo and pG is a pointer to an object with the same memory layout as a Goo. ...
    (microsoft.public.vc.language)
  • Re: OK, lets be a bit negative and look at what APL has not achieved !
    ... would avoid that wasted computation time ... Only one of FOO and GOO get executed. ... FOO and GOO are monadic function definitions; CONDITION selects one of them, and the selected function is applied to its argument THETA -- only one of FOO THETA and GOO THETA gets executed ...
    (comp.lang.apl)