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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:59:27+00:00 2026-05-19T11:59:27+00:00

When dealing with naked pointer, what is the best way to make sure that

  • 0

When dealing with naked pointer, what is the best way to make sure that memory get released? For example, implemented linked-list using C style.

#include <iostream>

using namespace std;

struct node {
    int value;
    node* next;
    node( int value ) : value( value ), next( NULL ) {
    }
};

void put( node* root, int value ) {
    node *newnode = new node( value );
    while( root->next )
        root = root->next;
    root->next = newnode;
}

node* pick( node *root ) {
    if( root->next ) {
        node* temp = root->next;
        root->value = root->next->value;
        root->next = root->next->next;
        delete temp;
        return root;
    }
    return NULL;
}

struct las {
private:
    node *a;
public:
    las() {
        a = new node( 0 );
        a->next = new node( 1 );
    }

    void next() {
        node* na = new node( 0 );
        while( a->next ) {
            pick( a );
            int count;
            int value = a->value;
            for( count = 1; a->next && a->value == a->next->value; count++ ) {
                pick( a );
            }
            put( na, count );
            put( na, value );
        }
        delete a;
        a = na;
    }

    friend  
    ostream& operator <<( ostream& os, const las& olas ) {
        node* root = olas.a;
        while( root->next ) {
            root = root->next;
            os << root->value;
        }
        return os;
    }
};

int main() {
    las* ls = new las();
    int n = 10;
    for( int i = 0; i < n; i++ ) {
        cout << *ls << endl;
        ls->next(); 
    }

    delete ls;
    return 0;
}

I’m not the author of this code, so how can I find out is there memory in this program?

  • 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-19T11:59:27+00:00Added an answer on May 19, 2026 at 11:59 am

    An alternative to a heap checker if you know which object may be a culprit is to manually instrument your code:

    unsigned nodes = 0;
    struct node {
        ~node()
        {
            --nodes;
        }
        int value;
        node* next;
        node( int value ) : value( value ), next( NULL ) {
            ++nodes;
        }
    };
    

    Then:

    int main()
    {
        // etc.
        delete ls;
        std::cout << "Nodes: " << nodes << std::endl;
        return 0;
    }
    

    This kind of light weight check can be built into a unit test (or even production code), ensuring that you pick up the leak as soon as possible. You might wrap it in NDEBUG so as to remove cleanly from a release build when wanted.

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

Sidebar

Related Questions

Basically, dealing with an XML feed that's not written in the nicest way (Youtube
I'm dealing with a design problem that I'm sure has a simple answer and/or
Dealing with an employee that went over my head
I'm dealing with a MySQL table that defines the JobName column as UNIQUE. If
I am dealing with MySQL tables that are essentially results of raytracing simulations on
The table I'm dealing with it potentially larger than available memory (let's say 10GB)
I'm dealing with an issue with my current employer that has seriously made me
I am dealing with a large codebase that has a lot of classes and
I'm dealing with a SOAP webservice call from a server that is expecting to
Dealing with go's funcs I discovered that one can't force the compiler to control

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.