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. Simple Question

Simple Question

Scheduled Pinned Locked Moved C / C++ / MFC
question
22 Posts 8 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.
  • E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #1

    char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

    B T S A C 7 Replies Last reply
    0
    • E Eytukan

      char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      Why not just try to build and run it? Most likely, the output will be "Codeproject", but you've hosed something. Bob Ciora

      1 Reply Last reply
      0
      • E Eytukan

        char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        it won't work as you are trying to cat a 12 characters long string into a 3 characters long array... [edit] there will be an "out of range exception"... According to the MSDN[^], "Because strcpy does not check for sufficient space in strDestination before copying strSource, it is a potential cause of buffer overruns." [/edit]


        TOXCCT >>> GEII power
        [toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005

        E 1 Reply Last reply
        0
        • E Eytukan

          char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

          S Offline
          S Offline
          sunit5
          wrote on last edited by
          #4

          output: Codeproject

          T 1 Reply Last reply
          0
          • E Eytukan

            char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

            A Offline
            A Offline
            Achim Klein
            wrote on last edited by
            #5

            char *strcpy( char *strDestination, const char *strSource ); The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. Have a look at the strlen() function. Regards


            We can do no great things, only small things with great love. - Mother Theresa

            1 Reply Last reply
            0
            • S sunit5

              output: Codeproject

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              never....


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              B S E 3 Replies Last reply
              0
              • T toxcct

                never....


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                B Offline
                B Offline
                Bob Ciora
                wrote on last edited by
                #7

                Probably :p Depends on what's after the output string. If there is more room in the data space, then some poor unsuspecting variable will be stomped. But you won't see that until you need it. Bob Ciora

                1 Reply Last reply
                0
                • T toxcct

                  never....


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  S Offline
                  S Offline
                  sunit5
                  wrote on last edited by
                  #8

                  toxxct but i got output Codeproject

                  1 Reply Last reply
                  0
                  • T toxcct

                    it won't work as you are trying to cat a 12 characters long string into a 3 characters long array... [edit] there will be an "out of range exception"... According to the MSDN[^], "Because strcpy does not check for sufficient space in strDestination before copying strSource, it is a potential cause of buffer overruns." [/edit]


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005

                    E Offline
                    E Offline
                    Eytukan
                    wrote on last edited by
                    #9

                    this is not happening .. its displayin "codeproject" then what's the use of declaring it as the var t2 as [3] ? i dont understand! :confused: V

                    T 1 Reply Last reply
                    0
                    • E Eytukan

                      this is not happening .. its displayin "codeproject" then what's the use of declaring it as the var t2 as [3] ? i dont understand! :confused: V

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005

                      E 2 Replies Last reply
                      0
                      • T toxcct

                        never....


                        TOXCCT >>> GEII power
                        [toxcct][VisualCalc]

                        E Offline
                        E Offline
                        Eytukan
                        wrote on last edited by
                        #11

                        TOX, i too got "CodeProject".. y no error is displayed? V -- modified at 7:32 Monday 29th August, 2005

                        T 1 Reply Last reply
                        0
                        • E Eytukan

                          TOX, i too got "CodeProject".. y no error is displayed? V -- modified at 7:32 Monday 29th August, 2005

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #12

                          yes, i understood that point. did you visit the link to the msdn i provided ? strcpy() doesn't check for sufficient space in strDestination before copying strSource...


                          TOXCCT >>> GEII power
                          [toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005

                          E 1 Reply Last reply
                          0
                          • E Eytukan

                            char t1[12] ="Codeproject"; char t2[3]; strcpy(t2,t1); AfxMessageBox(t2) what'll be the output?? thanx V

                            C Offline
                            C Offline
                            Cedric Moonen
                            wrote on last edited by
                            #13

                            To complete the previous answers: It will perhaps output 'Codeproject' but you cannot be sure because strcpy is copying the charcters outside the bounds of t2 (so in 'unprotected memory'). So, this memory is not 'locked' and your program can write other things in it. Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value that can lead to really baaaaaad things ;-) (like your variable changed magically)

                            E 1 Reply Last reply
                            0
                            • T toxcct

                              if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?


                              TOXCCT >>> GEII power
                              [toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005

                              E Offline
                              E Offline
                              Eytukan
                              wrote on last edited by
                              #14

                              u know buffer overflow error when u declare it as t2[3]="Codeproject".... releasing ? what will it do here? V

                              T 1 Reply Last reply
                              0
                              • T toxcct

                                if you can see it, it is pure luck ! it is because - as it's been asnwsered to me here - there is some empty room after the char[3] variable, but never you should think there will always be... moreover, what do you think about releasing the memory ?


                                TOXCCT >>> GEII power
                                [toxcct][VisualCalc] -- modified at 7:26 Monday 29th August, 2005

                                E Offline
                                E Offline
                                Eytukan
                                wrote on last edited by
                                #15

                                u know i get "buffer overflow error" when u declare it as t2[3]="Codeproject".... BTW releasing ? what will it do here? V

                                1 Reply Last reply
                                0
                                • E Eytukan

                                  u know buffer overflow error when u declare it as t2[3]="Codeproject".... releasing ? what will it do here? V

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #16

                                  i think it's a good point to wonder how things work, but there's no reason to do so when you perfectly know it is a bad thing to do... doing t2[3]="Codeproject", it will free t2, but t2 don't have "Codeproject" in it... (the constructor thrown the exception while trying to assigning the string...) (ps: you can delete your other postwritten twice)


                                  TOXCCT >>> GEII power
                                  [toxcct][VisualCalc] -- modified at 7:44 Monday 29th August, 2005

                                  1 Reply Last reply
                                  0
                                  • C Cedric Moonen

                                    To complete the previous answers: It will perhaps output 'Codeproject' but you cannot be sure because strcpy is copying the charcters outside the bounds of t2 (so in 'unprotected memory'). So, this memory is not 'locked' and your program can write other things in it. Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value that can lead to really baaaaaad things ;-) (like your variable changed magically)

                                    E Offline
                                    E Offline
                                    Eytukan
                                    wrote on last edited by
                                    #17

                                    cedric moonen wrote: Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value cedric, if the other variable is declared ,as u said it could reside only in 'protected memory' then how will it(unsure strcpy!) overwrite on a content which is in protected memory?.. plz expln Thanx V -- modified at 7:46 Monday 29th August, 2005

                                    C 1 Reply Last reply
                                    0
                                    • T toxcct

                                      yes, i understood that point. did you visit the link to the msdn i provided ? strcpy() doesn't check for sufficient space in strDestination before copying strSource...


                                      TOXCCT >>> GEII power
                                      [toxcct][VisualCalc] -- modified at 7:34 Monday 29th August, 2005

                                      E Offline
                                      E Offline
                                      Eytukan
                                      wrote on last edited by
                                      #18

                                      ya just got it Tox V

                                      1 Reply Last reply
                                      0
                                      • E Eytukan

                                        cedric moonen wrote: Even worse, because you are writing outside the bounds of t2, it may be that you write on some memory allocated for another variable, thus, erasing it's value cedric, if the other variable is declared ,as u said it could reside only in 'protected memory' then how will it(unsure strcpy!) overwrite on a content which is in protected memory?.. plz expln Thanx V -- modified at 7:46 Monday 29th August, 2005

                                        C Offline
                                        C Offline
                                        Cedric Moonen
                                        wrote on last edited by
                                        #19

                                        Sorry, protected was not really the good word. What I meant is memory allocated for another variable. strcpy will never check 1) that it writes still in the bounds of the string 2) if the memory it writes to is already allocated or not. When you declare a variable, memory is allocated for it to holds its value. So if something write at this location in memory, the value of the variable will altered.

                                        E 1 Reply Last reply
                                        0
                                        • C Cedric Moonen

                                          Sorry, protected was not really the good word. What I meant is memory allocated for another variable. strcpy will never check 1) that it writes still in the bounds of the string 2) if the memory it writes to is already allocated or not. When you declare a variable, memory is allocated for it to holds its value. So if something write at this location in memory, the value of the variable will altered.

                                          E Offline
                                          E Offline
                                          Eytukan
                                          wrote on last edited by
                                          #20

                                          so by no way u can assure that a properly declared variable can never get its value corrupted? so sad..:(... anyway.. thank you so much :-D V

                                          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