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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:52:00+00:00 2026-05-23T21:52:00+00:00

So in C++ if I create an object useing new I should always deallocate

  • 0

So in C++ if I create an object useing new I should always deallocate it using delete
For example

Segment::Segment(float length)
{
    segmentLength = length;

    angle = 0.0f;
    x = Rand::randFloat(1.0f, 1.5f);
    y = Rand::randFloat(1.0f, 1.5f);

    vx = Rand::randFloat(0.0f, 1.0f); 
    vy = Rand::randFloat(0.0f, 1.0f);
    prevX = Rand::randFloat(0.0f, 1.0f);
    prevX = Rand::randFloat(0.0f, 1.0f);
};

And lets say I use it like this in another class such as,

this._segmentObject = Segment(2.0f);
this._segmentPointer = new Segment(2.0f);

In the destructor of that class, I know I should call delete on the this._segmentPointer, however how do I make sure memory is deallocated for the other one?

  • 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-23T21:52:01+00:00Added an answer on May 23, 2026 at 9:52 pm

    Things not allocated with new, new[] or the malloc family should be destructed and “freed” when the object goes out of scope.

    Often this simply means that that the code has reached the end of the block it was declared it or that the object that it was in was destructed (one way or another).

    To see this in action, you can do something like this:

    struct C {
        C()  { std::cout << "C()" << std::endl; }
        ~C() { std::cout << "~C()" << std::endl; }
    };
    
    struct B {
        B()  { std::cout << "B()" << std::endl; }
        ~B() { std::cout << "~B()" << std::endl; }
    private:
        C c_;
    };
    
    struct A {
        A()  { std::cout << "A()" << std::endl; }
        ~A() { std::cout << "~A()" << std::endl; }
    };
    
    int main() {
        B *b = new B; // prints "B()", also constructs the member c_ printing "C()"
        { // starts a new block
            A a; // prints "A()";
        } // end of block and a's scope, prints "~A()"
    
        delete b; // prints "~B()", also destructs member c_, printing "~C()"
    }
    

    Note: that if we did not do delete b, the “~B()” and “~C()” would never print. Likewise, if c_ were a pointer allocated with new, it would need to be delete‘d in order to print “~C()”

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

Sidebar

Related Questions

I want to create a new object using Class::DBI. One of the fields of
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I have a VB6 dll that is trying to create a COM object using
How can I create an instance of an object using a class reference, and
i need to create a object Image from import javax.microedition.lcdui.Image; using a file .png
Using reflection, I need to investigate a user DLL and create an object of
I am using Time.parse to create a Time object from a string. For some
I'm using the socket.makefile method to create a file-like object on a UDP socket
I am planning to create my own custom Object Mapper for a type using
I'm trying to create an instance of a generic class using a Type object.

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.