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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:09:26+00:00 2026-06-02T04:09:26+00:00

Quick question; I’ve googled around and found some answers already, but I’m a bit

  • 0

Quick question; I’ve googled around and found some answers already, but I’m a bit paranoid so I want to be sure.

Consider this situation:

struct CoordLocation
{
    float X;
    float Y;
    float Z;
};

int main()
{
    CoordLocation *coord = new CoordLocation();
    delete coord;
    return 0;
}

Will calling delete also clear the memory used by the fields X, Y, Z? Some answers I found mentioned that I’d just delete the POINTER, not the actually referenced object this way.
What if…

struct CoordLocation
{
    float *X;
    float *Y;
    float *Z;
};

int main()
{
    CoordLocation *coord = new CoordLocation();
    delete coord;
    return 0;
}

And what if I manually free the memory for each object inside the struct’s constructor/destructor?

struct CoordLocation
{
    CoordLocation()
    {
         *X = new float;
         *Y = new float;
         *Z = new float;
    }
    ~CoordLocation()
    {
         delete X; delete Y; delete Z;
    }
    float *X;
    float *Y;
    float *Z;
};

int main()
{
    CoordLocation *coord = new CoordLocation();
    delete coord;
    return 0;
}

I noticed that for a simple situation such as:

   float *a = new float;
   *a = 5.0f;
   printf("%f", *a);
   delete a;
   printf("%f", &a);

printf would print 5.0, so the variable pointed to by a is not exactly destroyed.

So my question is:
How can I reliably free (as in no memory leaks) ALL the memory used by the struct in this case?

struct CoordLocation
{
    float X;
    float Y;
    float Z;
};

int main()
{
    CoordLocation *coord = new CoordLocation();
    delete coord;
    return 0;
}

Thanks!

  • 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-02T04:09:27+00:00Added an answer on June 2, 2026 at 4:09 am

    You only need to delete memory you allocate with new.

    printf would print 5.0, so the variable pointed to by a is not exactly destroyed.

    You’re actually running into undefined behavior. Although the value is still there, the memory was released and can be reused.

    So the following:

    struct CoordLocation
    {
        float X;
        float Y;
        float Z;
    };
    

    can’t create a memory leak if you omit the destructor.

    Your next snippet:

    struct CoordLocation
    {
        float *X;
        float *Y;
        float *Z;
    };
    
    int main()
    {
        CoordLocation *coord = new CoordLocation();
        delete coord;
        return 0;
    }
    

    can potentially create a memory leak, but not as it is. The following will:

    int main()
    {
        CoordLocation *coord = new CoordLocation();
        coord->X = new float();
        delete coord;
        return 0;
    }
    

    Your third example

    struct CoordLocation
    {
        CoordLocation()
        {
             *X = new float;
             *Y = new float;
             *Z = new float;
        }
        ~CoordLocation()
        {
             delete X; delete Y; delete Z;
        }
        float *X;
        float *Y;
        float *Z;
    };
    
    int main()
    {
        CoordLocation *coord = new CoordLocation();
        delete coord;
        return 0;
    }
    

    won’t create a memory leak because you free all the memory that you allocate. If you were to omit the destructor or forget to call delete coord;, they you’d have a memory leak.

    A good rule of thumb: call a delete for every new and a delete[] for every new[] and you’re safe.

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

Sidebar

Related Questions

Quick question, I found answers close to this but nothing that helps me. I
Quick question, having a look around it seems this is the case, but it
Quick question, If I want to document some code on a basic HTML and
Quick question regarding CSS and the browser. I tried searching SO and found some
Quick question. For some reason links referencing the home page contain an extra /web/guest/home
Quick question about the TransactionScope object. Found this on the internet: When you access
Quick question: I'm using jquery ajax to call a page that returns some json
Quick Question Lets say I have $colname = 'offer_id'; and then I want to
Quick question i imagine, but i'm having trouble getting a clear answer from googling.
Quick question about data joining. Say I have some elements that each contain a

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.