Search code examples
c++arrayspointersdynamic-memory-allocation

Creating a character array with `new` generates more characters than I specify. C++


So I have an assignment for my CS class that's giving me some trouble. The goal is to create my own non-NULL-terminated String class without using any of the cstring functions. The data in the class must contain the Length of the string, a char* called "data", and some other things that aren't related to my question.

So when I go to allocate memory for the string, I call the MyStrCopy(MyString* tar, const char* str) function which uses tar->data = new char[length] to allocate the memory, "length" being the length of the cstring passed in as str, which works as intended. When allocating the memory this way, though, the array is always much larger than I specified (ex. I ask for 6 bytes and I get upwards of 11) and the number of bytes I get seems to be random and it differs per run. I tried writing a function to cull the unwanted characters, but I think I just lack the skill/knowledge as to how I would accomplish that.

This extra data has thrown a couple of my functions off-kilter and I'm stuck as to how to fix it. Any ideas?

I've defined my Class below

#include <iostream>
#pragma once

class MyString {
private:
    char* data;
    int length;
    static bool printAsUppercase;

    int getLengnth(const char* str);
    void MyStrCopy(MyString* tar, const char* str);

public:
// Constructors
    MyString();
    MyString(const char* data);
    MyString(MyString& data2Copy);
    ~MyString();

// Operator Overloads
    // Assignment Operator
    MyString operator=(const MyString& data);
    MyString operator=(const char* data);
    // Arithmetic Operators 
    MyString operator+(const MyString& rightStr);
    // Pre/Post decrement
    MyString operator--();
    MyString operator--(int);
    // Boolean Operators
    friend bool operator==(const MyString& leftStr, const MyString& rightStr);
    friend bool operator>(const MyString& leftStr, const MyString& rightStr);
    friend bool operator<(const MyString& leftStr, const MyString& rightStr);
    // Streaming Operators
    friend std::ostream& operator<<(std::ostream& os, const MyString& str);
    friend std::istream& operator>>(std::ostream& is, MyString& str);
// Mutators
    MyString& uppercase();
    void cull();
// Accessors
    int getLengnth();
};

and here's the implementation. Note: most of this does not currently work as intended.

#include "MyString.h"

// Constructors
MyString::MyString() {
    data = NULL;
    length = 0;
}

MyString::MyString(const char* data) {
    MyStrCopy(this, data);
}

MyString::MyString(MyString& data2Copy) {
    MyStrCopy(this, data2Copy.data);
}

MyString::~MyString() {
    delete[] data;
}

MyString MyString::operator=(const MyString& data) {
    MyString temp;
    MyStrCopy(&temp, data.data);
    return temp;
}

MyString MyString::operator=(const char* data) {
    MyString temp;
    MyStrCopy(&temp, data);
    return temp;
}

void MyString::MyStrCopy(MyString* tar, const char* str) {
    // WIP Something's not right with the NEW line
    tar->length = getLengnth(str);
    if (data != NULL)
        delete data;
    tar->data = new char[length];
    for (int i = 0; i < tar->length; i++)
        tar->data[i] = str[i];
    tar->cull();
}

void MyString::cull() {
    // WIP currently does effectively nothing
    int currLen = getLengnth(data);
    while (currLen > length)
        data[currLen--];
}

int MyString::getLengnth() {
    return length;
}

int MyString::getLengnth(const char* str) {
    int len = 0;
    while (str[len] != NULL)
        len++;
    return len;
}

Thanks in advance!


Solution

  • So as it turns out, the extra characters I was getting in the debugger was the debugger trying to be helpful by continuing to print characters until it found a NULL, which could have been anywhere since I wasn't allowed to Null terminate. I thought there was actually something wrong with my computer, but no it was Just the debugger. Thanks for all of the additional input on this; it really helped me figure some stuff out!