why memcpy alters the string array?
<p>I have code which goes like this. Could you tell me why it is not behaving as I would expect it to be?</p>

<pre><code>/*
 * test.cpp
 *
 *  Created on: Dec 6, 2012
 *      Author: sandeep
 */

#include&lt;iostream&gt;
#include&lt;string.h&gt;
using namespace std;

int main()
{
    int i=0;
    string s="hello A B:bye A B";
    char *input;
    input=new char(s.size());
    for(i=0;i&lt;=s.size();i++)
        input[i]=s[i];
    char *tokenized1[2],*tokenized2[3];
    tokenized1[0]=strtok(input,":");
    tokenized1[1]=strtok(NULL,":");
    i=0;
    char *lstring;
    while(i&lt;2)
    {
        lstring=new char(strlen(tokenized1[i]));
        memcpy(lstring,tokenized1[i],strlen(tokenized1[i])+1);
        cout&lt;&lt;tokenized1[0]&lt;&lt;"  "&lt;&lt;tokenized1[1]&lt;&lt;endl;
        tokenized2[0]=strtok(lstring," ");
        tokenized2[1]=strtok(NULL," ");
        tokenized2[2]=strtok(NULL," ");
        char c=tokenized2[0][0];
        cout&lt;&lt;c&lt;&lt;endl;
        cout&lt;&lt;tokenized2[0]&lt;&lt;"  "&lt;&lt;tokenized2[1]&lt;&lt;"  "&lt;&lt;tokenized2[2]&lt;&lt;endl;
        i++;
    }

}
</code></pre>

<p>and the output is this.</p>

<pre><code>hello A B  by
h
hello  A  B
hello A B  by
b
by  
</code></pre>

<p>There are some junk values at the end of 1st, 4th and 6th line of output.
Why <code>tokenized1[1]</code> got altered when I I did memcopy of <code>tokenized1[0]</code>? and how to solve this?</p>
