new对象和delete

This commit is contained in:
朱毅骏 2021-08-20 15:50:26 +08:00
parent 4fbd7fa035
commit 403da0d718
6 changed files with 285 additions and 3 deletions

67
base/new/placenew.cpp Normal file
View File

@ -0,0 +1,67 @@
//
// Created by zhuyijun on 2021/8/20.
//
#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;
class JustTesting {
private:
string words;
int number;
public:
JustTesting(const string &s = "JustTesting", int n = 0) {
words = s;
number = n;
cout << words << " constructed" << endl;
}
~JustTesting() {
cout << words << " destroyed" << endl;
}
void Show() const {
cout << words << ", " << number << endl;
}
};
int main() {
char *buffer = new char[BUF];
JustTesting *pc1, *pc2;
pc1 = new(buffer)JustTesting;
pc2 = new JustTesting("Heap1", 20);
cout << "Memory block addresses:\n" << "buffer: " << (void *) buffer << " heap: " << pc2 << endl;
cout << "Memory contents: \n";
cout << pc1 << ": ";
pc1->Show();
cout << pc2 << ": ";
pc2->Show();
JustTesting *pc3, *pc4;
// pc3 = new(buffer)JustTesting("Bad Idea",6);
pc3 = new(buffer + sizeof(JustTesting))JustTesting("Bad Idea", 6);
pc4 = new JustTesting("Heap2", 10);
cout << "Memory contents: \n";
cout << pc3 << ": ";
pc3->Show();
cout << pc4 << ": ";
pc4->Show();
//free Heap1
delete pc2;
//free Heap2
delete pc4;
//调用析构函数
pc3->~JustTesting();
pc1->~JustTesting();
//free buffer
delete[] buffer;
cout << "Dome\n";
return 0;
}

54
base/new/saystring.cpp Normal file
View File

@ -0,0 +1,54 @@
//
// Created by zhuyijun on 2021/8/20.
//
#include "string.h"
const int ArSize = 10;
const int MaxLen = 81;
int main() {
using std::cout;
using std::cin;
using std::endl;
String name;
cout << "Hi, what's your name?\n>> ";
cin >> name;
cout << name << ", Please enter up to " << ArSize << "shart sayings < empty line to quit:" << endl;
String sayings[ArSize];
char temp[MaxLen];
int i;
for (i = 0; i < ArSize; ++i) {
cout << i + 1 << ": ";
cin.get(temp, MaxLen);
while (cin && cin.get() != '\n')
continue;
if (!cin || temp[0] == '\0')
break;
else
sayings[i] = temp;
}
int total = i;
if (total > 0) {
cout << "Here are your sayings: \n";
for (i = 0; i < total; ++i) {
cout << sayings[i][0] << ": " << sayings[i] << endl;
}
int shortest = 0;
int first = 0;
for (i = 1; i < total; ++i) {
if (sayings[i].length() < sayings[shortest].length())
shortest = i;
if (sayings[i] < sayings[first])
first = i;
}
cout << "Shortest saying:\n" << sayings[shortest] << endl;
cout << "First alphabetically: \n" << sayings[first] << endl;
cout << "This program used " << String::HowMany() << " String object. Bye. " << endl;
} else
cout << "No input ! Bye." << endl;
return 0;
}

86
base/new/string.cpp Normal file
View File

@ -0,0 +1,86 @@
//
// Created by zhuyijun on 2021/8/20.
//
#include <cstring>
#include "string.h"
using std::cin;
using std::cout;
using std::endl;
int String::num_strings = 0;
int String::HowMany() {
return num_strings;
}
String::String() {
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;
}
String::String(const char *s) {
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
}
String::~String() {
--num_strings;
delete[] str;
}
String &String::operator=(const String &st) {
if (this == &st) {
return *this;
}
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
String &String::operator=(const char *s) {
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
char &String::operator[](int i) {
return str[i];
}
const char &String::operator[](int i) const {
return str[i];
}
bool operator<(const String &st, const String &st2) {
return (std::strcmp(st.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2) {
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator==(const String &st, const String &st2) {
return (std::strcmp(st.str, st2.str) == 0);
}
std::ostream &operator<<(std::ostream &os, const String &st) {
os << st.str;
return os;
}
std::istream &operator>>(std::istream &is, String &st) {
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is) st = temp;
while (is && is.get() != '\n') continue;
return is;
}

57
base/new/string.h Normal file
View File

@ -0,0 +1,57 @@
//
// Created by zhuyijun on 2021/8/20.
//
#ifndef BASE_STRING_H
#define BASE_STRING_H
#include <iostream>
using std::ostream;
using std::istream;
class String {
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length() const {
return len;
}
//运算符重载
String &operator=(const String &);
String &operator=(const char *);
char &operator[](int i);
const char &operator[](int i) const;
//友元
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream &operator<<(ostream &os, const String &st);
friend istream &operator>>(istream &is, String &st);
//静态函数
static int HowMany();
};
#endif //BASE_STRING_H

View File

@ -33,6 +33,17 @@ StringBad::~StringBad() {
delete[] str;
}
StringBad &StringBad::operator=(const StringBad &st) {
if (this == &st) {
return *this;
}
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
std::ostream &operator<<(std::ostream &os, const StringBad &st) {
os << st.str;
return os;

View File

@ -4,19 +4,26 @@
#ifndef BASE_STRINGBAD_H
#define BASE_STRINGBAD_H
#include <iostream>
class StringBad {
private:
char * str;
char *str;
int len;
static int num_strings;
public:
StringBad(const char * s);
StringBad(const char *s);
StringBad();
~StringBad();
friend std::ostream & operator<<(std::ostream & os,const StringBad & st);
StringBad &operator=(const StringBad &);
friend std::ostream &operator<<(std::ostream &os, const StringBad &st);
};