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

513
Visualizações
Works fine in C++, but runtime error in C

I have this code here ::

int f(int x, int *py, int **ppz)
{
  int y, z;
  **ppz += 1; 
   z  = **ppz;
  *py += 2;
   y = *py;
   x += 3;
   return x + y + z;
}

int main()
{
   int c, *b, **a;
   c = 4;
   b = &c;
   a = &b; 
   printf( "%d\n", f(c,b,a));
   printf( "%d\n", c);
}

(Please choose the header files accordingly)

When I run this code as a C++ program, it runs smoothly. But, when I run it as a C program, it completes its execution, prints all the values, but on the last line of the code, gives a Runtime Error. I attached a debugger, and at the end of the program(while running this code in C) on clicking Step-Over, it takes me to a min_gw file, which looks something like this::

__mingw_CRTStartup()
__mingw_CRTStartup+208: mov    %eax,%ebx
__mingw_CRTStartup+210: call   0x407fb0 <_cexit>
__mingw_CRTStartup+215: mov    %ebx,(%esp)
__mingw_CRTStartup+218: call   0x408088 <ExitProcess@4>
__mingw_CRTStartup+223: nop

What is this file?? And why does the code fail in C??

Ideone references ::

C :: http://ideone.com/sDbqnI

C++ :: http://ideone.com/Ubb71k

Thanks for any help in advance.. :)

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

0

All versions of C++, and C since C99, add an implicit return 0; at the end of main before falling off the end.

C89 (which is the default for most compilers) does not, so it is UB if you actually return from main by falling off the end.

Edit: In practice (assuming you are running without sanitizers), this means that you will return whatever happens to be in $eax already, i.e. the return value of the last function. Since printf usually returns a non-zero value, that is treated as failure.

over 4 years ago · Santiago Trujillo Relatório

0

You forgot the return statement in the end of main indicating success.

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