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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:38:23+00:00 2026-06-04T14:38:23+00:00

I have a function that converts a relative path (e.g. log\crash.txt) to the full

  • 0

I have a function that converts a relative path (e.g. “log\crash.txt”) to the full path (e.g. “E:\Program\log\crash.txt”). The function is:

string_t GetAbsPath(string_t relPath)
{
    char abs[1024];

    //Get the working directory
    GetCurrentDirectory(1024, abs);

#pragma warning(disable:4996)
    //Add a slash and content folder
    memcpy(&abs, strcat(abs, "\\content\\"), 1024);
    //Append it to the relative path
    memcpy(&abs, strcat(abs, relPath.c_str()), 1024);
#pragma warning(default:4996)

    return abs;
}

string_t is a class I wrote, its basically a wrapper for a const char*. My problem is, (and I sort of anticipated this…) is that when the function returns, abs goes out of scope, and the string_t that gets the returned value is now empty/junk. In a situation like this, I would normally just use memcpy to copy it into a pointer that won’t go out of scope. But the issue there is, that pointer (which would be the const char* in string_t) would need to be delete[]‘d. string_t doesn’t have a destructor to begin with. I could write one and delete[] it there, but that posses another problem:

If I create a string_t like this:

string_t crash = "New[] isn't called! Ahh!";

When I go to delete[] it in the destructor, the program will crash because new[] was never called.

I could delete[] the const char* in the function that calls GetAbsPath, like this:

void LoadModel(string_t relPath)
{
     string_t fullPath = GetAbsPath(relPath);
     . . .
     delete[] fullPath.c_str();
}

But I know that if I come back to the code later, I’ll be like “Why is that delete[] there”, or I’ll add it when it isn’t needed… and well the pointer is, there’s a lot of room for error there.

What can I do to keep that char in scope (I’m guessing this can only be done with pointers), and make sure that allocated memory gets cleaned up? There has to be a way, because std::string keeps everything clean, and it has features like concatenation, something my string_t doesn’t even have. I appreciate any help here, because I’m at a loss…

  • 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-04T14:38:25+00:00Added an answer on June 4, 2026 at 2:38 pm

    You have three choices:

    1) Decide that string_t will always own the string it points to and it will be responsible for deallocating it. Have the constructors allocate/copy.

    2) Decide that string_t will never own the string it points to and code calling it will always be responsible for deallocating it if necessary.

    3) Decide that string_t will support both models and will need a flag to decide whether to call free[] or not in its destructor. Calling code will have to tell string_t whether to allocate/copy or whether to just stash the pointer.

    Make sure to follow the rule of three. Or better yet, just use std::string which consistently takes option 1 and thus you can just use it without worrying.

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

Sidebar

Related Questions

I have a function that reads lines from a log file, converts these lines
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have a function that converts 60000 into 60k or 60100 into 60.1k But
I have a function that takes longitude and latitude and converts it to x
I have a small C# function that takes any generic SQLDataReader and converts it
I have this function called ProperCase that takes a string, then converts the first
i have a function that converts an array to a hash which i would
I have a function that converts a .csv file to a datatable. One of
I have this function that converts an integer to a std::string: std::string intToStr(const int
It's a small thing, really: I have this function that converts dict objects to

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.