Brief
I am defining my own String class. Everything works fine except for my definition of += which I intend to use to concatenate Strings
//expected behaviour
String c = "foo";
String d = "lala";
c+=d;
cout<<c;
Should output:
foolala
I am having an issue with it as it does seem to work correctly except for the last bit where it seems to not be passing the pointer.
Here is the code (I have omitted most of the other definitions as I don't think they are useful for this)
Code
class String{
private:
unsigned int SizeS;
char *Buffer;
public:
String():SizeS(0){}
String(unsigned int i):SizeS(i){Buffer=new char[SizeS];}
String(const char *string)
{
//defines the initialiser
SizeS = strlen(string); //find out the length of the string
Buffer = new char[SizeS]; //allocate space for the entire string+1 for terminator
memcpy(Buffer,string,SizeS); //copy to buffer the whole thing
Buffer[SizeS]=0; //terminate the buffer with an end character
}
char * GetBuffer() const { return this->Buffer; }
String (const String& copied) :SizeS(copied.SizeS)
{
// defines how copying works
Buffer = new char[SizeS];
memcpy(Buffer,copied.Buffer,SizeS);
}
// this is where the issue is ------------------
String* operator += (const String& to_concat)
{
unsigned int newSize = this->SizeS + to_concat.SizeS;
String *p = new String(newSize) ;
memcpy(p->Buffer,this->Buffer,this->SizeS);
memcpy(p->Buffer+this->SizeS,to_concat.Buffer,to_concat.SizeS);
std::cout<<p->Buffer<<std::endl;
return p;
}
// this is where the issue ends ------------------
};
std::ostream& operator<< (std::ostream& stream, const String& other) { stream << other.GetBuffer(); return stream; }
int main()
{
String c="foo";
std::cout<<c<<std::endl;
c += c;
std::cout<<c<<std::endl;
}
Expected Output
foo
foofoo
foofoo
Actual Output
foo
foofoo
foo
Question
What am I doing wrong? From my understanding, I am overwritting the pointer c with the pointer p, but it seems as though c does not change. Why is that ?
Solution
After reading the comments and suggestion I came up with this solution which works.
String& operator += (const String& to_concat)
{
unsigned int newSize = this->SizeS + to_concat.SizeS;
char* p = new char[newSize];
memcpy(p,this->Buffer,this->SizeS);
memcpy(p+this->SizeS,to_concat.Buffer,to_concat.SizeS);
delete[](this->Buffer);
this->Buffer=p;
this->SizeS=newSize;
return *this;
}
Because you didn't write any code to change c.
There is no "pointer c" and, even if there were, you'd not be overwriting it.
Your += creates a new, dynamically allocated string, using the data from the original two strings, then returns a pointer to it which your program then throws away (incidentally, leaking that new string).
Instead of creating and returning p, you should be modifying the buffer in this (then, conventionally, returning *this as a String& to permit chaining).
Also, a += operator should not produce output.
A + operator could work similarly to how you've done it, since they're supposed to produce new objects, but you shouldn't actually use new for that — you'd still have a memory leak. Try to avoid dynamic allocation (though you're going to need to dynamically allocate the buffer that each String's buffer pointer point to).
Related
I'm encountering some issues with overloading the [] operator for a problem in class. This is the function for the overloading:
const char* Person::operator[](const char* str)
{
if (strcmp(str, "name") == 0)
return reinterpret_cast<const char *>atoi(name);
if (strcmp(str, "age") == 0)
{
char temp[4];
_itoa(age, temp, 10);
//cout << temp;
return temp;
}
}
The class defition looks like this
class Person
{
private:
const char* name;
unsigned int age;
double height;
int gradeNo;
int grades[10];
public:
Person();
Person(const char* name, int age, double height);
void addGrade(int grade);
const char* operator [] (const char* str);
operator int();
};
The problem I'm getting is with the return temp; line from the operator overload function. CLion returns the following warning: Address of stack memory associated with local variable 'temp' returned
Sure enough, when trying to use the operator, the value returned is a memory address. How can I go about fixing this? Is it related to the return type of the function?
You are taking an address to a temporary (that is located on the stack), that will leave the returned pointer dangling almost immediately.
I would very strongly suggest using std::string for strings, do not write C++ just as C with classes.
Then return by std::string by value here. Bad C-like alternative is to allocate the string on the heap and return that, preferably as std::unique_ptr at least.
EDIT after the comment below:
Since you are required to convert an integer to string and return the value, you cannot return a temporary variable, the result must outlive the method. There are basically two bad options:
Make temp static, this way the pointer remains valid. Downside is the function is no longer re-entrant. This is safer because it won't leak.
const char* foo(int age){
static char temp[4];
_itoa(age, temp, 10);
return temp;
}
Return a heap allocated string. Big danger is that you are leaving the user to deallocate it:
const char* foo(int age){
char* temp = new char[256];
_itoa(age, temp, 10);
return temp;
}
I believe you also have a typo in your code:
return reinterpret_cast<const char *>atoi(name);
The atoi should not be there, right?reinterpret_cast should not be needed.
I have an append function part of a string class I am working on, and something very strange happens upon usage. When I print out the appended string inside the function and then also in main, it works. But when I comment out the printing part inside the function and just leave the print in main, the output is some random character. Here is the code:
String.cpp:
void String::append(const String buf)
{
char c[99];
for (auto i = 0; i < this->length(); ++i) {
c[i] = this->cstr()[i];
}
for (auto i = this->length(); i < (this->length() + buf.length() + 1); ++i) {
c[i] = buf.cstr()[i - this->length()];
}
*this = c;
printf("%s\n", *this); // if I comment this line out then the append function doesn't work properly
}
Main:
int main()
{
String a = "Hello";
String b = "Hi";
a.append(b);
printf("%s\n", a);
}
When both print functions are used, the output is this:
When only the print function in main is used:
What might be causing this? Thanks.
Edit:
Assignment operator:
String &String::operator=(char* buf) {
_buffer = buf;
return *this;
}
Constructor:
String::String(char* buf) : _buffer(buf), _length(0) {
setLength();
}
char c[99];
is an array with automatic storage duration. Using a pointer to the first element (aka c) after you leave the append() function is undefined behaviour.
Storing it via your assignment operator will not save the data or prevent it from beeing deleted.
In order to keep the data you either need to deal with dynamic allocation using new and delete (which will be some effort, think about constructors, destructors, assignments, copy-constructors/assignments) or you need to copy the data to your previously assigned buffer.
For ways to copy an array of chars see this question
Assume I want to write my own string class. The string has a property char * s which is a pointer that points to a character.
Now in the constructor, String::String(), what would you pass in to assume another char * to that? You can't really do something like the code below since both pointers will point to the same thing (and we don't want that):
String::String(const char *str) {
s = str;
}
Any help is appreciated!
You need to deep copy the string, i.e. create a character buffer long enough to incorporate the contents of str, then copy the contents into it. The simplest way to achieve that would be using strdup strcpy, since the former is nonstandard:
s = new char[strlen (str) + 1];
if (s == NULL) throw some_exception;
strcpy (s, str);
Please do not write your own string class. There are hell of a lot of details you have to know not to introduce mistakes (for example, overloaded operators, boolean idioms etc), and a lot more details to make that string class efficient (for example, implement copy-on-write) etc. But just for educational purposes, you have to make a copy of the passed string. Here is an example:
#include <cstdint>
#include <cstring>
#include <cstdio>
class String
{
char *p_;
public:
explicit String (const char *str)
{
auto length = std::strlen (str) + 1;
p_ = new char [length];
std::memcpy (p_, str, length);
}
~String ()
{
delete [] p_;
p_ = nullptr;
}
inline const char *c_str () const
{
return p_;
}
};
int
main ()
{
String s ("Hello, world!");
std::printf ("%s\n", s.c_str ());
}
You should copy contents of null-terminated string that is passed as parameter.
One thing is that you might remove terminating null, because if you have your own class you can manage string boundaries manually by keeping current length.
I'm trying to re-learn C++ and was wondering if anyone could help me out here. I'm trying to implement my own String class to see if I can remember things, but I'm stuck on the constructor.
I have my header file and want to have a constructor as so:
Header File (MyFiles\String.h):
#ifndef STRING_
#define STRING_
using namespace std;
#include <iostream>
class String
{
private:
static const unsigned int MAX = 32; // Capacity of string
char Mem[MAX]; // Memory to hold characters in string
unsigned Len; // Number of characters in string
public:
// Construct empty string
//
String()
{
Len = 0;
}
// Reset string to empty
//
void reset()
{
Len = 0;
}
// Return status information
//
bool empty() const
{
return Len == 0;
}
unsigned length() const
{
return Len;
}
// Return reference to element I
//
char& operator[]( unsigned I )
{
return Mem[I];
}
// Return constant reference to element I
//
const char& operator[]( unsigned I ) const
{
return Mem[I];
}
// Construct string by copying existing string
//
String( const String& );
// Construct string by copying array of characters
//
String( const char [] );
// Copy string to the current string
//
String& operator=( const String& );
// Append string to the current string
//
String& operator+=( const String& );
};
// Compare two strings
//
bool operator==( const String&, const String& );
bool operator!=( const String&, const String& );
// Put a string into an output stream
//
ostream& operator<<( ostream&, const String& );
#endif
The bit I'm stuck on is this:
String::String(const String& str)
{
//what goes here?
}
Thanks!
Well, since it's a learning exercise.
I think you want to copy the contents of the other string here since this is a copy constructor. So you will want to copy across all the member variables. In your case
the copy constructor is not necessary because you've got a static array. If you had
dynamic memory (i.e. used new to allocate pointer to Mem) then you'd need this. However,
to show you how it's done, here you go.
String::String(const String& str)
{
//what goes here?
assert(str.Len < MAX); // Hope this doesn't happen.
memcpy(Mem, str.Mem, str.Len);
Len = str.Len;
}
You need to copy the data from str to this. The length is easy:
Len = str.Len; // or, equiv. this->Len= str.Len
The data is a little harder. You might use strcpy or memcpy, or even a for loop.
memcpy(Mem, str.Mem, sizeof Mem);
Good luck!
I concur with Kornel Kisielewicz: the fewer hand-rolled String classes, the better. But you're only doing this to learn, so fair enough :-). Anyway: your copy constructor needs to copy over the length and the contents of the Mem array, and that's it.
(If you were doing this to make something useful rather than as a learning exercise, I'd add: a string class with a fixed maximum string length -- especially one as small as 32 characters -- is a very bad idea indeed. But it's entirely reasonable if you don't feel like dealing with memory allocation and deallocation at the same time as you're trying to remember the even-more-basics...)
//In header file: class definition:
class myString
{
public:
myString(void);
myString(const char *str);
myString(const myString &); //copy constructor
~myString(void); //destructor
void swap(myString &from);
private:
char *stringPtr;
int stringLen;
};
//in cpp file, defining them member functions
myString::myString(const char *str)
{
stringLen = strlen(str);
stringPtr = new char[stringLen+1];
strcpy(stringPtr,str);
cout << "constructor with parameter called"<<endl;
}
myString::myString(const myString &str)
{
stringPtr = new char[str.stringLen +1];
strcpy(stringPtr,str.stringPtr);
cout << "copyconstructor"<<endl;
}
void myString::swap(myString &from)
{
myString buffer(from);
int lengthBuffer = from.stringLen;
from = new char[stringLen+1];
from.stringLen = stringLen;
strcpy(from.stringPtr, stringPtr);
stringPtr = new char[lengthBuffer+1];
stringLen = lengthBuffer;
strcpy(stringPtr,buffer.stringPtr);
}
You can't modify a reference. Even if you replace it with a pointer modifying a pointer will not modify an object pointed to. Instead you need to work through the reference - just swap the fields.
void myString::swap(myString &from)
{
std::swap( stringLen, from.stringLen );
std::swap( stringPtr, from.stringPtr );
}
the above is using std::swap() as suggested by user sbi in comments. This is completely equivalent to the following (just for illustration, don't reinvent STL):
void myString::swap(myString &from)
// First remember own length and pointer
const int myOldLen = stringLen;
char* myOldPtr = stringPtr;
// now copy the length and pointer from that other string
stringLen = from.stringLen;
stringPtr = from.stringPtr;
// copy remembered length and pointer to that other string
from.StringLen = myOldLen;
from.StringPtr = myOldPtr;
// done swapping
}
Both will work even when called fro self-swapping:
myString string;
string.swap( string );
You have already gotten a few good answers concerning the errors in you myString::swap() function. Yet, I'd like to add another one. There's some many things wrong with that function, I first found it hard to think of where to begin. But then I realized that you fail on some fundamental issue which I'd like to point out:
As a convention, a function called swap is expected to perform its task
in O(1)
without ever throwing an exception.
(Yes, I know, there are exceptions: std::tr1::array<>::swap(). But those should be very well justified.) Your implementation fails on both accounts. It is O(n) (strcpy) and might throw an exception (new) -- and it does so unnecessarily and without justification.
When you look at myString, you'll see that it only has two pieces of member data, which both are of built-in type. That means swapping two objects of this class is really simple to do while keeping to the conventions mentioned above: just swap the member data. That's as simple as calling std::swap on them:
void myString::swap(myString &from)
{
std::swap(this->stringPtr,from.stringPtr);
std::swap(this->stringLen,from.stringLen);
}
This is will never fail (swapping two pointers and two integers cannot fail), executes in O(1), is very easy to understand (well, once you get a grip on that swapping, anyway; it is an idiomatic form of implementing a class-specific swap function), and consists of two lines of code calling something well-tested in the standard library instead of 8 lines of code doing error-prone (and, in your case, erroneous) manual memory management.
Note 1: Once you've done this, you should specialize std::swap to call your implementation for your class:
namespace std { // only allowed for specializing function templates in the std lib
template<>
inline void std::swap<myString>(myString& lhs, myString& rhs)
{
lhs.swap(rhs);
}
Note 2: The best (simple, exception-safe, and self-assignment-safe) way to implement assignment for your class is to use its swap:
myString& myString::operator=(const myString& rhs)
{
myString tmp(rhs); // invoke copy ctor
this->swap(tmp); // steal data from temp and leave it with our own old data
return *this;
} // tmp will automatically be destroyed and takes our old data with it
from = new char[stringLen+1]; should be from.stringPtr = new char[stringLen+1]; . Also remember to free the previously allocated memory before allocating new one.
Look closely at the line
from = new char[stringLen+1];
It is the same as
from = MyString(new char[stringLen+1]);
so your constructor of MyString get uninitialized array of chars. Then you trying to get the length of the string, but strlen just looping through chars of the string looking for 0 char. As we don't know what content uninitialized array of chars might have, we don't know what length strlen could return. It can even go further than array boundary and crash your program with segfault. But I can say for sure, after that there's not enough space in from.stringPtr to hold the string you want to copy in it.
So, use from.stringPtr = new char[stringLen+1]; or better from = MyString(*this); since you have copy constructor already.