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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:28:32+00:00 2026-05-25T02:28:32+00:00

I would like to ask if I’s correct the following : MyClass *obj =

  • 0

I would like to ask if I’s correct the following :

MyClass *obj = new MyClass();//allocate memory

obj.Variab="HELLO";

obj=NULL;
delete obj; //free memory

Is the memory allocated for obj deleted after the last two sentences? Appreciate.THX
I would like to mention that I am working in c++ /Ubuntu. G++ is the compiler

EDIT:

What if I have?

int i=0;
list<string>l;
while (i<100)
{  
    MyClass *obj = new MyClass();//allocate memory
    obj->Variab="HELLO";
    //add the obj.valie in a list 
    l.push_back(obj);
    i=i+1;

delete obj; //free memory

}

it is ok?

  • 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-25T02:28:33+00:00Added an answer on May 25, 2026 at 2:28 am

    no, you should use delete before assigning to NULL

    delete obj; //free memory 
    obj=NULL;
    

    this is becasue the actual parameter to delete is the address of the allocated memory, but if you assign NULL before delete is used, you are actually passing NULL to delete, and nothing will happen, and you will get yourself a memory leak.


    your edit question:

    this code will not compile, as obj is not defined outside the while scope, in any case, also, l is a list<string> and you are trying to insert MyClass* types,this will result in another compilation error. also, you should use obj->Variab and not obj.Variab, since obj is a pointer.

    EDIT to EDIT:

    well, you still got a compilation error, since obj is not defined when you are trying to delete it. try this:

    #include <iostream>
    #include <list>
    using namespace std;
    class MyClass {
    public:
        string Variab;
    };
    
    void myfunction (const string& s) {
      cout << " " << s;
    }
    
    int main()
    {
        int i=0;
        list<string>l;
        while (i<100) {
            MyClass *obj = new MyClass();//allocate memory
            obj->Variab="HELLO";
            l.push_back(obj->Variab);
            i=i+1;
            delete obj; //free memory
        }
        for_each (l.begin(), l.end(), myfunction);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask about Memory Overhead in java, I have a large
I would like to ask: I have a array with predefined order. var order=new
I would like to ask probably simple question. Consider following php page: <p>Name of
I would like to ask about memory management with regards to MKReverseGeocoder , prior
I would like to ask you for help with the following code I have
I would like to ask about the effect of writing to global memory in
I would like ask if there's a way to download an android layout from
What I would like ask is best illustrated by an example, so bear with
I would like to ask for some simple examples showing the uses of <div>
I would like to ask you which automated build environment you consider better, based

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.