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

278
Visualizações
how does it happened that a variable used before it's declared?

I am confused about a function dictCreate() in file dict.c of redis implementation. I am going to paste the code here:

/* Create a new hash table 
 * T = O(1)
 */
dict *dictCreate(dictType *type, void *privDataPtr) {
    dict *d = zmalloc(sizeof(*d));
    _dictInit(d, type, privDataPtr);
    return d;
}

variable d is used in zmalloc(sizeof(*d)), but theoretically it will exist when this line was executed. So my question is how it is possible to use variable d before it is declared?

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

0

sizeof is not a function, it is an operator. It is executed (evaluated, to be exact) at compile time, so the scope or lifetime you're thinking about d, does not apply here. All it needs to know is the type of *d and that is known at compile time. Sufficient.

over 4 years ago · Santiago Trujillo Relatório

0

The statement

dict *d = zmalloc(sizeof(*d));  

is equivalent to

dict *d;
d = zmalloc(sizeof(*d));  

So, dict *d declares d as a pointer to dict type and = zmalloc(sizeof(*d)); used for initialization. dict *d = zmalloc(sizeof(*d)); declares d as dict * and then initializes it in single line.

over 4 years ago · Santiago Trujillo Relatório

0

Your assumption is wrong, the object exists starting from the = sign that starts the initialization. E.g in a initializer you are well allowed to use the address of the object that you are initializing.

Here, in addition, no access to the object itself is needed, sizeof only uses the type in this case.

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