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

  • SEARCH
  • Home
  • 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 8218947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:58:47+00:00 2026-06-07T12:58:47+00:00

This is a fairly basic question and I am pretty sure I know the

  • 0

This is a fairly basic question and I am pretty sure I know the answer, but seeing as the consequence for being wrong is a segfault I figure I should ask. I have been using strlen() and the new char[] operator in the following way for quite some time now and just noticed something that threw up a red flag:

void genericCopy(char *somestring, char *someOtherString) {
    someOtherString = new char[strlen(somestring)];
    strcpy(someOtherString,somestring);
}

My question is, seeing as a string should be null terminated, should I be doing this as such:

void genericCopy(char *somestring, char *someOtherString) {
    someOtherString = new char[strlen(somestring)+1];
    strcpy(someOtherString,somestring);
    someOtherString[strlen(someOtherString)] = '\0';
}

So far I have never had a problem with the first method, but that doesn’t mean I’m doing it right. Since the length being return by strlen()is the number of characters in the string without the null terminator so new isn’t reserving space for ‘/0’… At least I don’t think it is.

  • 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-06-07T12:58:49+00:00Added an answer on June 7, 2026 at 12:58 pm

    First of all, you should know that this function of yours is pointless to write, just use strdup (if available on your system).

    But yes, you need an additional byte to store the \0, so always do something like new char[strlen(somestring)+1];. However, there is no need to manually add the \0; strcpy already does this.

    You should use something like Valgrind to discover this and similar bugs in your code.

    There is however an additional problem in your code; your code will always leak someOtherString; it will not be returned to where you called it from. You either need to change your method to something like:

    char *genericCopy(char *something) {
        char *copy = new char[strlen(somestring)+1];
        strcpy(copy,somestring);
        return copy;
    }
    

    and then get the copy as follows:

    copy = genericCopy(something);
    

    Or you need to change your method to something like:

    void genericCopy(char *something, char **copy) {
        *copy = new char[strlen(somestring)+1];
        strcpy(*copy,somestring);
    }
    

    and call it as:

    genericCopy(something, &copy);
    

    If you’ll be using C++ you could also just change the method prototype to:

    void genericCopy(char* somestring, char*& someOtherString)
    

    and call it as:

    genericCopy(something, copy);
    

    Then someOtherString will be passed as a reference, and the new value you allocate to it will propagate outside of your method.

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

Sidebar

Related Questions

This is a fairly basic question, but haven't seen a good answer on various
This is a fairly basic question, but I haven't been able to find any
This is a fairly basic question about state server but assume there are 2
I think the title for this question is probably wrong, but I'm not sure
This is a fairly basic question about CakePHP, but since my knowledge of this
All, This would seem like a fairly basic asp.net question - but in all
This is probably a fairly basic question but I haven't managed to find anything
I'm fairly certain I know the answer to this, but any ideas would be
I posted this basic question before, but didn't get an answer I could work
I know this is fairly subjective, but I'm diving into testing and learning about

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.