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 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
                  • E Eytukan

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

                    K Offline
                    K Offline
                    kakan
                    wrote on last edited by
                    #21

                    t1 and t2 are stack variables. Your allocation makes the compiler to reserve memory on the stack for t1 and t2. The order of the allocation is undefined (I think). So if you are lucky, it the stack allocation starts with t2, followed by t1. You copy the content of t1 to t2, including the terminating \0, and nothing happens. Output will be "Codeproject". (If you output t1, it will be "eproject"). If it´s allocated the other way around, it will still output "Codeproject", but your program will likely go bananas when the function terminates, due to the corrupted stack. (The stack also contains the address to return to, usually the address to the function that called the present function). There are a lot of articles about what´s on the stack, written by people with far more knowledge on the subject than myself. If you want accurate info, pls. read those articles.

                    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

                      M Offline
                      M Offline
                      Marc Soleda
                      wrote on last edited by
                      #22

                      Depends on what you do after the code provided ... all after the third character can be overwritten at any time so, this data, is unsecured. ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

                      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