|
-
September 15th, 2009, 12:45 PM
#1
String Copy
Hi,
please see this code
Code:
#include <iostream>
using namespace std;
int main(void)
{
char *pA=new char[10]="Raj";
char *pB;
cout<<pA<<endl;
pB=new char[10];
char *q = pB;
while(*pB++ = *pA++);
*pB = '\0';
cout<<q<<endl;
return 0;
}
This code works fine and the out put is
Raj
Raj
string copy happens correctly.
(I am running this code in .NET 2005).
But if I change a little like below the code is giving exception.
Code:
#include <iostream>
using namespace std;
int main(void)
{
char *pA=new char[10]="Raj";
char *pB;
cout<<pA<<endl;
pB=new char[10]= "aaa"; // This is the changed line
char *q = pB;
while(*pB++ = *pA++);
*pB = '\0';
cout<<q<<endl;
return 0;
}
please clarify.
-
September 15th, 2009, 01:10 PM
#2
Re: String Copy
Code:
pB=new char[10]= "aaa"; // This is the changed line
This line of code makes no sense. You are trying to allocate a new block of memory but then assign a pointer to a const string literal "aaa". Does this statement crash immediately or does it crash later? What you are trying to do is very confusing and results in undefined behavior. First you have to allocate the memory. If you want to initialize the memory allocated by operator new you have to use a separate statement to copy the memory.
Code:
char* pB=new char[10]; // allocate the memory
strcpy(pb, "aaa"); // initialize the memory
std::string myString("aaa"); // better to just use a std::string
const char* pb = "aaa"; // if you really want a string literal; this isn't a copy only a pointer assignment
By the way, your first example was not correct either. It just didn't crash for whatever reason. That is why they call it undefined behavior. Sometimes it crashes, sometimes it doesn't.
Last edited by kempofighter; September 15th, 2009 at 01:12 PM.
Reason: code tags added one more example
-
September 15th, 2009, 01:11 PM
#3
Re: String Copy
While that almost looks like a placement new (which I doubt you want) initially, what it is actually doing is tossing away your pointer to the:
new char[10]
and assigning pB to point to the constant character array of "aaa". Typically, that array will be located in memory marked as READ ONLY, so when you try to write to it, you are getting the exception of writing to invalid memory.
It will also leak the allocation of the new char[10].
-
September 15th, 2009, 01:25 PM
#4
Re: String Copy
But if I go for array of char the it works.
So what exactly the new operator forcing not to replace the contect.
Code:
#include <iostream>
using namespace std;
int main(void)
{
char *pA=new char[10]="Raj";
char *pB1;
char pB2[10]="aaa";
cout<<pA<<endl;
pB1=pB2;
char *q = pB1;
while(*pB1++ = *pA++);
*pB1 = '\0';
cout<<q<<endl;
return 0;
}
By the way if I write like
char *pA= new char[10];
p="Raj"
Then also the meaning is same.It is first reserving memory for 10 char Then giving the address of "Raj' to the pointer. Now p contains the address of "Raj".
This is a test program. Just to copy the string using new.
-
September 15th, 2009, 01:31 PM
#5
Re: String Copy
 Originally Posted by Rajesh1978
But if I go for array of char the it works.
That is because an array is writeable, while a string literal isn't writeable.
Code:
int main()
{
char *p = "abc";
p[0] = 'x'; // Undefined behaviour, maybe crash!
}
Code:
int main()
{
char p[] = "abc";
p[0] = 'x'; // OK
}
Regards,
Paul McKenzie
-
September 15th, 2009, 01:33 PM
#6
Re: String Copy
 Originally Posted by Rajesh1978
But if I go for array of char the it works.
So what exactly the new operator forcing not to replace the contect.
Code:
#include <iostream>
using namespace std;
int main(void)
{
char *pA=new char[10]="Raj";
char *pB1;
char pB2[10]="aaa";
cout<<pA<<endl;
pB1=pB2;
char *q = pB1;
while(*pB1++ = *pA++);
*pB1 = '\0';
cout<<q<<endl;
return 0;
}
By the way if I write like
char *pA= new char[10];
p="Raj"
Then also the meaning is same.It is first reserving memory for 10 char Then giving the address of "Raj' to the pointer. Now p contains the address of "Raj".
This is a test program. Just to copy the string using new.
You didn't read my last post. You have not copied anything other than a pointer. You did not copy a string.
Moreover, operator new returns a completely different pointer to an uninitiailized block of memory. If you reassign to the string literal "Raj" then you have just leaked the memory returned by operator new. Plus you aren't trying to overwrite the string pointed to by pA which is why the first program didn't crash (even though it was still not correct).
-
September 15th, 2009, 01:35 PM
#7
Re: String Copy
Why don't you tell us what you want to do so that we can help you do it correctly.
-
September 15th, 2009, 09:19 PM
#8
Re: String Copy
Thanks all
I am trying to clear my pointer and string concept. By doing some kind of code I can make my doubts clear.
Also all your comments helping me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|