Re: string-486.h?

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Thu Feb 24 2000 - 08:08:49 EST


On Wed, 23 Feb 2000, David Schleef wrote:
>
> it.c:
>
> #include <stdio.h>
> int main(int argc,char *argv[])
> {
> char str[12]="mystring";
> printf(str);
> return 0;
> }
>
> cc -S it.c
>
> it.s:
>
> .file "it.c"
> .version "01.01"
> gcc2_compiled.:
> .section .rodata
> .LC0:
> .string "mystring"
> .globl memset
> .text
> .align 4
> .globl main
> .type main,@function
> main:
> pushl %ebp
> movl %esp,%ebp
> subl $12,%esp
> movl $.LC0,%eax
> movl (%eax),%edx
> movl %edx,-12(%ebp)
> movl 4(%eax),%edx
> movl %edx,-8(%ebp)
> movb 8(%eax),%al
> movb %al,-4(%ebp)
> leal -3(%ebp),%eax
> pushl $3
> pushl $0
> pushl %eax
> call memset
> addl $12,%esp
> leal -12(%ebp),%eax
> pushl %eax
> call printf
> addl $4,%esp
> xorl %eax,%eax
> jmp .L1
> .align 4
> .L1:
> leave
> ret
> .Lfe1:
> .size main,.Lfe1-main
> .ident "GCC: (GNU) 2.7.2.3"
>

Well the problem is bad 'C' code. It's not the compiler's fault. It's
just doing what it's told to do.

         char str[12]="mystring";

   declares a string on the stack with room for 12 bytes. However it is
initialized with only 9 (8 characters + the implied \0). So the rest
of the area has to be zeroed, which the compiler does.

Characters used to initialize a string have to be stored somewhere.
So code such as above wastes both space and the CPU cycles necessary
to initialize the string. It should have been decared as:

         static char str[12]="mystring";

  outside of the function body so the compiler allocates and initializes
it once.

Cheers,
Dick Johnson

Penguin : Linux version 2.3.41 on an i686 machine (800.63 BogoMips).

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Feb 29 2000 - 21:00:09 EST