Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I want to copy strings,but occur this....."Access to memory errors....",why?

I want to copy strings,but occur this....."Access to memory errors....",why?

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresperformancequestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    milestanley
    wrote on last edited by
    #1

    /*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; }

    T C L I R 5 Replies Last reply
    0
    • M milestanley

      /*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; }

      T Offline
      T Offline
      theCPkid
      wrote on last edited by
      #2

      Any idea what's the problem in the following code?

      int main()
      {
      char* str = "Hello";
      str[0] = 'Y';
      cout<

      L 1 Reply Last reply
      0
      • M milestanley

        /*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; }

        C Offline
        C Offline
        carter2000
        wrote on last edited by
        #3

        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";

        L 1 Reply Last reply
        0
        • M milestanley

          /*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; }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 the to 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 is to before it gets incremented, hence it is correct.[/EDIT]

          modified on Monday, October 12, 2009 5:45 AM

          1 Reply Last reply
          0
          • T theCPkid

            Any idea what's the problem in the following code?

            int main()
            {
            char* str = "Hello";
            str[0] = 'Y';
            cout<

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            theCPkid wrote:

            Any idea what's the problem in the following code?

            Apart from the fact that it will print "Yello", nothing.

            T 1 Reply Last reply
            0
            • C carter2000

              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";

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 or char str[] yields the same result.

              C 1 Reply Last reply
              0
              • L Lost User

                theCPkid wrote:

                Any idea what's the problem in the following code?

                Apart from the fact that it will print "Yello", nothing.

                T Offline
                T Offline
                theCPkid
                wrote on last edited by
                #7

                have you really tried it? i have the feeling that you will get write access error...

                L 1 Reply Last reply
                0
                • L Lost User

                  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 or char str[] yields the same result.

                  C Offline
                  C Offline
                  carter2000
                  wrote on last edited by
                  #8

                  :) Maybe I made something wrong, but it just works fine in my computer. My compiler is VS2008.

                  L 1 Reply Last reply
                  0
                  • M milestanley

                    /*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 Offline
                    I Offline
                    Iain Clarke Warrior Programmer
                    wrote on last edited by
                    #9

                    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/[^]

                    1 Reply Last reply
                    0
                    • T theCPkid

                      have you really tried it? i have the feeling that you will get write access error...

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      theCPkid wrote:

                      i have the feeling that you will get write access error...

                      Yes, you're right; my apologies.

                      1 Reply Last reply
                      0
                      • C carter2000

                        :) Maybe I made something wrong, but it just works fine in my computer. My compiler is VS2008.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        foot - mouth - brain! I must remember brain comes first. :sigh:

                        1 Reply Last reply
                        0
                        • M milestanley

                          /*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; }

                          R Offline
                          R Offline
                          rejupal
                          wrote on last edited by
                          #12

                          Try this char from[] = "c plus plus"; char to[] = "oooooooooooooooo"; instead of char *from = "c plus plus"; char *to = "oooooooooooooooo";

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups