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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:54:01+00:00 2026-06-13T11:54:01+00:00

Alright so Say I have a class with all its definition, bla bla bla…

  • 0

Alright so Say I have a class with all its definition, bla bla bla…

template <class DT>
class Foo{
private:
    DT* _data;
    //other stuff;
public:
    Foo(DT* data){ _data = data; }
    virtual ~Foo(){ delete _data; }
    //other methods
};

And then I have in the main method:

int main(){
    int number = 12;

    Foo<anyRandomClass>* noPrimitiveDataObject = new Foo<anyRandomClass>(new anyRandomClass());
    Foo<int>* intObject = new Foo<int>(number);

    delete noPrimitiveDataObject; //Everything goes just fine.
    delete intObject; //It messes up here, I think because primitive data types such as int are allocated in a different way.

    return 0;
}

My question is: What could I do to have both delete statements in the main method work just fine?

P.S.: Although I have not actually compiled/tested this specific code, I have reviewed it extensively (as well as indented), so if you find a mistake, please be nice.

  • 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-13T11:54:02+00:00Added an answer on June 13, 2026 at 11:54 am

    You’re taking the address of a literal and then calling delete on it later, which is wrong. It was not allocated with new, therefore you cannot deallocate it with delete (nor would it make any sense to).

    If you had written new int(12) instead it would be ok, however, there are other problems.

    First, your class violates The Rule of Three. What happens if I copy intObject and then call delete on both of them? You end up calling delete on the same pointer twice.

    Second, why are you allocating these things dynamically to begin with? You create an RAII style wrapper to handle deallocation for you… and then proceed to allocate it manually. What problem is that solving?

    I suppose this is an exercise for you, and that’s great. Just remember what problem you’re trying to solve with this code.

    If I am using a std::vector<T> I am certainly not going to use it like this:

    std::vector<int> *v = new std::vector<int>;
    

    It defeats the entire purpose of using a vector! Now I have to manually manage this pointer/memory, and that’s the problem that the vector class (and other RAII style classes) were created to solve.

    So, to use it properly, you do this:

    void foo() 
    {
        std::vector<int> v;
        // do stuff with v
        // it allocates its memory dynamically so you don't have to.
        // when we exit the function the destructor is called, the memory 
        // deallocated, and life continues as it should.
    }
    

    Use automatic storage duration to your advantage, that’s the whole point. Also be very clear about who owns the memory. If it is not clear from your class design who owns a given chunk of memory then it is not safe to delete it in your destructor.


    Ok, you changed the code to this now:

    int number = 12;
    // ...
    Foo<int>* intObject = new Foo<int>(number);
    

    Same problem; you are taking the address of a variable allocated with automatic storage duration and then calling delete on it. This is wrong. Anything you allocate with new you deallocate with delete, but nothing else. Ever. That’s it.

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

Sidebar

Related Questions

Alright lets say I have a cPlayer class that inherits from cOrganism , which
Alright, let's say that I have the following markup: <div class=ticker> <p>News Item One.
Alright, let's say I have these two tables: items with columns id, stuff item_properties
Alright so lets say I have these files in vendor/assets/javascript/ : modernizer.js lightbox.js highchart.js
Alright, so here's what I'm trying to do. Let's say I have the following
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Alright totally new to jeditable, Say I have some <div> 's and <input type='hidden'
Alright. Lets say i have a UITextField where a user can input an url
Alright, so let's say I have a database with a table named COMPANY_PARAMETERS that
Alright, so here is the situation... Say I have a navbar for a site,

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.