1

I have the following code:

void function(char *str)
{
    int i;
    char buffer[strlen(str) + 1];

    strcpy(buffer, str);
    buffer[strlen(str)] = '\0';

    printf("Buffer: %s\n", buffer);
}

I would expect this code to throw a compile time error, as the 'buffer' being allocated on the stack has a runtime dependent length (based on strlen()). However in GCC the compilation passes. How does this work? Is the buffer dynamically allocated, or if it is still stack local, what is the size allocated?

4
  • 4
    This is valid C99, and yes, it is allocated on the stack, based on the size it computes. Jul 16, 2014 at 14:09
  • @mafso Why do you say it would cause undefined behavior?
    – dragosht
    Jul 16, 2014 at 15:07
  • 2
    You've tagged two different languages. This is valid in (modern) C, but not (standard) C++. Which are you using? Jul 16, 2014 at 15:08
  • @DrewMcGowen Sorry I am a little slow. So what I understand is that this is completely valid in non-C99 and that the buffer would be allocated on the stack at every different call to function(); and the size would be equal to whatever strlen() evaluates to? Jul 16, 2014 at 16:39

2 Answers 2

2

C99 allows variable length arrays. Not compiling your code in C99 will not give any error because GCC also allow variable length array as an extension.

6.19 Arrays of Variable Length:

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++.

1

By disassembling your function you could easily verify this:

$ objdump -S <yourprogram>

...
void function(char *str)
{
   4011a0:   55                      push   %ebp
   4011a1:   89 e5                   mov    %esp,%ebp
   4011a3:   53                      push   %ebx
   4011a4:   83 ec 24                sub    $0x24,%esp
   4011a7:   89 e0                   mov    %esp,%eax
   4011a9:   89 c3                   mov    %eax,%ebx
     int i;
     char buffer[strlen(str) + 1];
   4011ab:   8b 45 08                mov    0x8(%ebp),%eax
   4011ae:   89 04 24                mov    %eax,(%esp)
   4011b1:   e8 42 01 00 00          call   4012f8 <_strlen>
   4011b6:   83 c0 01                add    $0x1,%eax
   4011b9:   89 c2                   mov    %eax,%edx
   4011bb:   83 ea 01                sub    $0x1,%edx
   4011be:   89 55 f4                mov    %edx,-0xc(%ebp)
   4011c1:   ba 10 00 00 00          mov    $0x10,%edx
   4011c6:   83 ea 01                sub    $0x1,%edx
   4011c9:   01 d0                   add    %edx,%eax
   4011cb:   b9 10 00 00 00          mov    $0x10,%ecx
   4011d0:   ba 00 00 00 00          mov    $0x0,%edx
   4011d5:   f7 f1                   div    %ecx
   4011d7:   6b c0 10                imul   $0x10,%eax,%eax
   4011da:   e8 6d 00 00 00          call   40124c <___chkstk_ms>
   4011df:   29 c4                   sub    %eax,%esp
   4011e1:   8d 44 24 08             lea    0x8(%esp),%eax
   4011e5:   83 c0 00                add    $0x0,%eax
   4011e8:   89 45 f0                mov    %eax,-0x10(%ebp)
....

The relevant piece of assembly here is sub %eax,%esp anyway. This shows that the stack was expanded based on whatever strlen returned earlier to get space for your buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.