Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!