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

98
Visualizações
VLAs and side-effect in sizeof's operand

I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew.

void g(int n) {
    printf("g(%d)\n", n);
}

int main(void) {
    int i = 12;

    char arr[i]; // VLA

    (void)sizeof *(g(1), &arr); // Prints "g(1)"
    (void)sizeof (g(2), arr);   // Prints nothing

    return 0;
}

What is going on?

Just in case, this is compiled with GCC 5.1 on Coliru.

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

0

It seems that I should think twice before posting, because it struck me right after I did.

My understanding of how sizeof interacts with VLAs is actually correct, as the following quote confirms (thanks @this !) :

6.5.3.4 The sizeof and _Alignof operators
If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant

That's not what is causing this surprising (to me) behaviour.

(void)sizeof (g(2), arr);

In the (g(2), arr) subexpression, the comma operator triggers arr's array-to-pointer decay. Thus, sizeof's operand is no longer a VLA, but a plain char*, and it falls back to not evaluating its operand.

Apparently this behaviour has been altered in C++, where the comma operator won't decay arrays anymore.

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