writing cstr functions from scratch

So I need to write many cstr functions from scratch and I'm running into trouble with my constructor that uses strcpy to construct a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class String
  {
  public:
    /// constructs this String from the parameter s
    String( const char * s = ""){ strcpy(buf,s); }

  private:

static int strlen(const char *s)
    {
        int len = 0;
        for(int i = 0; s[i] != '\0';i++)
            len++;
        return len;
    }

static char *strcpy(char *dest, const char *src)
    {
        dest = new char[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>
using namespace 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?
Last edited on
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.]
Last edited on
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
static char *strcpy(char *dest, const char *src)
    {
        dest = new char[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.
Last edited on
Again, in strcpy dest 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? like:

1
2
3
4
5
String( const char * s = "")
    {
        buf = new char[strlen(s)+1];
        strcpy(buf,s);
    }


Just transfer the allocation line from inside strcpy to outside of it whenever it is called?
Last edited on
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.

closed account (E0p9LyTq)
omega4relay wrote:
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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String{
public:

void print( ostream & out )
    {
        out << buf;
    }
    
    void read( istream & in )
    {
        in >> buf;
    }

private:
// etc...
};

ostream & operator << ( ostream & out, String str )
  {
    str.print(out);
    return out;
  }
  istream & operator >> ( istream & in, String & str )
  {
    str.read(in);
    return in;
  }


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.
Topic archived. No new replies allowed.