Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:12:26+00:00 2026-05-25T14:12:26+00:00

Is it correct to write a function that returns char *? Something like this:

  • 0

Is it correct to write a function that returns char *?

Something like this:

char * func()
{  
 char *c = new char[3];
 strcpy(c, "hi");
 c[2] = '\0';
 return c;
}

int main()
{
 char *c;
 c = func();
 //code using c
 delete [] c;
 return 1;
}

It works, but is it correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:12:27+00:00Added an answer on May 25, 2026 at 2:12 pm

    It is correct (assuming you meant strcpy and not strlen), but it must be very clear from the documentation of the function that it’s the caller’s responsibility to free the returned pointer, and what method does it have to use to free it (new => delete, new [] => delete[], malloc => free, etc). That is the “C way” of doing it.

    This relies on users of your function (including you after several months you wrote it) to read the documentation to get things right, so it’s quite error prone; also, in C++ there are several more complications that are not present in C (namely: exceptions) that make using raw pointers in these contexts not such a good idea.

    That’s the reason why you normally should return classes that encapsulate resources (e.g. std::string in this case, and in general containers that manage the memory automatically) or ownership-transferring smart pointers, that also have the advantage of being exception-safe (which your code is not).

    This sounds extremely complicated, but in fact it’s not:

    #include <string>
    
    std::string func()
    {  
       return "hi"; // actually, to be more explicit it should be return std::string("hi")
    }
    
    int main()
    {
        std::string c;
        c=func();
        return 1;
    }
    

    That’s it, no need to worry about allocations/deallocations and exceptions, all this is automatically handled by the std::string class, so you can manage strings almost as they were builtin types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I am trying to write a function that returns me value based on
I have to write a function that fills a char* buffer for an assigned
I'm interested in not having to write map the int function to the tuple
I have a wrapper function that returns a function. Is there a way to
I have to write a program in which main calls other functions that test
I've been doing a lot of research on how best to write correct network
please correct the program. Q. Write a java prg to accept RollNo, Name of
So I wrote this short script (correct word?) to download the comic images from
Correct me if I am wrong, int is 4 bytes, with a range of
Am I correct in assuming that the only difference between "windows files" and "unix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.