0

I've got a problem. When i'm starting my program, it works great, but when it proceeds it says that there is a breakpoint error. Then it says that there is a problem with a memory allocation so it's not working. Here is the code: Header:

#pragma once

#include <iostream>
#include <string>

class basics
{
private:
    char *str;
    int len;
    static int string_obj;
public:
    basics() { str = "ces", len = strlen(str), string_obj = 1; }
    basics(const basics &st);
    basics(const char *s);
    ~basics();
    friend std::ostream & operator<<(std::ostream &os, const basics &st);
};

Class functions:

#include "basics.h"
int basics::string_obj = 0;


basics::basics(const char *s)
{
    string_obj++;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    std::cout << string_obj << " objects was created." << std::endl;
}

basics::basics(const basics & st)
{
    string_obj++;
    len = std::strlen(st.str);
    str = new char[len + 1];
    std::strcpy(str, st.str);
    std::cout << string_obj << " objects was created." << std::endl;
}

basics::~basics()
{
    std::cout << string_obj << " objects was created. Now we'll delete one      of them." << std::endl;
    --string_obj;
    std::cout << string_obj << " objects left." << std::endl;
    delete[] str;
}

std::ostream & operator<<(std::ostream &os, const basics &st)
{
    os << st.str;
    return os;
}

Calling part:

#include "basics.h"

int main()
{
    basics gigas("fear");
    basics cop(gigas);
    gigas.~basics();
    system("pause");
    return 0;
}
4
  • While you don't show anything about the specifics of what error you get, I can imagine the issue here is your explicit call to gigas.~basics(); The destructor will be automatically called for you on scope exit. So you're calling it twice. And delete[] on an already deleted pointer can kind of do whatever. Crashing in this case. Commented Feb 7, 2016 at 19:16
  • If you just default construct your class, then delete will be called on memory not allocated by new. Commented Feb 7, 2016 at 19:16
  • 1
    You need to read about the rules of three, five and zero. And then stop using character pointers for string and change to std::string. Commented Feb 7, 2016 at 19:17
  • Thanks a lot! I've been working on the old compiler for some months and there was no auto-destructors. So, i used to write them manually. P.S. Character pointers was some kind of experiment, in other cases i'll use std::string, of course :) Commented Feb 7, 2016 at 19:25

1 Answer 1

4

Don't call the destructor explicitly:

gigas.~basics();

It will be called automatically when the scope is left. Thus the crash appears because it's called in fact twice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.