Code:
#include <stdio.h>
int main(void)
{
int i;
for(i=0;i<100;i++)
{
char szBuffer[1000];
strcpy(szBuffer,"bleh");
printf("szBuffer = %s\n",szBuffer);
}
return 0;
}
Code:
.file "tehlocal.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "bleh\0"
LC1:
.ascii "szBuffer = %s\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $1064, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -1036(%ebp)
movl -1036(%ebp), %eax
call __alloca
call ___main
movl $0, -12(%ebp)
L2:
cmpl $99, -12(%ebp)
jg L3
movl $LC0, 4(%esp)
leal -1032(%ebp), %eax
movl %eax, (%esp)
call _strcpy
leal -1032(%ebp), %eax
movl %eax, 4(%esp)
movl $LC1, (%esp)
call _printf
leal -12(%ebp), %eax
incl (%eax)
jmp L2
L3:
movl $0, %eax
leave
ret
.def _printf; .scl 3; .type 32; .endef
.def _strcpy; .scl 3; .type 32; .endef
Compiled the .c file with the -S switch on MinGW, and the assembly file is what was provided. All local variables have been allocated at the beginning of the function. Therefore, I would say you're not getting a real performance increase or decrease by allocating the buffer anywhere. At this point, you'd dealing with good C coding practice for readability, maintainability, etc. etc..
In case there is any doubt, I compiled it with the buffer declared before i, and the assembly output was almost identical.
I'm not really sure what you think you're gaining by using the first method anyway.