C++ – Realloc()/Resize an object in C++ for a string implementation


When they are represented in memory, are C++ objects the same as C structs?

For example, with C, I could do something like this:

struct myObj {
       int myInt;
       char myVarChar;

int main() {
       myObj * testObj = (myObj *) malloc(sizeof(int)+5);
       testObj->myInt = 3;
       strcpy((char*)&testObj->myVarChar, "test");
       printf("String: %s", (char *) &testObj->myVarChar);

I don't think C++ allows overloading the + operator for the built-in char * type.

So i'd like to create my own lightweight string class which has no extra overhead that std::string has. I think std::string is represented contiguously:

(int)length, (char[])data

I want exactly the same functionality but without prefixing the length (saving 8 bytes overhead).

Here is the code i'm using to test, but it results in a segfault

#include <iostream>
using namespace std;
class pString {
        char c;
        pString * pString::operator=(const char *);

pString * pString::operator=(const char * buff) {

    cout << "Address of this: " << (uint32_t) this << endl;
    cout << "Address of this->c: " << (uint32_t) &this->c << endl;

    realloc(this, strlen(buff)+1);
    memcpy(this, buff,  strlen(buff));
    *(this+strlen(buff)) = '\0';

    return this;

struct myObj {
        int myInt;
        char myVarChar;

int main() {

    pString * myString = (pString *) malloc(sizeof(pString));
    *myString = "testing";
    cout << "'" << (char *) myString << "'";    

Edit: nobody really understands what i want to do. Yes i know i can have a pointer to the string in the class but thats 8 bytes more expensive than a plain cstring, i wanted exactly the same internal representation. Thanks for trying though

Edit: The end result of what i wanted to achieve was being able to use the + operator with NO extra memory usage compared to using strcat etc

const char * operator+(const char * first, const char * second);

Best Solution

You should not waste your time writing string classes - there's a reason people spent time writing them in the first place and it's naive to think they wrote them because they wanted to create big obfuscated and overheaded code that you could easily improve in a matter of hours.

For example your code has quadratic complexity for memory reallocations in the assignment operator - each assignment of a sting greater by 1 character will use a new memory block greater by 1 byte resulting in big memory fragmentation after a "few" assignments like this - you save a few bytes but potentialy lose megabytes to address space and memory page fragmentation.

Also designing this way you have no way of efficiently implementing the += operator as instead of just copying the appended string in most cases you will always need to copy the whole string - thus reaching quadratic complexity again in case you append small strings to one bigger one a few times.

Sorry but your idea looks to have great chances of becoming terrible to maintain and orders of magnitude less efficient then the typical string implementations like std::string.

Don't worry - this is true for practicaly all great ideas of "writing your own better version of a standard container" :)