0

I am trying to implement Copystring functionality, wherein I don't want to return my destination string as a return value, and want to send it as out parameter in the below CopyString() method. I am also keen to have memory allocated within the CopyString block only.

#include "stdafx.h"
void CopyString (char *strSrc, char* strDsn, int iLen)
{
    strDsn = new char[iLen];

    for (int i=0; i< iLen; i++)
        strDsn[i] = strSrc[i];

}

int main()
{
    char * mystrSrc = "Testing Copy Method";

    int iLen = 0;
    for(int i=0; mystrSrc[i] != '\0'; i++)
        iLen++;

    char * mystrDsn = 0;
    CopyString (mystrSrc, mystrDsn, iLen);

    printf(mystrDsn);
    return 0;
}

Now as I am doing Pass-by-Value, strDsn object of CopyString method will get destroy when Stackunwinding takes place, and hence caller will fail to get the copied value. How to go ahead?

8
  • 4
    Please read about RAII - that, what you are doing is plain wrong (from my point of view) - at least make it char*& strDsn Commented Mar 3, 2015 at 19:55
  • 1
    C or C++? Pick one. Commented Mar 3, 2015 at 19:55
  • 2
    strDsn = new char[iLen]; won't work as you expect, unless you take this parameter by reference: void CopyString (char* strSrc, char*& strDsn, int iLen) Commented Mar 3, 2015 at 19:56
  • 2
    @πάνταῥεῖ char* strDsn& is not right. Commented Mar 3, 2015 at 19:57
  • 1
    Some notes: Pointers to literal strings should be const (const char *mystrSrc = "..."). The source pointer to CopyString should be const (const char *strSrc). The memory allocated in CopyString will need to be released after the printf (delete[] mystrDsn). Passing a non-format string as the format to printf can be dangerous as it will be parsed. (printf("%s",mystrDsn)). Commented Mar 3, 2015 at 20:41

1 Answer 1

2

I just copied your code and made a couple changes, but you need a pointer to a pointer if you want to get the newly created memory out (you can do a reference to a pointer, but eh, pointers show more on the calling side that it may be modified).

#include "stdafx.h"
void CopyString (char *strSrc, char** strDsn, int iLen) // strDsn is a ptr to ptr.
{
    *strDsn = new char[iLen + 1]; // update the ptr value. + 1 for null terminator.

    for (int i=0; i< iLen; i++)
        *strDsn[i] = strSrc[i]; // index painfully (would be look nicer with a temp variable).

}

int main()
{
    char * mystrSrc = "Testing Copy Method";

    int iLen = 0;
    for(int i=0; mystrSrc[i] != '\0'; i++)
        iLen++;

    char * mystrDsn = 0;
    CopyString (mystrSrc, &mystrDsn, iLen); // pass the address of mystrDsn

    printf(mystrDsn);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be better to copy the memory rather than assigning each value?
@Hayden, I think the point of the problem is to write the memory copier. If you look at strcpy they use a while loop (which I like more). I think the best code is readable code, since the optimizer does so many crazy things, usually trying prematurely optimize code makes it harder for the optimizer, and you shouldn't really trouble yourself with performance issues if performance isn't an issue (unless it's that obvious of a problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.