Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I\'m trying to copy one char array to another (without using thestring classe

ID: 3610159 • Letter: S

Question

So I'm trying to copy one char array to another (without using thestring classes), but I'm running into a problem.

When I re-initialize dest, I get a large amount of junk; how wouldI go about deleting the stuff after the relevant data?

Function:

void String::Copy(const char* src,char*& dest)
{
    int size = GetLength(src); //GetLength(s) isessentially strlen(s)
    delete[] dest;
    dest = new char[size];
    for(int i = 0; i < size; i++)
        dest[i] = src[i];
}

For example, using asdf as src, I get this:
src    0x0014f6dc "asdf"    constchar *
dest    0x007554d0"asdfýýýý««««««««"   char * &

Will rate lifesaver if your solution solves my problem.
Using MSVS 2008.

Explanation / Answer

//Hope this willhelp you.. void String::Copy(const char* src,char*& dest) {     int size = GetLength(src); //GetLength(s) isessentially strlen(s)     delete[] dest;     dest = new char[size+1];       //It should be null terminated ,so one more for nullchar     for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote