Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Overflowing stack with huge local variable?

As it is said that 8 mb of stack is given to each process. This stack will be used to store local variables. So if i take an array of size max than of the stack , it must overflow ??

 int main()
{
int arr[88388608];
int arr1[88388608];
int arr2[88388608];
while(1);
return 0;
}

But i am unable to get the result !

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Welcome to the world of optimizing compilers!

Because of the as-if rule, the compiler is only required to build something that would have same observable results as your original code. So the compiler if free to:

  • remove the unused arrays
  • remove the empty loop
  • store the dynamic arrays from main outside of the stack - because main is a special function that shall be called only once by the environment

If you want to observe the stack overflow (the bad one, not our nice site :-) ), you should:

  • use some code to fill the arrays
  • compile with all optimization removed and preferently in debug mode to tell the compiler do what I wrote as accurately as you can

The following code does SIGSEGV with CLang 3.4.1 when compiled as cc -g foo.c -o foo

#include <stdio.h>

#define SIZE 88388608

void fill(int *arr, size_t size, int val) {
    for (size_t i=0; i<size; i++) {
        arr[i] = val;
    }
}    
int main() {
    int arr[SIZE];
    int arr1[SIZE];
    int arr2[SIZE];

    fill(arr, SIZE, 0);
    fill(arr1, SIZE, 0);
    fill(arr2, SIZE, 0);
    printf("%d %d %d\n", arr[12], arr1[15], arr2[18]);

    return 0;
}

and even this code works fine when compiled as -O2 optimization level... Compilers are now too clever for me, and I'm not brave enough to thoroughly look at the assembly code which would be the only real way to understand what is actually executed!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda