class String
{
public:
/// constructs this String from the parameter s
String( constchar * s = ""){ strcpy(buf,s); }
private:
staticint strlen(constchar *s)
{
int len = 0;
for(int i = 0; s[i] != '\0';i++)
len++;
return len;
}
staticchar *strcpy(char *dest, constchar *src)
{
dest = newchar[strlen(src) + 1];
for(int i = 0; dest[i] = src[i]; i++){}
return dest;
//delete[] dest;
}
char * buf;// base of array for the characters in this string
// DO NOT add a length data member!! use the null terminator
};
At the moment all my main has is this, and when I try to compile it crashes saying nothing more than "your exe has stopped working". I'm probably breaking a big rule here, does anyone know what it is? I'd like to know what's wrong with my code so I can read up more on the rules that I seem to be breaking.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <blah.h>
usingnamespace std;
int main(){
String s("wow");
cout << s << endl;
return 0;
}
EDIT: tried commenting out lines in my main, it seems everything runs fine except for the part where I'm trying to print my string, any clues?
Your constructor doesn't allocate any memory. All it does is write to some random memory address which points to somewhere you don't have permission to write to.
[Edit: Your strcpy does not replicate the functionality of the C version faithfully (which certainly does not allocate memory.) Both parameters are passed by value, so any changes made to those parameters will not be visible outside the function. You have a memory leak therein.]
Alright let me check if I'm understanding how exactly strcpy works.
1. takes src and copies it into dest so,
2. we dynamically allocate enough memory for dest so that it's the same length as src, +1 for the null termination char
3.we run it through for(int i = 0; i < strlen(src) && src[i] != '\0'; i++), I know it's different from what I had above, decided to remake it. So this iterates through the index of src up until '\0'
4.for each iteration assign each index of src into dest
5. now that dest should be the same as src, we return dest
like so?:
1 2 3 4 5 6 7 8 9 10 11
staticchar *strcpy(char *dest, constchar *src)
{
dest = newchar[strlen(src) + 1]; //allocate memory same size as src
for(int i = 0; i < strlen(src) && src[i] != '\0'; i++) //iterate through src up until '\0'
{
dest[i] = src[i]; //assign each index of src starting at 0 into dest
}
return dest; //dest should be the same as src, so we can return it since it should be copied now?
}
EDIT: alright I think I may have solved the issue, I just allocated new memory in the constructors. I thought my methods would allocate my memory for me but I guess not.
Again, in strcpydest is a copy of the pointer you fed to the function. Changing dest (line 3) in the function has no effect on the original.
And, again, if you're copying the C functionality, strcpy only copies. It does not allocate memory. If you wish to allocate memory, strdup would be more appropriate.
So I should be looking to allocate memory for dest outside of the method whenever I call strcopy?
You're designing your own class, so you can have it operate any way you choose.
However, as cire pointed out, if you're trying to replicate the operation of the C library strcpy (which does not do allocation), then yes, you would want to allocate outside of the function. Using the function name strcpy with different semantics would be confusing.
it seems everything runs fine except for the part where I'm trying to print my string, any clues?
std::ostream's insertion operator (<<) doesn't know how to deal with your String class, you have to overload the operator, usually done as a class method, to deal with your class.
Thanks for the replies. Yeah it doesn't crash on me any more since I've allocated memory outside of strcpy instead of within it.
As for the operator overloading that FurryGuy mentioned, I had that implemented in my class. I just omitted it because I thought it wasn't the culprit but here it is just in case:
Now the problem seems to be that my constructor isn't returning a string. Probably has something to do with how I'm assigning each index of src into dest for strcpy.