HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

C++ String class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classstringstackoverflow

Problem

I made a string class to be used as a replacement for std::string. It has all the find functions (find, rfind, find_first_of, ...), basic iterators (just begin and end, no reverse iterators), almost all the operators (+, +=, ==, [], <<), an insert, erase and substring function, c_str(), and it is dynamic.

It is mostly faster than std::string. It allocates the characters on the heap (malloc, realloc, free), and it shouldn't have any undefined behaviour.

I have to know if the memory management is optimal or if some undefined behaviour is present.

String.h

```
#pragma once
#include
#include
#include

class String
{
private:
char* str;
size_t length;
size_t capacity;

public:
typedef char* iterator;

const short int npos = -1;

String(int size = 0)
{
length = 0;
capacity = size;
str = (char)malloc((capacity + 1) sizeof(char));
};

String(char* string)
{
length = strlen(string);
capacity = length;
str = (char)malloc((length + 1) sizeof(char));
memcpy(str, string, (length + 1) * sizeof(char));
}

String(const String &string)
{
str = (char)malloc((string.length + 1) sizeof(char));
length = string.length;
capacity = string.capacity;
memcpy(str, string.str, length * sizeof(char));
}

~String()
{
free(str);
}

const char* begin()
{
return str;
}

const char* end()
{
return str+length;
}

char front()
{
return *str;
}

char back()
{
return str[length];
}

char& operator[](int n)
{
return str[n];
}

friend String& operator+(String lhs, String &rhs)
{
lhs += rhs;
return lhs;
}

String& operator+=(String &right)
{
*this += right.str;
return *this;
}

String& operator+=(char* right)
{
size_t toAdd = strl

Solution

You claim that your String is faster than std::string I do not believe this claim. Please post your benchmark code.

std::string often has Small String Optimisation to avoid dynamic memory allocation when used on small strings which is fairly common.

Your code is not const correct. For example:

const char* c_str()


should be:

const char* c_str() const


and:

bool operator==(String &right)


should be:

bool operator==(const String &right) const


This is not the correct signature for operator +:

friend String& operator+(String lhs, String &rhs)


because it allows the following to compile:

String a,b;
a + b = "foo";


It should be:

friend String operator+(const String& lhs, const String& rhs)


Do not repeat yourself, your copy constructor and assignment operator share a lot of code. You can implement copy constructor like this:

String(const String& s)
    : String() // Initialise to empty string
{
    *this = s;
}


You should also implement move constructor and move assignment operator.

You should also used std::vector as the backing store, this way you don't need the size or capacity members. Your code will simplify a lot and you will not need to worry about manually keeping track of the memory. Using malloc/free in a C++ program is a code smell.

But all in all, the above is pretty pointless because you really should use std::string and save yourself the trouble of maintaining and coding your own string class.

Code Snippets

const char* c_str()
const char* c_str() const
bool operator==(String &right)
bool operator==(const String &right) const
friend String& operator+(String lhs, String &rhs)

Context

StackExchange Code Review Q#150733, answer score: 11

Revisions (0)

No revisions yet.