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 7954925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:34:48+00:00 2026-06-04T03:34:48+00:00

Situation: I’m implementing a list-like container that supports a Pop() function that should return

  • 0

Situation: I’m implementing a list-like container that supports a Pop() function that should return the user the const char* stored at the front of the container. However, I’m not sure as an implementer whether or not I should be returning the original const char* (deleting the node pointer from the container but not calling delete on the const char* itself), or whether I should allocate new memory and return a copy of the element.

From class and projects, I’ve encountered those who espouse always copying, so that no previously returned references (from getters, etc.) and pointers to the const char* can alter the popped version, but since this approach requires an extra allocation and strcpy, I thought I’d ask whether simply returning the original, undeleted pointer to const char without deleting it is really a problem. Here’s a code snippet of the approach WITH allocation and copy (and deleting the original reference afterwards):

const char* LinkedList::PopHeadString()
{
    node* deletehead = head_;
    char* output = NULL;

    if (head_ != NULL && GetHeadType() == STRING) {
        output = new char[strlen(head_->en.data.str) + 1];
        strcpy(output, head_->en.data.str);
        head_ = head_->next;
        delete deletehead->en.data.str;
        delete deletehead;
        --nEntries_;
    }
    return output;
}

Since Pop() is a common container operation, I thought I’d ask what the general approach 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-04T03:34:49+00:00Added an answer on June 4, 2026 at 3:34 am

    If you return the pointer without copying you have to decide who owns the pointer. The one who owns it is responsible for deleting it when it is no longer needed. This can be a very hard problem, especially if your code gets more complex. Copy-semantics are much simpler to reason about.

    In your specific example, the first thing to change would be to use std::string instead of const char * to represent strings. If you return an std::string by value, it will take care of the copying for you.

    If you really want to prevent the copy, but still manage the lifetime elegantly, you should consider using an std::shared_ptr<std::string> (or boost::shared_ptr<std::string> if you don’t have a C++11-compiler). A shared_ptr uses reference-counting to determine when the object it points to should be released, thus taking the burden of manual memory management from you, which is a Good Idea.

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

Sidebar

Related Questions

Situation I'm implementing a Listener for PHPUnit that will record test results to a
Situation: A user submits a url and my php script adds that url to
Situation is a string that results in something like this: <p>This is some text
Situation: I am creating a database user interface for a client's website. I know
Situation: I need to make an imap client (using java mail api) that if,
Situation with Thai text on a client site is that we can't control where
Situation: I am trying to write a efficient query using "LIKE" statement to look
Situation: I am writing a program in C that maintains a number of threads.
Situation: I send an ajax request that returns HTML containing elements needing event handlers
Situation: I have an Xcode project based on the Navigation-Based-Application template. So that means

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.