Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Is returning a heap-allocated pointer from function OK?

When we return from function, if return value is a pointer, it must be define as static. Is this true for heap memory allocated pointer (such as new/malloc pointers),too?

consider this example:

#include"stdio.h"
char * func()
{
    char * X=new char[10];
    X[0]='C';
    x[1]='\0';
    return X;//is it a good return from function?
}
main()
{
    char * A=func();
    puts(A);
    delete[] A;
}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

As others already pointed out, technically, the code is okay.
However, the real problem with the code is the function signature char * func(). It only specifies that it will return a pointer to a char. That pointer can be

  • a normal pointer to a char variable
  • a pointer to a char array
  • a pointer to a c-style string

The pointer can point to

  • static memory
  • heap-allocated memory that will be freed somewhere else
  • heap-allocated memory that you are suppose to free yourself.

There is no way of knowing what to do unless you want to trust the documentation or to investigate in possibly a lot of code (functions calling functions calling functions...).

This can be avoided by returning

  • a char
  • an std::string
  • a container
  • a smart pointer.

Manual memory management is hard to get right within a complete application and should never be your main concern, so it should be avoided and not hidden by functions returning pointers.

over 4 years ago · Santiago Trujillo Denunciar

0

Besides the weird increment/decrement fiddling, the code is okay.

The variable X goes out of scope when the function returns, but that's only the actual variable and not what it points to.

Pointers are basically simple integers, whose contents just happens to be an address to somewhere in memory, and treated specially by the compiler. What happens when the function returns is that the contents of the variable X is copied before the variable goes out of scope. Then the copy of the contents (the actual memory address) is again copied, this time to the variable A in the calling function.

over 4 years ago · Santiago Trujillo Denunciar

0

C++ is not garbage collected. X in func() is not deallocated until the delete [] A statement in main().

The shenanigans with incrementing and decrementing X do not change that (unless they cause X to point outside the memory allocated with operator new, which would result in undefined behaviour).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda