3

I'm trying to understand how the C Standard explains that the declaration can cause an error. Consider the following pretty simple code:

int main()
{
    char test[1024 * 1024 * 1024];
    test[0] = 0;
    return 0;
}

Demo

This segfaluts. But the following code does not:

int main()
{
    char test[1024 * 1024 * 1024];
    return 0;
}

Demo

But when I compiled it on my machine the latest one segfaulted too. The main function looks as

00000000000008c6 <main>:                        
 8c6:   55                      push   %rbp                            
 8c7:   48 89 e5                mov    %rsp,%rbp                                         
 8ca:   48 81 ec 20 00 00 40    sub    $0x40000020,%rsp        
 8d1:   89 bd ec ff ff bf       mov    %edi,-0x40000014(%rbp) // <---HERE    
 8d7:   48 89 b5 e0 ff ff bf    mov    %rsi,-0x40000020(%rbp)                      
 8de:   64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
 8e5:   00 00                       
 8e7:   48 89 45 f8             mov    %rax,-0x8(%rbp)
 8eb:   31 c0                   xor    %eax,%eax
 8ed:   b8 00 00 00 00          mov    $0x0,%eax       
 8f2:   48 8b 55 f8             mov    -0x8(%rbp),%rdx
 8f6:   64 48 33 14 25 28 00    xor    %fs:0x28,%rdx
 8fd:   00 00                              
 8ff:   74 05                   je     906 <main+0x40>
 901:   e8 1a fe ff ff          callq  720 <__stack_chk_fail@plt>
 906:   c9                      leaveq
 907:   c3                      retq
 908:   0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
 90f:   00

As far as I understood the segfault occurred when trying to mov %edi,-0x40000014(%rbp).

I tried to find the exaplanation in the N1570, Section 6.7.9 Initialization, but it does not seem to be the relevant one.

So how does the Standard explains this behavior?

7
  • 1
    Different levels of optimization? Try compiling with -O2 Commented Nov 17, 2018 at 9:02
  • @KeineLust I didn't specify any optimization flags when compiling on my machine neither in this demos. Just gcc -c main.c. Commented Nov 17, 2018 at 9:04
  • @KeineLust O2 makes it work, thanks. But anyway, how about the formal explanation of this behavior? Would be greate to find some reference in the Standard. This is the defined behavior, isn't it? Commented Nov 17, 2018 at 9:05
  • yes thats what I say, maybe Coliru is compiling with some level of optimization and skipping the whole program. Commented Nov 17, 2018 at 9:06
  • 1
    no the standard doesn't say that auto memory uses the stack. Some compilers could allocate the memory on the heap (and that would work) and free it in the end. Commented Nov 17, 2018 at 9:16

1 Answer 1

3

The result is implementation-dependent

I can think of several reasons of why the behaviour should differ

  • compiler seeing that variable isn't used, no possible side-effect, and optimizing it away (even without optimization levels)
  • stack resizing on request. Since there are no writes to this variable yet, why resizing the stack now?
  • compilers don't have to use the stack for auto memory. Compiler can allocate memory using malloc, and free it on exit. Using heap would allow to allocate 1Gb without issues
  • stack size set at 1Gb :)
Sign up to request clarification or add additional context in comments.

1 Comment

and optimizing it away (even without optimization levels) good point! no volatile no party :P

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.