On 1/11/2010 12:19 AM, CK wrote:
> I'm trying to define dynamic array using "new".
>
> Char *str = "Hello World";
> Char *newstr = new char[strlen(str)];
>
> Cout<<strlen(str)<<endl;
> Cout<<strlen(newstr)<<endl;
>
> Strlen(str) = 11;
> Strlen(newstr) = 3;
>
> Why is it only 3? What is wrong? Can somebody help me with this?
>    

Try initializing the memory to which newstr points before trying to use 
it: this includes getting its length. I'm too lazy to look it up in the 
standard right now, but I am pretty sure there is no guarantee as to the 
contents of that memory.

Also, if you are using keyword "new" you are using C++. I recommend 
using std::string instead of character arrays. They are called "C 
strings" for a reason.

-- 
John Gaughan
http://www.jtgprogramming.org/

Reply via email to