'The delete keyword is not working even though when I build the code it say there are not errors
#include <iostream>
#include <cstring>
#include <string>
class Cd {
private:
const char* performers;
const char* label;
int selections;
double playtime;
public:
Cd(){
int eight = 8;
performers = new char[eight];
label = new char[eight];
label = "Unknown";
performers = "Unknown";
selections = 0;
playtime = 0.0;
}
Cd(const char* performers, const char* label, int selections, double playtime) {
this->performers = new char[strlen(performers)+1];
this->performers = performers;
this->label = new char[strlen(label) + 1];
this->label = label;
this->selections = selections;
this->playtime = playtime;
}
void setLabel(const char* label) {
this->label = label;
}
void setPerformers(const char* performers) {
this->performers = performers;
}
void setSelections(int selections) {
this->selections = selections;
}
void setPlaytime(double playtime) {
this->playtime = playtime;
}
const char* getLabel() {
return label;
}
const char* getPerformers() {
return performers;
}
int getSelections() {
return selections;
}
double getPlaytime() {
return playtime;
}
virtual void Report() {
std::cout << "Performers: " << performers<<std::endl
<<"Label: " <<label<< std::endl
<<"Number of selections: " << selections << std::endl
<<"Play time: " << playtime << std::endl;
}
~Cd() {
delete[] performers;
delete[] label;
}
};
class Classic : public Cd {
private:
const char* primaryWork;
public:
Classic() {
primaryWork = new char[8];
primaryWork = "Unknown";
}
Classic(const char* primaryWork, const char* performers, const char* label, int selections, double playtime) {
this->primaryWork = new char[strlen(primaryWork) + 1];
this->primaryWork = primaryWork;
setLabel(label);
setPerformers(performers);
setSelections(selections);
setPlaytime(playtime);
}
virtual void Report() {
std::cout << "Primary work: " << primaryWork << std::endl<<
"Performers: " << getPerformers() << std::endl <<
"Label: " <<getLabel() << std::endl<<
"Number of selections: " << getSelections() << std::endl
<< "Play time: " << getPlaytime() << std::endl;
}
~Classic() {
delete[] primaryWork;
};
};
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips" , 2, 57.17);
Cd* parent = &c1;
std::cout << "\nUsing object directly:\n";
std::cout << "***************************" << std::endl;
c1.Report();
c2.Report();
std::cout << "\nUsing type cd * pointer to objects:\n";
std::cout << "***************************" << std::endl;
// Call Report() using Cd type pointer created above
parent->Report();
Classic* classic = &c2;
classic->Report();
// Call Report() using Cd type pointer containing Classic object address
return 0;
}
I don't understand what is wrong with the delete keyword. My code is supposed to delete the memory allocation for the array at the end to save memory, but it is not working. The delete_scalar file pops up and the code does not finish executing. The rest of the code is working fine. The big problem is when I build the code the compiler tells me that there are no errors found.
Solution 1:[1]
Write
std::string performers;
instead of
const char* performers;
and so on for the other string-like members. Then you can drop the new[] and delete[] entirely. The bug you have is a reassignment of the pointer to some read-only const char[] type "Unknown" which compiles due to the rules of pointer decay.
Using bare pointers for class members also means you need to write constructors, a destructor, and an assignment operator correctly. If you use std::string then you can rely on the compiler-generated ones.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bathsheba |
