initializing char* a = new char[size] does not work - c++

I have a problem about initializing char* a = new char[size]. Here is my code.
class Practice
{
public:
Practice(const char* a);
~Practice();
const char* getString() const;
private:
char* mString;
int mSize;
};
#include "Practice.h"
Practice::Practice(const char * a)
:mSize(0)
,mString(nullptr)
{
while (a[mSize] != '\0')
{
mSize++;
}
mString = new char[mSize];
for (int i = 0; i < mSize; i++)
{
mString[i] = a[i];
}
}
Practice::~Practice()
{
delete[] mString;
}
const char* Practice::getString() const
{
return mString;
}
int main()
{
Practice p("Hello");
std::cout << p.getString() << std::endl;
return 0;
}
I expected the result is Hello.
But the result was like Hello²²²²▌▌▌▌▌▌▌■a%{▌.
I thought I initialized the mString member variable through mString = new char[mSize]. But it was not working the way I thought.
Can anybody enlighten me what's wrong with my code and fix it?

mString is not NUL-terminated.

Your're checking a constructor parameter for \0. But you are not allocating mString to put the same \0 there, and don't copy \0 from a.
So, resulting mString is not properly NULL-terminated and it would be read beyond end.
Reading beyond end of allocated is undefined behavior. In particular case, it is likely that either mString would output until some accidental zero, or crash would occur.

while (a[mSize] != '\0')
{
mSize++;
}
This sets mSize equal to the length of a, excluding the terminating '\0'. You should include the terminator in your copy:
while (a[mSize++])
{
}
or simply:
mSize = strlen(a) + 1;

You are not null-terminating your mString data, but it is expecting to be null-terminated when you pass it to std::cout. Without that terminator, std::cout reads into surrounding memory until it encounters a random null byte (or crashes with a read access error). That is why you are seeing std::cout output random garbage after your data.
You are also not following the Rule of 3/5/0, as you are missing a default constructor, copy and move constructors, and copy and move assignment operators.
Try this:
class Practice
{
public:
Practice(const char* a = nullptr);
Practice(const Practice &src);
Practice(Practice &&src);
~Practice();
Practice& operator=(Practice src);
const char* getString() const;
private:
char* mString;
int mSize;
};
#include "Practice.h"
#include <utility>
Practice::Practice(const char * a)
: mSize(0)
, mString(nullptr)
{
if (a)
{
while (a[mSize] != '\0')
{
++mSize;
}
}
mString = new char[mSize + 1];
for (int i = 0; i < mSize; ++i)
{
mString[i] = a[i];
}
mString[mSize] = '\0';
}
Practice::Practice(const Practice &src)
: mSize(src.mSize)
, mString(nullptr)
{
mString = new char[mSize + 1];
for (int i = 0; i < mSize; ++i)
{
mString[i] = src.mString[i];
}
mString[mSize] = '\0';
}
Practice::Practice(Practice &&src)
: mSize(src.mSize)
, mString(src.mString)
{
src.mString = nullptr;
src.mSize = 0;
}
Practice::~Practice()
{
delete[] mString;
}
Practice& Practice::operator=(Practice src)
{
std::swap(mString, src.mString);
std::swap(mSize, src.mSize);
return *this;
}
const char* Practice::getString() const
{
return mString;
}
#include <iostream>
#include "Practice.h"
int main()
{
Practice p("Hello");
std::cout << p.getString() << std::endl;
return 0;
}

Related

Why do I keep getting a segmentation fault with my delete function

I have an assignment where I need to use an overloaded destructor to delete dynamically allocated pointers. However, when it runs, some of my pointers are deleted until a segmentation fault with one of my objects' pointers the one pointing to " and the second" made with a parameterized constructor.
I have tried to go through and make sure the delete operator had brackets (because my new operator did) I made sure the object still existed by printing out its information and address. I have tried to rewrite my allocation function, and I have tried to go over my destructor to see where it messes up.
If it helps, I have included my destructor, my allocation function, my deallocation function, and my parameterized constructor.
'''
//Destructor
MyString::~MyString()
{
buffer_deallocate();
};
void MyString::buffer_deallocate() {
cout << m_buffer << endl;
delete[](m_buffer);
m_buffer = NULL;
m_size = 0;
}
void MyString::buffer_allocate(size_t size) {
try {
m_buffer = new char[size];
m_size = size;
}
catch(bad_alloc&)
{
cout << "Errror: Unable to allocate memory" << endl;
buffer_deallocate();
}
}
//Parameterized Constructor
MyString::MyString(const char * str)
:m_size(0)
{
const char * strPtr = str;
while(*strPtr)
{
strPtr++;
m_size++;
}
buffer_allocate(m_size);
for(int i = 0; i < m_size; i++)
{
m_buffer[i] = str[i];
}
};
'''
Every time however I get the output after "and the second" to be
Segmentation fault (core dumped)
Edit: I have tried the majority of what was recommended. At least what I understood, the problem still persists, and I realize now I was kind of sparse on my code. (Please forgive me, I'm still learning.) Here is the new code along with the rest of the function file for reference:
'''
#include<iostream>
#include<string.h>
using namespace std;
#include"MyString.h"
//Default Constructor
MyString::MyString()
:m_size(0), m_buffer(NULL)
{
buffer_allocate(0);
};
//Parameterized Constructor
MyString::MyString(const char * str)
:m_size(strlen(str)+1), m_buffer(NULL)
{
buffer_allocate(m_size);
strncpy(m_buffer, str, m_size);
};
//Copy Constructor
MyString::MyString(const MyString & other)
:m_size(0), m_buffer(NULL)
{
const char * otherPtr = other.c_str();
buffer_allocate(other.size());
for(int i = 0; i < size(); i++)
{
m_buffer[i] = otherPtr[i];
}
m_buffer[m_size] = '\0';
};
//Destructor
MyString::~MyString()
{
buffer_deallocate();
};
size_t MyString::size() const
{
return m_size;
}
size_t MyString::length() const{
return m_size-1;
}
const char * MyString::c_str() const{
return m_buffer;
}
bool MyString::operator==(const MyString & other) const {
char * m_bufferPointer = m_buffer;
while(*m_bufferPointer++)
{
const char * str_ptr = other.c_str();
if(*m_buffer != *str_ptr++)
{
return 0;
}
}
return 1;
}
MyString & MyString::operator=(const MyString & rhs) {
buffer_deallocate();
buffer_allocate(rhs.size());
const char * c_strPtr = rhs.c_str();
int i;
for(i = 0; i < rhs.size(); i++)
{
this->m_buffer[i] = c_strPtr[i];
}
return *this;
}
MyString MyString::operator+ (const MyString & other_myStr) const {
char * temp_pointer;
temp_pointer;
size_t temp_size = m_size + other_myStr.size();
//New Combined Buffer for Concatanation
try {
temp_pointer = new char[temp_size];
temp_pointer = strcat(this->m_buffer, other_myStr.c_str());
}
catch(bad_alloc&)
{
cout << "Error: Unable to Allocate Memory";
return NULL;
}
return MyString(temp_pointer);
}
char & MyString:: operator[](size_t index) {
return m_buffer[index];
}
const char & MyString::operator[] (size_t index) const {
return m_buffer[index];
}
ostream & operator << (ostream& os, const MyString & myStr) {
os << myStr.m_buffer;
return os;
}
void MyString::buffer_deallocate() {
cout << "Trying to delete : " <<m_buffer << endl;
if(m_buffer){
delete[](m_buffer);
}
cout << " Success" <<endl;
m_buffer = NULL;
m_size = 0;
}
void MyString::buffer_allocate(size_t size) {
try {
m_buffer = new char[size];
m_size = size;
}
catch(bad_alloc&)
{
cout << "Errror: Unable to allocate memory" << endl;
m_size = 0;
}
}
'''
in MyString::buffer_deallocate
cout << m_buffer << endl;
requires that m_buffer be null-terminated. Unfortunately, MyString::MyString(const char * str) does not make this guarantee.
You could
for(int i = 0; i < m_size; i++)
{
cout << m_buffer[i] << endl;
}
to print the string out character by character instead, but it is probably more useful to waste a byte, null terminate, and take advantage of the Standard library
MyString::MyString(const char * str)
:m_size(0)
{
const char * strPtr = str;
while(*strPtr)
{
strPtr++;
m_size++;
}
buffer_allocate(m_size);
for(int i = 0; i < m_size; i++)
{
m_buffer[i] = str[i];
}
m_buffer[m_size] = '\0'; // add the null
}
and then
void MyString::buffer_allocate(size_t size) {
try {
m_buffer = new char[size+1]; // +1 for the null terminator
m_size = size;
}
catch(bad_alloc&) // this is a bad thing to do here. More on that later.
{
cout << "Errror: Unable to allocate memory" << endl;
buffer_deallocate();
}
}
But we can simplify this with a few library function calls.
MyString::MyString(const char * str)
:m_size(strlen(str))
{
buffer_allocate(m_size);
strcpy(m_buffer, str);
}
Addendum:
Your class may be violating the Rule of Three. If MyString does not have a copy constructor and an assignment operator to go along with the destructor any copies, deliberate or accidental, will turn a MyString into a time bomb. The destructor of one of the copies will be run before the others leaving the others without a valid m_buffer.
MyString::buffer_allocate cannot safely return void unless it allows the exception to propagate. Catching the bad_alloc leaves the object without a valid allocation at m_buffer and the rest of the program will not know this. Either every other access in the program must test for a valid buffer or engage in undefined behaviour trying to access the invalid memory. It's likely better to let the exception fall through and be caught by another part of the program that is better suited to making a decision on what to do.
MyString::buffer_allocate will leak an existing allocation if called on a MyString that already has a valid allocation at m_buffer. I recommend a
if (m_buffer)
{
delete[] m_buffer;
}
and initializing m_buffer to null in MyString's constructor
MyString(const char* str)
: m_size(std::strlen(str)), m_buffer(nullptr)
{
buffer_allocate(m_size);
std::strncpy(m_buffer, str, m_size);
}
So I had to remake this to some working code.
The problem might be that a string length must be extended with 1 to add the nul-termination. So that makes:
// prevent some MSVS warnings->errors
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
//Destructor
class MyString {
private:
size_t m_size;
char* m_buffer;
void buffer_allocate(size_t size) {
try {
m_buffer = new char[size];
m_size = size;
}
catch (std::bad_alloc&)
{
std::cout << "Errror: Unable to allocate memory\n";
// allocation failed, nothing changed, don't delete/
m_size = 0;
}
}
void buffer_deallocate() {
// printing in the buffer deallocation??
delete[] m_buffer;
m_buffer = nullptr;
m_size = 0;
}
public:
MyString(const char* str)
: m_size(std::strlen(str)+1) // add null termination
, m_buffer(nullptr)
{
buffer_allocate(m_size);
std::strncpy(m_buffer, str, m_size);
}
~MyString() {
buffer_deallocate();
}
void Print() const {
std::cout << m_buffer << '\n';
}
};
int main() {
std::string input{ "Hello World!" };
MyString myString(input.c_str());
myString.Print();
}

Trying to write my own string class getting exception in gcc

Basically what the title says. I have been trying to write my own string class using only char arrays and, while my code works when I run it in Visual Studio, I have trouble with it when using gcc .The problem seems to be coming when i try to delete in my getData function(can be seen below) The exception I get is:
Exception thrown at 0x6262436B (ucrtbased.dll) in string.exe: 0xC0000005: Access violation reading location 0xCCCCCCBC. occurred
My code :
Header:
#pragma warning(disable:4996)
#ifndef STRING_STRING_H
#define STRING_STRING_H
#include<iostream>
#include<cstring>
#include<fstream>
class String {
private:
char *data; //holds the text
size_t maxSize; //maximum number of chars in data
size_t currentSize; //current number of chars in data
void getData(const char *, size_t maxSize); //sets currentSize to the other char* size and
// copies the content of the other char* to data
public:
String(); //default constructor
~String(); //destructor
String(const String &); //copy-constructor(from String)
String(const char *); //copy-constructor(from char*)
String operator=(const String &); //operator= (from string)
String operator=(const char *); //operator=(from char*)
size_t length() const; //currentSize getter
void addChar(const char); //adds a char to the data array
void getLine(std::ifstream&,const char); // reads line till deliminator and stores it in this string object(all data previously stored is lost)
size_t find(const char*); //searches for text in the string and if found returns the starting position , if not found returns -1;
void print() const; //prints the string object to console
char* toChar() const; //returns a new allocated char pointer with the text inside (must be deleted afterwards)
};
#endif //STRING_STRING_H
cpp:
#include "String.h"
String::String() {
currentSize = 0;
maxSize = 16;
try {
data = new char[maxSize];
data[0] = '\0';
}
catch (std::bad_alloc &) {
std::cerr << "Not enough memory" << std::endl;
throw;
}
}
String::~String() {
delete[] data;
}
size_t String::length() const {
return currentSize;
}
String::String(const String &other) {
this->maxSize = other.maxSize;
getData(other.data, maxSize);
}
String::String(const char *other) {
this->maxSize = strlen(other) *2;
getData(other, maxSize);
}
void String::getData(const char *dataSource, size_t maxSize) {
currentSize = strlen(dataSource);
try {
char *newData = new char[maxSize];
delete[] data;
data = newData;
strcpy(data, dataSource);
}
catch (std::bad_alloc &) {
std::cerr << "Not enough memory" << std::endl;
throw;
}
}
String String::operator=(const String &other) {
if (this != &other) {
maxSize = other.maxSize;
getData(other.data, maxSize);
}
return *this;
}
String String::operator=(const char *other) {
if (this->data != other) {
maxSize = strlen(other) *2;
getData(other, maxSize);
}
return *this;
}
void String::addChar(const char newChar) {
if (maxSize == currentSize+1) {
maxSize *= 2;
getData(this->data, maxSize);
}
data[currentSize++] = newChar;
}
void String::getLine(std::ifstream & is, const char delim='\n')
{
char temp;
while (!is.eof())
{
is.get(temp);
if (temp == delim)
break;
else
addChar(temp);
}
return;
}
size_t String::find(const char * text)
{
size_t currPos=-1;
bool found = 0;
for (size_t i = 0; i < currentSize; i++)
{
if (data[i] == text[0])
{
for (size_t j = i+1; j < currentSize; j++)
{
if (data[j] == text[j - i])
found = 1;
else
{
found = 0;
break;
}
}
if (found == 1)
{
currPos = i;
break;
}
}
}
return currPos;
}
void String::print() const
{
for (size_t i = 0; i < currentSize; i++)
{
std::cout << data[i];
}
std::cout << std::endl;
}
char * String::toChar() const
{
char* text= new char[currentSize+1];
for (size_t i = 0; i < currentSize; i++)
{
text[i] = data[i];
}
text[currentSize + 1] = 0;
return text;
}
Your problem is caused by calling delete [] on uninitialized memory.
In the copy constructor, the data member is not initialized before calling getData(). In getData(), you are using delete [] data;.
You could initialize data to nullptr in the constructors to avoid the problem.
It's always a good idea to initialize all variables to some sensible value before the body of the constructor. E.g. you can implement the copy constructor as:
String::String(const String &other) : currentSize(0),
maxSize(other.maxSize),
data(nullptr)
{
getData(other.data, maxSize);
}
Similarly, implement the constructor from char const* as:
String::String(const char *other) : currentSize(0),
maxSize(strlen(other) *2),
data(nullptr)
{
getData(other, maxSize);
}

_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) after "return 0"

I'm writing a program where I have to write my own string class and a potion class. I know that this run-time error is usually caused when the program is deleting something that wasn't allocated, but it occurs after the 'return 0' in main. I've debugged it line-by-line and tried some things to try and eliminate the run-time error, but nothing has really worked. Is there anybody that can help me out with this?
Here is my code:
//Main
#include "Potion.h"
#include <iostream>
using std::cout;
using std::endl;
void TotalCost( Potion ArrayofPotions[5] )
{
String Currency[4] = {"Platinum", "Gold", "Silver" ,"Copper"};
int TotalCost[4] = {0, 0, 0, 0};
int Cost[4] = {0, 0, 0, 0};
for (short i = 0; i < 5; i++)
{
for (short k = 0; k < 4; k++)
{
Cost[k] = ArrayofPotions[i].ConvertCost(k);
TotalCost[k] += Cost[k];
if ( i != 0 )
TotalCost[k] = ArrayofPotions[k].CalculateCost(TotalCost[k - 1], TotalCost[k]);
}
}
cout << "\nTotal cost for all potions: ";
for (short i = 0; i < 4; i++)
{
cout << TotalCost[i] << ' ';
Currency[i].Display();
}
}
int main()
{
Potion Haggling("Potion of Haggling", "You haggle for 10% better prices for 30 seconds",
"Weak", "0.80.0.4.");
Potion Strength("Draught of Strength", "You can carry 20% more for 300 seconds",
"Low", "2.60.5.1.");
Potion Health("Solution of Health", "You are 30% tougher for 60 seconds",
"Mild", "2.20.5.1.");
Potion Stealth("Philter of Stealth", "You are 40% harder to detect for 60 seconds",
"Moderate", "0.90.5.1.");
Potion Waterbreathing("Elixir of Waterbreathing", "You can breathe underwater for 60 seconds",
"Strong", "2.10.5.0.");
Potion ArrayOfPotions[5] = {Haggling, Strength, Health, Stealth, Waterbreathing};
for (short i = 0; i < 5; i++)
ArrayOfPotions[i].DisplayPotions();
TotalCost(ArrayOfPotions);
system("pause");
return 0;
}
//String class
class String
{
public:
String() : m_str(nullptr)
{ }
String(char chr) : m_str(nullptr)
{
m_str = new char;
*m_str = ch;
}
String(char * str)
{
if (str != nullptr)
{
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
}
String(const String & copy) : m_str(copy.m_str)
{ }
String& operator=(const String & rhs)
{
if ( this != &rhs )
{
delete [] m_str;
m_str = new char[strlen(rhs.m_str) + 1];
strcpy(m_str, rhs.m_str);
}
return *this;
}
~String()
{
delete [] m_str;
}
void Display() const
{
cout << m_str;
}
char * GetStr() const
{
return m_str;
}
String Upper()
{
char * check = this->m_str;
for (unsigned short i = 0; i < strlen( this->m_str ); i++, check++)
{
if ( *check > 96 && *check < 123 )
{
*check -= 32;
}
else
m_str = this->m_str;
}
return m_str;
}
private:
char * m_str;
};
//Potion class
#include "String.h"
class Potion
{
public:
Potion() : m_name(nullptr), m_description(nullptr), m_potency(nullptr),
m_cost(nullptr)
{ }
Potion(String name, String description, String potency, String cost)
{
m_name = name;
m_description = description;
m_potency = potency.Upper();
m_cost = cost;
}
Potion(const Potion & copy) : m_name(copy.m_name), m_description(copy.m_description),
m_potency(copy.m_potency), m_cost(copy.m_cost)
{ }
int ConvertCost(int index)
{
int cost = 0;
char * new_cost = m_cost.GetStr();
char * temp_string = new char[strlen( new_cost ) + 1];
strcpy(temp_string, new_cost);
char * temp = strtok( temp_string, "." );
for (short k = 0; k != index; k++)
temp = strtok( NULL, "." );
cost = atoi( temp );
delete [] temp_string;
return cost;
}
int CalculateCost(int & cost1, int cost2)
{
if (cost > 99)
{
cost1++;
cost2 -= 100;
}
return cost2;
}
void DisplayPotions()
{
String Currency[4] = {"Platinum", "Gold", "Silver" ,"Copper"};
int cost = 0;
m_name.Display();
m_description.Display();
m_potency.Display();
for (short i = 0; i < 4; i++)
{
cost = ConvertCost(i);
if (cost != 0)
{
cout << ConvertCost(i) << ' ';
Currency[i].Display();
}
}
}
private:
String m_name;
String m_description;
String m_potency;
String m_cost;
};
I'm sorry it's really long and I left out some of the output formatting, but I really need some help with this.
P.S. My professor wants us to convert the cost from a string to an int and it has to be put in that "0.0.0.0" format. That's why it is written like that.
Your assignment operator is broken, as it copies the pointer from the right hand side.
You need to follow the same procedure as in the copy constructor.
By the way, the copy constructor is also broken, as it will fail if rhs is empty.
(It's a safer idea to use an empty string in your representation of an empty string (i.e. use "" instead of nullptr).
It saves you from a lot of null checking.)
This constructor is also broken:
String(char * str)
{
if (str != nullptr)
{
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
}
as it leaves m_str uninitialised if you pass it nullptr - and you do that a lot.
This constructor is also broken:
String(char chr) : m_str(nullptr)
{
m_str = new char;
*m_str = ch;
}
as it doesn't construct a zero-terminated string.
You need
m_str = new char[2];
m_str[0] = ch;
m_str[1] = 0;
As others stated, your constructors are broken. In addition to this, the assignment operator is also broken in a subtle way.
The easiest way to fix this is move most of the assignment operator code to the copy constructor.
String(const String & copy) : m_str(new char[strlen(copy.m_str)+1])
{
strcpy(m_str, copy.m_str);
}
As to your assignment operator, the flaw is it deletes the memory before new[] is called. What happens if new[] throws an exception? You've destroyed your old data and you have no way to recover the data.
Instead of how you wrote the assignment operator, a better way is this:
#include <algorithm>
//...
String& operator=(String rhs)
{
std::swap(rhs.m_str, m_str);
return *this;
}
That was simple, and it works. It requires a working copy constructor, and working destructor for String. Since you provided these two functions, writing the assignment operator becomes trivial.
See the copy/swap idiom: What is the copy-and-swap idiom?
This also ensures that if new[] throws an exception, your original string is not destroyed. The std::swap merely swaps the two items -- if you can't use std::swap then write your own function to swap the pointers, as it should be very simple to implement.

C++ string implementation error

I am trying to implement a basic string class in C++. However, I am stuck on the append() function. It would like it to be Hello World, but it results as Hello ÍWorlýýýý««««««««þ:
#define __START namespace lib{
#define __END }
__START
class string
{
public:
string(const char* s)
{
_str = s;
}
const char operator[](int position)
{
return _str[position];
}
void operator=(string s)
{
_str = s.c_str();
}
void append(string s)
{
size_t nlength = (length() + s.length());
char* nstr = new char[nlength];
for (int i = 0; i < length(); ++i)
{
nstr[i] = _str[i];
}
for (int i = length() + 1; i < nlength; ++i)
{
nstr[i] = s[(i - length() - 1)];
}
_str = const_cast<const char*>(nstr);
}
void operator+=(string s)
{
append(s);
}
const char* c_str()
{
return _str;
}
size_t length()
{
return strlen(_str);
}
std::string str()
{
return _str;
}
private:
const char* _str;
};
__END
int main()
{
lib::string s = "Hello ";
s.append("World"); // s += "World";
std::cout << s.c_str();
getchar();
}
There are a lot of errors, not only with append
string(const char* s)
{
_str = s;
}
The constructor is wrong, you should make a copy of s in order to free it later, this way:
~string()
{
delete[] _str; // or free(_str) in case you use malloc/realloc, thanks Fred!
}
Private member variable:
private:
const char* _str;
The internal string should not be const, you should be able to resize it later
const char operator[](int position)
{
return _str[position];
}
You are missing a check: length() > position
void operator=(string s)
{
_str = s.c_str();
}
You are not modifying s, it should be const string& s
You are also not copying s.c_str() which means that now s and this are sharing the same internal buffer
void append(string s) // s should be a const reference too
{
size_t nlength = (length() + s.length());
char* nstr = new char[nlength];
for (int i = 0; i < length(); ++i)
{
nstr[i] = _str[i];
}
for (int i = length() + 1; i < nlength; ++i)
{
nstr[i] = s[(i - length() - 1)];
}
_str = const_cast<const char*>(nstr);
}
Should be easier to write in terms of realloc:
void append(string s)
{
int start = length();
_str = realloc(_str, length() + s.length());
for (int i = 0; i < s.length(); i++) {
_str[start+i] = s[i];
}
}
If you want to stick to new its OK, but you must free _str before assigning it to the new one.
The following operators should be const:
const char* c_str() const;
size_t length() const;
std::string str();
Update: Options for the constructor:
// option one (use delete[] to cleanup _str)
string(const char* s) {
int n = strlen(s);
_str = new char[n+1];
memcpy(_str, s, n+1); // s is NULL terminated
}
// option two (use free() to cleanup _str)
string(const char* s) {
int n = strlen(s);
_str = (char*)malloc(n+1);
memcpy(_str, s, n+1); // s is NULL terminated
}
// option 3: rely on append taking a char* argument
string(const char *s) : _str(NULL) {
append(s, strlen(s));
}
..
void append(const string& s) {
append(s.c_str(), s.length())
}
void append(const char *s, int len) {
int start = _str ? length() : 0;
_str = realloc(_str, start + len);
for (int i = 0; i < len; i++) {
_str[start+i] = s[i];
}
}
Update 2: It will be better to use size_t or unsigned int instead of plain int because the size is always greater than or equal to zero.
There are tons of problems with your code, so let's do this step by step.
First, you don't need your preprocessor magic for the namespace, but just a basic namespace mynamespace{}.
Secondly, it is ideal to create a basic_string class, so that it can be used with different character types. char / wchar_t, e.t.c.
Problems with your class:
1) private: const char *_str;. The pointer will be modified, so the const is useless.
2) There are no typedefs. You need them if you are attempting to re-implement a STL class. (Explained in example)
3) Use an allocator. This way you can construct, and destroy elements, allocate, and deallocate memory. You will be certain to keep your memory a lot safer this way.
4) Strings must be null terminated. That means an extra '\0' at the end, meaning you will have to allocate an extra byte for that. Strings are null terminated, because it is a way of telling the code to stop reading the string.
5) You are assigning to a string which has not been allocated. _str = s.c_str(); could easily crash, depending on your compiler, because you are writing to non-allocated memory.
6) Use const references, rather than normal types for your parameters (string = const string &). You also need this for your copy constructor basic_string(const _Myt &).
I still may not have highlighted all of the problems
Example basic_string class
template < typename _Elem, typename _Traits = std::char_traits<_Elem>, typename _Alloc = std::allocator<_Elem> > class basic_string
{
public:
typedef basic_string<_Elem, _Traits, _Alloc> _Myt;
typedef _Elem value_type;
typedef _Traits traits_type;
typedef _Alloc allocator_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type *iterator;
typedef const value_type *const_iterator;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
basic_string()
{
__data = _Alloc().allocate(1);
_Alloc().construct(&__data[0], '\0');
}
basic_string(const_pointer _Init)
{
int count = 0;
__data = _Alloc().allocate(_Traits::length(_Init) + 1);
for (const_iterator i = &_Init[0]; i != &_Init[_Traits::length(_Init)]; ++i, ++count)
{
_Alloc().construct(&__data[count], *i);
}
_Alloc().construct(&__data[_Traits::length(_Init)], '\0');
}
basic_string(const _Myt &_Init)
{
if (this != &_Init)
{
int count = 0;
__data = _Alloc().allocate(_Traits::length(_Init.__data) + 1);
for (const_iterator i = &_Init.__data[0]; i != &_Init.__data[_Traits::length(_Init.__data)]; ++i, ++count)
{
_Alloc().construct(&__data[count], *i);
}
_Alloc().construct(&__data[_Traits::length(_Init.__data)], '\0');
}
else
{
__data = _Alloc().allocate(1);
_Alloc().construct(&__data[0], '\0');
}
}
~basic_string()
{
if (__data)
{
size_type tmp = size();
for (iterator i = begin(); i != end(); ++i)
{
_Alloc().destroy(i);
}
_Alloc().deallocate(__data, tmp);
}
}
_Myt &assign(const_pointer _Rhs)
{
int count = 0;
reserve(_Traits::length(_Rhs) + 1);
for (const_iterator i = &_Rhs[0]; i != &_Rhs[_Traits::length(_Rhs)]; ++i, ++count)
{
_Alloc().construct(&__data[count], *i);
}
_Alloc().construct(&__data[_Traits::length(_Rhs)], '\0');
return *this;
}
_Myt &operator=(const_pointer _Rhs)
{
return assign(_Rhs);
}
_Myt &append(const_pointer _Rhs)
{
int count = size();
reserve(size() + _Traits::length(_Rhs) + 1);
for (const_iterator i = &_Rhs[0]; i != &_Rhs[_Traits::length(_Rhs)]; ++i, ++count)
{
_Alloc().construct(&__data[count], *i);
}
_Alloc().construct(&__data[count], '\0');
return *this;
}
_Myt &operator+=(const_pointer _Rhs)
{
return append(_Rhs);
}
iterator begin()
{
return &__data[0];
}
iterator end()
{
return &__data[size()];
}
size_type size()
{
return _Traits::length(__data);
}
_Myt &swap(basic_string<_Elem> &_Rhs)
{
std::swap(__data, _Rhs.__data);
return *this;
}
void reserve(size_type _Size)
{
int count = 0;
if (_Size < size())
{
return;
}
pointer buf = _Alloc().allocate(_Size);
for (iterator i = begin(); i != end(); ++i, ++count)
{
_Alloc().construct(&buf[count], *i);
}
std::swap(__data, buf);
for (iterator i = &buf[0]; i != &buf[_Traits::length(buf)]; ++i)
{
_Alloc().destroy(i);
}
_Alloc().deallocate(buf, _Traits::length(buf));
}
operator const_pointer()
{
return __data;
}
operator pointer()
{
return __data;
}
template < typename _Traits1, typename _Alloc1 > friend std::basic_ostream<_Elem> &operator<<(std::basic_ostream<_Elem> &_Stream, basic_string<_Elem, _Traits1, _Alloc1> &_Str)
{
return _Stream << _Str.c_str();
}
const_pointer data() const
{
return __data;
}
const_pointer c_str() const
{
return __data;
}
private:
pointer __data;
};
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
Of course, there is still a bit missing, but I'm sure you can implement that with a little online help.
First of all, your most critical problem is in char* nstr = new char[nlength];.
You must change it to char* nstr = new char[nlength+1];.
Then, in function append, after the two for loops, set nstr[nlength] = 0;
Second, for better performance (as well as correct coding), you probably need to change string s to const string& s in the following functions:
void operator=(string s)
void append(string s)
void operator+=(string s)
You have an off-by-one error in the second loop; the second string needs to be copied to length(), immediately after the end of the first:
for (int i = length(); i < nlength; ++i)
{
nstr[i] = s[i - length()];
}
You'll also need to allocate one more byte to put a null terminator on the end.
Note that you don't need that scary-looking cast to add const to the pointer, since that's a perfectly safe thing to do. const_cast is only needed to remove qualifiers. You might also want to fix the memory leak and, for bonus points, cache the length so you don't have to read the entire string every time you want it.
for (int i = length() + 1; i < nlength; ++i)
{
nstr[i] = s[(i - length() - 1)];
}
You first copy chars up to length()-1 and then start back at length() + 1, so you skip a char and there could be anything in there after allocation(unless you do a memset beforehand).
Then you need to terminate your string with a null character(\0) so that the std::string that gets constructed on str() method return knows when to stop reading chars.

Strange program crash

I'm trying to implement a String class for an excercise. Here is my all code: (you don't need to read it all) :
#include <iostream>
using namespace std;
// ******************************* String ************************
class String {
private:
char* char_arr;
int strlen(const char* str) {
int length;
for(length=0;*(str+length)!='\0';length++);
return length;
}
void strCopy(const char* str1, char* str2) {
int length = strlen(str1);
for(int i=0;i<length;i++)
str2[i] = str1[i];
}
public:
// --- Constructor ---
String(const char* str) {
int length = strlen(str);
char_arr = new char[strlen(str)+1];
char_arr[length] = '\0';
strCopy(str,char_arr);
}
// --- size Constructor ---
explicit String(int size) {
char_arr = new char[size+1];
char_arr[size+1] = '\0';
}
// --- Destructor ---
~String() {
delete char_arr;
}
// --- Copy Constructor ---
String(const String& rhs) {
char_arr = new char[strlen(rhs.char_arr)+1];
strCopy(rhs.char_arr,char_arr);
}
// --- copy-assignment Constructor
const String& operator=(const String& rhs) {
delete char_arr;
char_arr = new char[strlen(rhs.char_arr)+1];
strCopy(rhs.char_arr,char_arr);
}
// --- operator== ---
bool operator==(const String& rhs) {
int this_length = strlen(char_arr);
int rhs_length = strlen(rhs.char_arr);
if(this_length==rhs_length) {
bool return_value = true;
for(int i=0;i<this_length;i++) {
if(char_arr[i]!=rhs.char_arr[i]) {
return_value = false;
break;
}
}
return return_value;
}
return false;
}
// --- operator+ ---
String operator+(const String& rhs) {
int this_length = strlen(char_arr);
int rhs_length = strlen(rhs.char_arr);
String new_str(this_length+rhs_length);
strCopy(char_arr,new_str.char_arr);
for(int i=0;i<rhs_length;i++) {
new_str.char_arr[i+this_length] = rhs.char_arr[i];
}
new_str.char_arr[this_length+rhs_length] = '\0';
return new_str;
}
// --- print ---
void print() {
cout << char_arr;
}
};
// ~~~~~~~ main ~~~~~~~~~
int main() {
String s = "This is";
String s1 = " My Name";
String s2 = s+s1;
s1.print();
return 0;
}
The problem is in the operator+ overloading. If you look at the main function and change the values of s and s1, the program sometimes will crash and sometimes it won't. any ideas why it's happens?
Your strCopy does not copy the trailing zero byte. While there is nothing woring with it, it is somewhat counterintuitive.
Your copy constructor uses the above strCopy() and does not add zero byte too, leaving copied string unterminated. The same applies to assignment operator.
Your concatenation operator dos not allocate enough room for trailing zero byte (but appends it). The program crashes because zero byte is placed after the allocated memory block.
Not sure this is the only problem, but you need to return *this in your const String& operator=(const String& rhs). Do enable warnings in the compiler, and it should tell you "reaching end of function without return".