I want to copy strings,but occur this....."Access to memory errors....",why?
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }
Loveprogramer wrote:
char *from = "c plus plus"; char *to = "oooooooooooooooo";
Here "from" and "to" are constant strings exactly, so the codes below would be mistake. "*to++ = *from++". If you declare "from" and "to" as array of string, your program would work fine. Just like: char from[] = "c plus cplus"; char to[] = "oooooooooooooooooooooo";
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }
I think the problem is probably in the following statement:
while((*to++ = *from++) != '\0');
. The C++ language specification for the assignment operator states "The assignment operators return the value of the object specified by the left operand after the assignment." i.e the to pointer. Thus in the above statement the comparison is done with the next element of theto
array, which being longer than the from array, contains the character 'o'. The while loop is now out of control as it is copying garbage. Rework your loop so that it tests the source array element for '\0', rather than the destination. [EDIT]Sorry, I got this wrong, the result of the expression isto
before it gets incremented, hence it is correct.[/EDIT]modified on Monday, October 12, 2009 5:45 AM
-
Any idea what's the problem in the following code?
int main()
{
char* str = "Hello";
str[0] = 'Y';
cout< -
Loveprogramer wrote:
char *from = "c plus plus"; char *to = "oooooooooooooooo";
Here "from" and "to" are constant strings exactly, so the codes below would be mistake. "*to++ = *from++". If you declare "from" and "to" as array of string, your program would work fine. Just like: char from[] = "c plus cplus"; char to[] = "oooooooooooooooooooooo";
-
theCPkid wrote:
Any idea what's the problem in the following code?
Apart from the fact that it will print
"Yello"
, nothing. -
carter2000 wrote:
Here "from" and "to" are constant strings exactly, so the codes below would be mistake.
Not true; declaring a string as
char* str
orchar str[]
yields the same result.:) Maybe I made something wrong, but it just works fine in my computer. My compiler is VS2008.
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }
Well, what have you discovered for yourself? Does that copy_pointer loop die on the first time it does something? When it hits a NULL? Does it die if
to
is a dynamically created variable? ie,char *buf = new char [128]; // or (char*)malloc(128); if you prefer
copy_pointer (from, buf);
delete [] buf; // or free (buf); if you prefer.I'm 90% sure I know what's wrong, but I'll wait for you to run these tests and see if you can find out for yourself. Iain.
I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need contract work done, give me a job! http://cv.imcsoft.co.uk/[^]
-
:) Maybe I made something wrong, but it just works fine in my computer. My compiler is VS2008.
-
/*Copy strings*/ #include "stdafx.h" #include <iostream> using namespace std; /*Achieve via array*/ void copy_array(char from[], char to[]) { int i = 0; while(from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } /*Achieve via pointer*/ void copy_pointer(char *from, char *to) { while((*to++ = *from++) != '\0'); } int _tmain(int argc, _TCHAR* argv[]) { char *from = "c plus plus"; char *to = "oooooooooooooooo"; //copy_array(from, to); copy_pointer(from, to);//Here: Test.exe Department of 0x00411536 unhandled exception: //0xC0000005: Write Access Violation occurs when the //position of 0x00417ac8,Why? cout << to << endl; return 0; }