human header
class human
{
char name[];
public:
void setName(char nameValue[]);
char* getName();
}
human cpp
void human::setName(char nameValue[])
{
char name[] = "walter";
}
char* human::getName()
{
return name;
}
main cpp
int main()
{
human myHuman;
char name[] = "walter";
myHuman.setName(name);
char* result3 = myHuman.getName();
cout << "My human has a name of " << result3 << endl;
return 0;
}
I assign the string "walter" but when I print it I get "╠╠╠╠╦ß╩d└²╒".
I don't understand which part is wrong. Can you tell me what's wrong with my code?
First and foremost you are assigning nameValue to a variable local to the function setName, which means that the class variable name is still unitialized when you return it in getName, hence the strange output.
The char name[]; declaration is also incorrect, C++ does not allow for variable length arrays or arrays with unspecified bounds unless they are immediately initialized in which case the size will be deduced given the size of the assigned string.
On that note, warnings must have been issued by your compiler, if not, crank them up, or better yet, make it treat warnings as errors, you'll have more robust code.
For the purpose of your assignment you should just go ahead and use pointers, a small problem arises because C++ does not allow for assignment of string literals to char* variables, this is easily fixed if you use const char*, your compiler may allow the former but it is illegal as per C++ rules.
Applying the corrections above, your code should look more like this:
class human
{
const char* name;
public:
void setName(const char *nameValue);
const char* getName();
};
void human::setName(const char *nameValue)
{
name = nameValue;
}
const char* human::getName(){
return name;
}
int main()
{
human myHuman;
const char* name = "walter";
myHuman.setName(name);
const char* result3 = myHuman.getName();
cout << "My human has a name of " << result3 << endl;
return EXIT_SUCCESS;
}
I try to read a file decompress it and parse it to a tree in C++. Everything works fine with one excaption: For some reason I can construct a char[] string and use a pointer on it to output it with the help of cout inside a befriended overloaded <<-operator, but can't use the pointer returned by the operator to cout the string. I was thinking it might have to do with visibility of the string-memory, but that makes no sense to me since with "new" allocated memory should be visible to everyone since its on the heap of the program and not somekind of object or class heap. This all sounds rather confusing, let me show you a striped down version of the source and I'm pretty sure everything becomes clear (probably some really stupid mistake on my end... but I try since two days and don't find the mistake):
main:
#include "dekompstream.h"
#include <iostream>
int main(void) {
char *testString;
DekompStream ds(nullptr, 0);
std::cout << "TEST" << std::endl;
testString << ds;
std::cout << "Outside operator: " << testString << std::endl; // Fails misarably. Some weird randome memory-slice looking output
//delete [] testString;
return 0;
}
dekompstream.cpp:
#include "dekompstream.h"
DekompStream::DekompStream(uint8_t *kompDaten, unsigned int anzahlBytes) {
}
DekompStream::~DekompStream() {
}
uint8_t *DekompStream::dekompremieren() {
char *test = new char[4];
test[0] = 'A';
test[1] = 'B';
test[2] = 'C';
test[3] = '\0';
return (uint8_t *)test;
}
char *operator<<(char *zeichenkette, DekompStream &dekompStream) {
zeichenkette = (char *)dekompStream.dekompremieren();
std::cout << "Inside operator: " << zeichenkette << std::endl; // Works fine
return zeichenkette;
}
dekompstream.h:
#ifndef DEKOMPSTREAM_H
#define DEKOMPSTREAM_H
#include <cstdint>
#include <iostream>
class DekompStream {
public:
DekompStream(uint8_t *kompDaten, unsigned int anzahlBytes);
~DekompStream(void);
friend char *operator<<(char *zeichenkette, DekompStream &dekompStream);
private:
uint8_t *dekompremieren();
};
#endif
Thanks in advance :-)
testString << ds doesn't do what you think it does.
If we take a look at the code for operator<<:
char *operator<<(char *zeichenkette, DekompStream &dekompStream) {
zeichenkette = (char *)dekompStream.dekompremieren();
return zeichenkette;
}
The function reassigns the char* argument to a new value, then returns the new value. Fine. OK. But how does the compiler use this?
When we write:
testString << ds;
The compiler turns it into:
operator<<(testString, ds);
See the error?
You returned a new value, but because the operator overload was turned into a function call, the allocated pointer was lost. In addition, because the char* argument was simply reassigned, the original pointer value wasn't changed, meaning you got whatever was on the stack previously.
If we look at the declarations for the STL functions for iostream, we'll see the way to fix it:
ostream& operator<< (ostream& os, const char* s);
Notice the first parameter is passed by reference - this ensures any modifications to the stream made by the function persist in the calling context.
Simply change your function definition to:
char *operator<<(char *&zeichenkette, DekompStream &dekompStream){
...
}
and the issue will disappear.
I am new to C++ OOP concepts and I come from a Java OOP background. I have tried this->name=name but I get an error and then I tried to use a pointer, but, even though the code runs, it does not 'save' the name in the object's name.
#include <iostream>
using namespace std;
class Person{
private:
char name[];
int age;
public:
Person(char name[], int age){
char* pName=this->name;
pName=name;
this->age=age;
cout<<this->name;
}
getInformation(){
cout<<"name: "<<this->name<<" age: "<<this->age;
}
};
int main(){
Person foo("foo",5);
}
In C++, arrays usually need to be declared with a length - in this case, char name[] would be invalid.
Additionally, arrays declared int array[5] (with a size) cannot be directly assigned to. i.e. this is invalid:
class A
{
public:
int array[5];
void foo() {
int other_array[5] = {0, 0, 0, 0, 0};
this->array = other_array;
}
}
Instead, you need to assign each element one at a time (e.g. in a loop):
for(int i = 0; i < 5; ++i) this->array[i] = other_array[i];
This is because in C++ arrays have space for all the elements, unlike Java where char name[] is a pointer to an array. If you want to be able to directly change what array a variable refers to, you should use a pointer instead - a pointer can be dereferenced like an array:
class A
{
public:
int *array;
int foo(int *other_array) {
this->array = other_array;
}
}
Though if you do this, be careful of memory management.
Ignoring (for the moment) the fact that the code is invalid and won't compile; here:
char* pName=this->name;
You instantiate a local pointer and assign the unitialised pointer name to it. Then you immediately reassign it with a pointer to the parameter name:
pName=name;
But both pName and name are temporary and are pointers to strings, not the strings themselves. Moreover pName is not a reference to this->name. So on return from the Person() constructor, the object remains unchanged (and uninitialised). You may have intended:
char*& pName = this->name ;
pName = name ;
But that is simply a convoluted form of:
this->name = name ;
and is still ill advised because you are then assigning this->name with the address of the literal string "foo" from main() which is const. In any case you are giving Person a pointer to data that belongs to something else and is not "owned" by Person, and Person has no knowledge of the data's lifetime or validity. It is recipe for all kinds of bugs and undesired behaviour.
Null-terminated (or ASCIZ or C-style) strings are not a true data type; it is merely a char array convention. Moreover arrays even are not first-class data types in C++ and you cannot assign one array to another or pass an array by value as a function argument - instead assignment and argument passing are pointer operations. In Person(), name is a pointer to the first character in the string constant "foo" not an independent string containing "foo".
To correct this using C-style strings:
#include <iostream>
#include <cstring>
class Person
{
private:
char name[256]; // << Must have size here.
int age;
public:
Person( const char* name, int age) // << Must have const arg for "foo" to be a valid parameter.
{
std::strncpy( this->name, // << C strings must be explicitly copied, not "assigned".
name,
sizeof(this->age) - 1 ) ;
this->age = age;
}
void getInformation() const // << must have return type, and may be const.
{
std::cout << "name: " << this->name << " age: " << this->age << "\n" ;
}
} ;
However in most cases it would be preferable to use C++ std::string objects:
#include <iostream>
#include <string> // << Use C++ string library
// (i.e. string rather than cstring/string.h)
class Person
{
private:
std::string name; // << std::string memory management is intrinsic
int age;
public:
Person( const std::string name, int age)
{
this->name = name ; // << Assignment possible due to
// operator overloading
this->age = age;
}
void getInformation() const
{
std::cout << "name: " << this->name << " age: " << this->age << "\n" ;
}
} ;
As said above, std::string is to recommend for now.
Not only for this problem, std::string is very nice to work with in many cases. especially in the learningface.
Just take a look at the string class here from MS' site:
https://learn.microsoft.com/en-us/cpp/standard-library/basic-string-class?view=vs-2019
If you're using c++, it's way better to use std::string to deal with strings.
But answring to your question, when you assign a value to any variable, it overrides its previous value and gets a new one.
In your code:
char* pName=this->name;
pName=name;
The first line assigns a new value to your pName variable. and the second line, simply overrides your first line, and copies something new into your pName variable. so, that's not a correct approach for what you want to do.
If you want to have a char * variable and copy content of another char * into it, I suggest something like:
private:
char *name;
...
public:
Person(char name[], int age){
this->name = strdup(name);
...
}
And be careful to free memory of name in your destructor function.
I've tried to write a class for a student information(name,IDno,and degree), this information will be printed on the screen; but I don't know where is the errors?!
#include <iostream>
#include <conio.h>
using namespace std;
class Student {
private:
char name;
int idnumber;
char degree;
public:
Student(char,int,char);
void setName(char n){name = n;}
int getName(){return name;}
void setIdnumber(char id){idnumber = id;}
int getIdnumber(){return idnumber;}
void setDegree(char d){degree = d;}
int getDegree(){return degree;}
};
Student::Student(char n,int id,char d){
name = n;
idnumber = id;
degree = d;
}
int main(){
Student s1, s2;
s1.setName(Sara);
s1.setIdnumber(333);
s1.setDegree(A);
s2.setName(Jack);
s2.setIdnumber(222);
s2.setDegree(B);
cout << "name: " << s1.getName() << ",IDnumber: " << s1.getIdnumber() << ",Degree: " << s1.getDegree() << endl;
cout << "name: " << s2.getName() << ",IDnumber: " << s2.getIdnumber() << ",Degree: " << s2.getDegree() << endl;
getch();
return 0;
}
Apparently, you have the following issues:
Student s1, s2;
This will try to call the default constructor. However, you defined a constructor that takes 3 parameters, which inhibits the compiler to generate a default constructor for you, so you will fail to create those objects, which effectively makes your follow up member function calls fail.
s1.setName(Sara);
setName takes char type as parameter type, if you mean string literal "Sara", then you will have trouble. Similar issues can be found in other function calls. You should fix this.
Meanwhile, you should prefer to use member initialization list instead of using assignment in constructor body to initialize your members.
Student::Student(char n,int id,char d): name(n), idnumber(id), degree(d){}
make sure that your members are declared in order of name, idnumber, degree.
char is a single character, not a string. try using std::string instead.
also, to declare a string literal, surround your string in quotes ""
eg:
s1.setName("Sara");
also, to use std::string, you will need to #include <string>
There are few problems with your code
char name;
name here is just a char literal so it will only store one literal while you are trying to pass string Sara to it. So change name to array or pointer
char name[10] or char *name
Also while passing name pass it with double quotes like "Sara". Change all the formal arguments in the functions to char array or pointer.
Student s1, s2;
Here while creating objects s1 and s2 it will call default constructor, which you have not provided, So include default constructor in your code
Student(){}
s1.setDegree(A);
Here you are trying to pass a literal not a variable so quote it
s1.setDegree('A');
C++ provides string datatype which is much easy to use wrt to char or char[], so use it. You can operate upon it like you use other datatype like int, double.
That will avoid your hassle to specify array length, using strcpy() to copy strings.
string name;
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 {
public:
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);
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" :)
struct myObj {
//...
char myVarChar;
};
This won't work. You either need a fixed size array, a pointer to char or use the struct hack. You won't be able to assign a pointer to this myVarChar.
so i'd like to create my own lightweight string class which has no extra overhead std::string has.
What extra overhead are you referring to? Have you tested and measured to see if std::string is really a bottleneck?
I think std::string is represented contiguously
Yes, mostly, the character buffer part. However, the following:
(int)length(char[])data
is not required by the standard. Translated: A string implementation need not use this particular layout of its data. And it may have additional data.
Now, your lightweight string class is frought with errors:
class pString {
public:
char c; // typically this is implementation detail, should be private
pString * pString::operator=(const char *);
// need ctors, dtors at least as well
// won't you need any functions on strings?
};
Try something along the lines of the following:
/* a light-weight string class */
class lwstring {
public:
lwstring(); // default ctor
lwstring(lwstring const&); // copy ctor
lwstring(char const*); // consume C strings as well
lwstring& operator=(lwstring const&); // assignment
~lwstring(); // dtor
size_t length() const; // string length
bool empty() const; // empty string?
private:
char *_myBuf;
size_t _mySize;
};
Wow. What you're trying to do is a complete abuse of C++, would be totally compiler dependent if it worked, and would surely land you in TheDailyWTF some day.
The reason you're getting a segfault is probably because your operator= is reallocating the object to a different address, but you're not updating the myString pointer in main. I hesitate to even call it an object at this point, since no constructor was ever called.
I think what you're trying to do is make pString a smarter pointer to a string, but you're going about it all wrong. Let me take a crack at it.
#include <iostream>
using namespace std;
class pString {
public:
char * c;
pString & operator=(const char *);
const char * c_str();
};
pString & pString::operator=(const char * buff) {
cout << "Address of this: " << (uint32_t) this << endl;
cout << "Address of this->c: " << (uint32_t) this->c << endl;
c = (char *) malloc(strlen(buff)+1);
memcpy(c, buff, strlen(buff));
*(c+strlen(buff)) = '\0';
return *this;
};
const char * pString::c_str() {
return c;
}
int main() {
pString myString;
myString = "testing";
cout << "'" << myString.c_str() << "'";
}
Now I wouldn't use malloc but new/delete instead, but I left this as close to your original as possible.
You might think you are wasting the space of a pointer in your class, but you aren't - you're trading it for the pointer you previously kept in main. I hope this example makes it clear - the variables are the same size, and the amount of additional memory allocated by malloc/realloc is the same as well.
pString myString;
char * charString;
assert(sizeof(myString) == sizeof(charString));
P.S. I should point out that this code still needs a lot of work, it's full of holes. You need a constructor to initialize the pointer, and a destructor to free it when it's done, just for starters. You can do your own implementation of operator+, too.
You cannot change the size of an object/struct in either C or C++. Their sizes are fixed at compile time.
when they are represented in memory are objects C++ objects the same as C structs.
Strictly speaking, no. In general, yes. C++ classes and structs are identical in memory layout to C structs except:
Bit fields have different packing rules
Sizes are fixed at compile time
If there are any virtual functions, the compiler will add a vtable entry to the memory layout.
If the object inherits a base class, the new class' layout will be appended to the base class layout, including vtable, if any.
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 std::string has. I think std::string is represented contiguously
You can create a operator+ overload for the char* type. Normal behavior is pointer arithmetic. std::string overloads operator+ to append char* data to the string. The string is stored in memory as a C string, plus additional information. The c_str() member function returns a pointer to the internal char array.
In your C example, you're relying on undefined behavior. Don't realloc like that. It can result in Bad Things - namely bizarre segfaults.
Your C++ example is also doing Bad Things in doing realloc(this). Instead, you should carry a char* and get a new char[] buffer to store the chars in instead of a realloc(). Behavior for such a realloc is undefined.
There is a lot a wrong with your class definition/usage. If you want to store a string you should use a pointer type, like char* a member, not an individual char. Using a single char means that only a single character of memory is allocated.
Another mistake is the allocation code where you do a realloc on this - you can potentially change the memory allocated, but not the value of this. You must assign the result to this to achieve this (this = (*pString)realloc(this, strlen(buff+1));) and that is really bad practice anyway. Much better to use realloc on a char* member.
Unfortunately C++ proper has no alternative for realloc or expand and you must instead use new and delete, doing any copying yourself.
Why do you write in C with classes, why don't use C++?
I do not think 'this' works the way you think it works.
Specifically, you cannot reallocate this to point to a larger buffer in a member function, because whatever called that member function still has a pointer to the old 'this'. Since it's not passed by reference there is no way that you can update it.
The obvious way around that is that your class should hold a pointer to the buffer and reallocate that. However, reimplementing a string class is a good way to give yourself lots of headaches down the line. A simple wrapper function would probably accomplish what you wanted (assuming "being able to use the + operator with NO extra memory usage compared to using strcat" is really what you wanted):
void concatenate(std::string& s, const char* c) {
s.reserve(s.size() + strlen(c));
s.append(c);
}
There's some probability that append may do that internally anyway though.
don't mind the lack of const correctness, as this is a mock up, but how about this:
class light_string {
public:
light_string(const char* str) {
size_t length = strlen(str);
char* buffer = new char[sizeof(size_t) + length + 1];
memcpy(buffer, &length, sizeof(size_t));
memcpy(buffer + sizeof(size_t), str, length);
memset(buffer + sizeof(size_t) + length, 0, 1);
m_str = buffer + sizeof(size_t);
}
~light_string() {
char* addr = m_str - sizeof(size_t);
delete [] addr;
}
light_string& operator =(const char* str) {
light_string s = str;
std::swap(*this, s);
return *this;
}
operator const char*() {
return m_str;
}
size_t length() {
return
*reinterpret_cast<size_t *>(m_str - sizeof(size_t));
}
private:
char* m_str;
};
int main(int argc, char* argv[])
{
cout<<sizeof(light_string)<<endl;
return 0;
}
You are moving the "this" pointer. Thats not going to work.
I think what you really want is just a wrapper around a buffer.
#include <iostream>
using namespace std;
class pString {
public:
char c[1];
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->c, strlen(buff)+1);
memcpy(this->c, buff, strlen(buff));
*(this->c+strlen(buff)) = '\0';
return this;
};
struct myObj {
int myInt;
char myVarChar;
};
int main() {
pString * myString = (pString *) malloc(sizeof(pString));
*myString = "testing vijay";
cout << "'" << ((char*)myString << "'";
}
This should work. But its not advisable.
#include <iostream>
using namespace std;
class pString {
public:
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;
char *newPoint = (char *)realloc(this, strlen(buff)+1);
memcpy(newPoint, buff, strlen(buff));
*((char*)newPoint+strlen(buff)) = '\0';
cout << "Address of this After: " << (uint32_t) newPoint << endl;
return (pString*)newPoint;
};
int main() {
pString * myString = (pString *) malloc(sizeof(pString));
*myString = "testing";
cout << "Address of myString: " << (uint32_t) myString << endl;
cout << "'" << (char *) myString << "'";
}
Works When realloc doesn't re-assign the pointer i.e.
Address of this: 1049008
Address of this->c: 1049008
Address of this After: 1049008
Address of myString: 1049008 'testing'
Works, but when the the following happens it fails
Address of this: 1049008
Address of this->c: 1049008
Address of this After: 1049024
Address of myString: 1049008 ''
the obvious solution is to have
this = (pString*) newPoint;
But the compiler complains about an invalid lvalue in assignment. Does anyone the correct way to update this (just for completeness, i doubt i'll use the code since everyone seems to hate it). Thanks
If you want something that is basically the same as std::string except that it doesn't know how long the string is, you should learn how std::string works, what operator overloads it has, etc. and then mimic that, with just the differences you want.
There is unlikely to be any real point to this, however.
Regarding your latest update - you say you want a design in which general application code will be passing around naked pointers to heap objects. With no automatic cleanup.
This is, quite simply, a very bad idea.
What you want to do doesn't and cannot work in C++. What you are looking for is the C99-feature of flexible arrays. This works nice in C99 for two reasons, first you don't have build-in constructors and second you don't have inheritance (at least not as a language feature). If a class inherits from another the memory used by the subclass is packed by hind the memory of the parent class, but a flexible array needs to be at the end the structure/class.
class pString {
char txt[];
}
class otherString : pString { // This cannot work because now the
size_t len; // the flexible array is not at the
} // end
Take std::string it was written by experts of C++, I'm sure they didn't leaved out a "good trick" without a reason. If you still find out that they don't perform very well in your programm, use plain C strings instead, of course, they don't provide the sweet API, you want.
You can't realloc C++ objects. As others pointed out this is not really a pointer you can modify, there's no guarantee that it will be pointing to an area realloc has access.
One solution to concatenation is to implement a class hierarchy that will defer the real concatenation until it is needed.
Something like this
class MyConcatString;
class MyString {
public:
MyString(const MyConcatString& c) {
reserve(c.l.length()+c.r.lenght());
operator = (l);
operator += (r);
}
MyConcatString operator + (const MyString& r) const {
return MyConcatString(*this, r);
}
};
class MyConcatString {
public:
friend class MyString;
MyConcatString(const MyString& l, const MyString& r):l(l), r(r) {};
...
operator MyString () {
MyString tmp;
tmp.reserve(l.length()+r.length());
tmp = l;
tmp += r;
return tmp;
}
private:
MyString& l;
MyString& r;
}
So if you have
MyString a = "hello";
MyString b = " world";
MyString c = a + b;
Will turn into
MyString c = MyConcatString(a, b);
For more detail check "The C++ Programming language".
Other solution, is to wrap char* inside a struct, but you seem to no like this idea.
But whatever solution you will choose, objects in C++ can't be relocated.
If you want performance, you can write your class like this:
template<int max_size> class MyString
{
public:
size_t size;
char contents[max_size];
public:
MyString(const char* data);
};
Initialize max_size to an appropriate value under context. In this way the object can be created on stack, and no memory allocation is involved.
It is possible to create what you desired by overloading new operator:
class pstring
{
public:
int myInt;
char myVarchar;
void* operator new(size_t size, const char* p);
void operator delete(void* p);
};
void* pstring::operator new(size_t size, const char* p)
{
assert(sizeof(pstring)==size);
char* pm = (char*)malloc(sizeof(int) + strlen(p) +1 );
strcpy(sizeof(int)+pm, p);
*(int*)(pm) = strlen(p); /* assign myInt */
return pm;
}
void pstring::operator delete(void* p)
{
::free(p);
}
pstring* ps = new("test")pstring;
delete ps;
This code is a mess and RnR and others suggested is not advisable. But it works for what i want it to do:
#include <iostream>
using namespace std;
struct pString {
/* No Member Variables, the data is the object */
/* This class cannot be extended & will destroy a vtable */
public:
pString * pString::operator=(const char *);
};
pString& operator+(pString& first, const char *sec) {
int lenFirst;
int lenSec = strlen(sec);
void * newBuff = NULL;
if (&first == NULL)
{
cout << "NULL" << endl;
lenFirst = 0;
newBuff = malloc(sizeof(pString)+lenFirst+lenSec+1);
} else {
lenFirst = strlen((char*)&first);
newBuff= (pString*)realloc(&first, lenFirst+lenSec+1);
}
if (newBuff == NULL)
{
cout << "Realloc Failed"<< endl;
free(&first);
exit(0);
}
memcpy((char*)newBuff+lenFirst, sec, lenSec);
*((char*)newBuff+lenFirst+lenSec) = '\0';
cout << "newBuff: " << (char*)newBuff << endl;
return *(pString*)newBuff;
};
pString * pString::operator=(const char * buff) {
cout << "Address of this: " << (uint32_t) this << endl;
char *newPoint = (char *)realloc(this, strlen(buff)+200);
memcpy(newPoint, buff, strlen(buff));
*((char*)newPoint+strlen(buff)) = '\0';
cout << "Address of this After: " << (uint32_t) newPoint << endl;
return (pString*)newPoint;
};
int main() {
/* This doesn't work that well, there is something going wrong here, but it's just a proof of concept */
cout << "Sizeof: " << sizeof(pString) << endl;
pString * myString = NULL;
//myString = (pString*)malloc(1);
myString = *myString = "testing";
pString& ref = *myString;
//cout << "Address of myString: " << myString << endl;
ref = ref + "test";
ref = ref + "sortofworks" + "another" + "anothers";
printf("FinalString:'%s'", myString);
}