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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:16:23+00:00 2026-05-13T06:16:23+00:00

NOTE: THIS IS NOT HOMEWORK IT IS FROM A PRACTICE EXAM GIVEN TO US

  • 0

NOTE: THIS IS NOT HOMEWORK IT IS FROM A PRACTICE EXAM GIVEN TO US BY OUR PROFESSORS TO HELP US PREPARE FOR OUR EXAM

I’m currently studying for a programming exam. On one of the sample tests they gave us we have the following question:

Suppose you have been given a templated Container that holds an unordered collection of objects.

template <typename T>
class Container {
   public:
      void insert(T *op);
      // EFFECTS: inserts the object pointed to by op into
      // the container
      T *remove();
      // EFFECTS: removes an object from the Container, and
      // returns a pointer to it. Returns NULL if no
      // objects remain in the Container.
      // Note: the implementation can choose which
      // object to return if more than one exists.
      Container(); // ctor
      Container(const Container &l); // copy ctor
      Container &operator=(const Container &l); // assignment
      ~Container(); // dtor
   private:
      ...
};

Note that this is the interface only; the implementation details have been left out for brevity.
However, you may assume that the implementation is node based; a linked collection of nodes
hold objects.

You suspect that the implementation of the destructor does not satisfy the Conservation Rule of the At-Most-Once invariant, and is leaking memory instead. Write an acceptance test (similar to those in Project 4) to check for this condition. You must supply a suitable contained type, and a main that performs the test.

Note that you cannot depend on any behavior that the language leaves undefined, you may not
assume that you have the altnew allocator from Project 5 available to you, and you may not
override the delete operator. Hint: you are allowed to use a global variable.

I though something like:

#include <iostream>

using namespace std;

int *p = NULL;

void leak() {
    int *num = new int(5);
    p = num;
    delete num;
}

int main() {
    if ((*p = 6)) {
        cout << "Memory leak\n";
    } else {
        cout << "No Leak\n";
    }
}

The basic idea behind this is I though I couldn’t write to a space of memory that I hadn’t allocated. In compiling this test code though it works just fine so apparently you can. Any ideas on how to write such a test case though?

  • 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-13T06:16:24+00:00Added an answer on May 13, 2026 at 6:16 am

    What if you create a class to use as the template parameter that will add 1 to a global variable in it’s constructor and decrease that same global variable by 1 in it’s destructor.

    Then you can perform whatever tests you want on the container (create it, fill it and empty it, delete it, etc) and check for memory leaks by checking that the global variable is 0 after the container has been destroyed.

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

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Moderator note: This question is not a good fit for our question and answer
Note This is not a REBOL-specific question. You can answer it in any language.
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using
Note that this function does not have a { and } body. Just a
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Exactly what the title says. Note, this is not about subscriptable objects.
This is a homework question. Frankly, I'm not sure how a C program delivers
Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note: this is a different problem to https - it's related to privacy security
Note: This is the opposite direction to most similar questions! I have an iPhone

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.