Когда я запускаю свой код, я получаю ошибку сегментации при вызове конструктора по умолчанию, но не каждый раз, когда я его вызываю. Это дает сбой, когда я пытаюсь динамически выделить новую память после перегрузки оператора +. Я знаю, что сбои сегментов возникают из-за неправильного использования указателей, но я вызываю def. конструктор предшествует в коде, и он работает.
здесь - это репозиторий git с основным заголовком и заголовком, если это необходимо:
#include <iostream>
#include "proj1-5-MyString.h"
#include <cmath>
using namespace std;
MyString::MyString() {
capacity = 10;
size = 0;
data = new char[capacity];
data[size] = '\0';
}
//Constructor
MyString::MyString(const char *input) {
this->capacity = 10;
this->size = 0;
this->data = new char[this->capacity];
for (int count = 0; input[count] != '\0'; count++) {
if (count >= this->capacity) {
this->capacity *= 2;
char *temp = new char[this->capacity];
for (int i = 0; i < this->size; i++) {
temp[i] = this->data[i];
}
delete[] this->data;
this->data = temp;
delete[] temp;
}
this->data[count] = input[count];
this->size = count + 1;
}
while ((double) this->size < 0.25 * ((double) (this->capacity))) {
this->capacity = (int)floor(this->capacity / 2);
}
this->data[this->size + 1] = '\0';
}
//Constructor with an initialization character string
MyString::~MyString() {
delete[] this->data;
data = NULL;
}
//Destructor
MyString::MyString(const MyString &that) {
this->data = new char[this->capacity];
this->capacity = that.capacity;
this->size = that.size;
for(int i = 0; i < size; i++){
this->data[i] = that.data[i];
}
while(this->size < 0.25 * (double)(this->capacity)){
this->capacity = (int)((double)that.capacity / 2.0);
}
}
//Copy constructor
MyString &MyString::operator=(const MyString &input) {
if (this->data != input.data) {
delete[] this->data;
this->capacity = input.capacity;
this->size = input.size;
int charCount = 0;
for ( charCount; charCount < input.size; charCount++) {
if (charCount >= this->capacity) {
this->capacity *= 2;
char *temp = new char[this->capacity];
for (int j = 0; j < charCount; j++) {
temp[j] = this->data[j];
}
delete[] this->data;
this->data = temp;
delete[] temp;
}
this->data[charCount] = input.data[charCount];
this->size = charCount + 1;
}
}
return *this;
}
//Overloaded assignment operator, make a copy of MyString object
bool MyString::operator==(const MyString &otherString) const {
int i = 0;
bool same = true;
if (this->size == otherString.size) {
while (i < otherString.size) {
if (this->data[i] == otherString.data[i]) {
same = i <= size;
} else {
same = false;
}
i++;
}
} else {
same = false;
}
return same;
}
//overloaded equivalence relational operator
char &MyString::operator[](int val) {
return this->data[val];
}
//overloaded [ ] should return a char by reference
void MyString::operator+=(const MyString &otherStr) {
*this = *this + otherStr;
}
//overloaded += operator, use to concatenate two MyStrings
MyString MyString::operator+(const MyString &otherStr) const {
// SEGFAULT ЗДЕСЬ
MyString doubleString;
cout << doubleString.capacity << endl;
while (doubleString.capacity < (this->size + otherStr.size)) {
doubleString.capacity *= 2;
}
for (int i = 0; i < max(this->size, otherStr.size); i++) {
doubleString.data[i] = this->data[i];
doubleString.data[i + this->size] = otherStr.data[i];
}
doubleString.size = this->size + otherStr.size;
return doubleString;
}
//Create a new MyString object that is the concatenation of two MyString
//objects
void MyString::getline(istream &in, char delimit) {
delimit = '\n';
int size = 10;
char buffer[size];
int charNum = 0;
while (in.get(buffer[charNum++])) {
if (buffer[charNum - 1] == delimit) {
break;
}
if (charNum >= size) {
char *temp = new char[size * 2];
for (int j = 0; j < size; j++) {
temp[j] = data[j];
}
delete[] data;
data = temp;
delete[] temp;
}
}
}
//reads an entire line from a istream. Lines are terminated with delimit
//which is newline ‘\n’ by default
int MyString::length() const {
return this->size;
}
//return the length of the string
ostream &operator<<(ostream &out, MyString &stringWord) {
for (int i = 0; i < stringWord.size; i++) {
out << stringWord.data[i];
}
return out;
}
//overloaded insertion operator
Например, если у вас есть этот код:
MyString s0 = "ABC";
MyString s1 = "DEF";
auto s2 = s0 + s1;
Первая проблема в конструкторе MyString
. Ваши строки не завершены \0
.
Однако это можно легко исправить:
MyString::MyString(const char *input)
{
this->capacity = 10;
this->size = 0;
//this->data = new char[this->capacity]; //<- Old
this->data = new char[this->capacity](); //<- New
Есть несколько мест, где вы не инициализируете выделенный массив символов. Пожалуйста, проверьте свой код и сделайте это. Либо через конструктор char()
, либо другими способами (std::fill
, memset
, ...).
Пожалуйста, не забудьте отладить свой собственный код, прежде чем размещать вопрос. Вы можете легко обнаружить это в отладчике.
можно выложить небольшую функцию, воспроизводящую эту проблему? хотя бы иногда? также, каково значение
this
, когда вы получаете segfault?