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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:44:44+00:00 2026-06-05T08:44:44+00:00

If I am using a CString like this: void myFunc(char *str) { CString s(str);

  • 0

If I am using a CString like this:

void myFunc(char *str)
{
  CString s(str);
  // Manipulate other data with CString
  // ...
  // Finished

  // Should I somehow delete 's' here to avoid a memory leak?
}

Is the string erased once the function goes out of scope?

Also, I know that the new keyword allocates memory, if I construct an object without the new keyword, is memory still allocated? My intuition tells me yes, but I would like to verify.

e.g.

CString *asdf = new CString("ASDF");
// same as?
CString asdf("ASDF"); 
  • 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-05T08:44:47+00:00Added an answer on June 5, 2026 at 8:44 am

    new allocates memory on the heap, so

    CString *asdf = new CString("ASDF");
    

    Allocates a CString on the heap and assigns a pointer to it to asdf. That memory will not be freed, nor will the destructor of asdf be called until you call delete asdf.

    Without new, you are allocating on the stack, so

    CString asdf("ASDF");
    

    allocates stack memory, which asdf represents. This memory is automatically reclaimed when the stack is unwound (as in when you return from a function) and the destructor of asdf is automatically called when it goes out of scope.

    Also, CString cleans up its own resources, so if the CString object is cleaned up (goes out of scope if it’s on the stack or is deleted if it’s on the heap), the resources it uses will also be cleaned up.

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

Sidebar

Related Questions

#include <iostream> #include <string> #include <fstream> #include <cstring> using namespace std; int main(){ char
If I convert a std::string into a CString using something like: std::string ss(Foo); CString
I know how to PInvoke a method that wants a CString by using LPCTSTR
I am using the following code to open a file in my program. CString
#include <iostream> #include <cstring> #include <QString> using namespace std; class A { public: static
Using Jenkins or Hudson I would like to create a pipeline of builds with
I am writing a custom textfile-data parser (JSON-like) and I have lost many hours
I would like to load a text file using BIO_new_file(myPath, r) in objective-c. As
I understand that using vector is a good way to store binary data when
I need a function like string ToLiteral(string input) from this post . Such that

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.