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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:40+00:00 2026-05-31T15:22:40+00:00

I am teaching myself c++ templates. I wrote the following code, and I am

  • 0

I am teaching myself c++ templates. I wrote the following code, and I am getting a weird error about a pointer being freed that wasn’t allocated. I am guessing that something in my class template constructor isn’t actually calling new on the int when I ask for an <int> type of this class. The code is being compiled and run automatically by CodeRunner for mac which I set up to use the clang++ compiler for c++ files.

#include <vector>
#include <iostream>

template <typename T>
class HeapVal
{
    public:
        HeapVal(T val) {ptr = new T(val);}
        ~HeapVal() {delete ptr;}
        T get() {return *ptr;}
    private:
        T* ptr;
};

int main(){
  std::vector< ::HeapVal<int> > vec;
  for(int i = 0; i < 1000; ++i){
    ::HeapVal<int> h(i);
    vec.push_back(h);
  }
  for(int i = 0; i < 1000; ++i){
    std::cout << vec[i].get() << std::endl;
  }
  return( 0 );
}

That code results in the following error either during compilation or execution (looks to me like a run-time kind of error).

Untitled(30214) malloc: *** error for object 0x7f82f24007c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Run Command: line 1: 30214 Abort trap: 6           ./"$2" "${@:3}"
  • 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-31T15:22:42+00:00Added an answer on May 31, 2026 at 3:22 pm

    You’re missing a copy constructor and assignment operator for HeapVal. The implication of that is that you’ll ultimately end up trying to delete the same memory more than once, because you have multiple pointers to it – that’s what causes the crash.

    Try something like this:

    template <typename T>
    class HeapVal
    {
    public:
        explicit HeapVal(T val)
        :   ptr(new T(val))
        {}
    
        ~HeapVal()
        {
            delete ptr;
        }
    
        HeapVal(const HeapVal& rhs)
        :   ptr(new T(*rhs.ptr))
        {}
    
        HeapVal& operator=(const HeapVal& rhs)
        {
            HeapVal(rhs).swap(*this);
            return *this;
        }
    
        T get() const
        {
            return *ptr;
        }
    
    private:
        T* ptr;
    
        void swap(HeapVal& rhs)
        {
            std::swap(ptr, rhs.ptr);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm teaching myself about structures in C, and am having trouble compiling this code:
I'm teaching myself Python and my most recent lesson was that Python is not
I am currently teaching myself about various data strutures and am a little frustrated
I'm teaching myself some basic scraping and I've found that sometimes the URL's that
Teaching myself C and finding that when I do an equation for a temp
I'm still teaching myself how to bind and use observable collection. One problem that
I've been teaching myself C++ and someone told me that C++ does not have
I am teaching myself Haskell. I want to write a function that recursively finds
Teaching myself some c# and my problem is that I have a class which
Still teaching myself python so please don't hate me if my code is terrible...

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.