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

133
Visualizações
Handle a char array returned from a function in C

I am using a library called tinyXML, which parses XML files. Many of its methods return a const char*.

After having read this question: how to return a char array from a function in C

I now believe that every time a method returns a char* it is the caller's (my) responsibility to explicitly free it, because it is probably allocated dynamically on the heap. Am I right / wrong? What can I assume?

(If I ever wrote a library I would much rather return std::string instead of char* arrays, because they are so much simpler for the user.)

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

0

You can't assume anything and must check the documentation for the method you are calling to know if you must free the pointer or not. Sometimes a function returning a const char * might be returning a statically allocated string:

const char *getName(){
    return "SPQR3";
}

Or it might be a pointer that is someone else's responsibility to free. For example, strcpy's return value is the same as the pointer you pass to it as input.

over 4 years ago · Santiago Trujillo Relatório

0

Read the library's documentation. Both usages (pointer to an internal buffer, or pointer to a dynamically-allocated buffer) are common, and aren't distinguishable from the function prototype alone.

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