You have just allocated memory which contains garbage or nothing.

Strlen will try to print from the first address until it finds NULL. So when 
you say strlen(newstr) it is giving 3 which is a undefined value.

For example let  us a take c prog:

Main()
{

        Char *p;

        Printf("%d",strlen(p));

}

Out: 3 which is an undefined value.

Let us give char *p = NULL;

Now try printing it will give segmentation fault.  Because the pointer is 
pointing to nothing and your collecting length from NULL.

Char *str = "Hello World";
Char *newstr = new char[strlen(str)];

After this assign some thing to newstr and try to print the strlen.

Thanks & Regards,

Vijaya Rama Raju . Jampana,

________________________________
From: c-prog@yahoogroups.com [mailto:c-p...@yahoogroups.com] On Behalf Of CK
Sent: Monday, January 11, 2010 10:49 AM
To: c-prog@yahoogroups.com
Subject: [c-prog] Help with new char array



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?

Thank you

CK

[Non-text portions of this message have been removed]


________________________________
"DISCLAIMER: This message is proprietary to Aricent and is intended solely for 
the use of the individual to whom it is addressed. It may contain privileged or 
confidential information and should not be circulated or used for any purpose 
other than for what it is intended. If you have received this message in error, 
please notify the originator immediately. If you are not the intended 
recipient, you are notified that you are strictly prohibited from using, 
copying, altering, or disclosing the contents of this message. Aricent accepts 
no responsibility for loss or damage arising from the use of the information 
transmitted by this email including damage from virus."


[Non-text portions of this message have been removed]

Reply via email to