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. Strange string problem ...

Strange string problem ...

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelp
14 Posts 7 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    hi, in my code i do the following thing char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå", 16); So, the content of my pByte pointer should be "çj]%Þ6f$.JÛ.$(ñå" (and all following characters until editor reach a null-terminated string character). But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char * :doh: I don't understand why my content is modified. I'm using VC++ 2003. On another project i do exactly the same thing and my pointer is not modified. the two project have exactly the same settings ... This problem make me crazy X|, all suggestions are welcome. Thanks.

    C M R M 4 Replies Last reply
    0
    • L Lost User

      hi, in my code i do the following thing char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå", 16); So, the content of my pByte pointer should be "çj]%Þ6f$.JÛ.$(ñå" (and all following characters until editor reach a null-terminated string character). But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char * :doh: I don't understand why my content is modified. I'm using VC++ 2003. On another project i do exactly the same thing and my pointer is not modified. the two project have exactly the same settings ... This problem make me crazy X|, all suggestions are welcome. Thanks.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Actually your code works fine on my system, the 16 characters of pByte being initialized to "çj]%Þ6f$.JÛ.$(ñå" of course the debugger shows some garbage at the end, since the string is not 0 terminated. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      R L 2 Replies Last reply
      0
      • L Lost User

        hi, in my code i do the following thing char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå", 16); So, the content of my pByte pointer should be "çj]%Þ6f$.JÛ.$(ñå" (and all following characters until editor reach a null-terminated string character). But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char * :doh: I don't understand why my content is modified. I'm using VC++ 2003. On another project i do exactly the same thing and my pointer is not modified. the two project have exactly the same settings ... This problem make me crazy X|, all suggestions are welcome. Thanks.

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        Ahryman40k wrote:

        But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char *

        How are you verifying this?

        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

        L 1 Reply Last reply
        0
        • L Lost User

          hi, in my code i do the following thing char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå", 16); So, the content of my pByte pointer should be "çj]%Þ6f$.JÛ.$(ñå" (and all following characters until editor reach a null-terminated string character). But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char * :doh: I don't understand why my content is modified. I'm using VC++ 2003. On another project i do exactly the same thing and my pointer is not modified. the two project have exactly the same settings ... This problem make me crazy X|, all suggestions are welcome. Thanks.

          M Offline
          M Offline
          MANISH RASTOGI
          wrote on last edited by
          #4

          First check memory is allocated or not. Then check ASCII value stored in pByte memory location in memory debug window.

          L 1 Reply Last reply
          0
          • C CPallini

            Actually your code works fine on my system, the 16 characters of pByte being initialized to "çj]%Þ6f$.JÛ.$(ñå" of course the debugger shows some garbage at the end, since the string is not 0 terminated. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            R Offline
            R Offline
            ramana g
            wrote on last edited by
            #5

            "the debugger shows some garbage at the end, since the string is not 0 terminated" as Pallini told, this what is happening in your case. you better increase your buffer length to 17 and check again, then the debugger shows the string properly.

            L 1 Reply Last reply
            0
            • R ramana g

              "the debugger shows some garbage at the end, since the string is not 0 terminated" as Pallini told, this what is happening in your case. you better increase your buffer length to 17 and check again, then the debugger shows the string properly.

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

              The problem is not what the debugger diplay, but the content displayed. i know my data is not null-terminated, but each character should be the same as the original string.

              D 1 Reply Last reply
              0
              • C CPallini

                Actually your code works fine on my system, the 16 characters of pByte being initialized to "çj]%Þ6f$.JÛ.$(ñå" of course the debugger shows some garbage at the end, since the string is not 0 terminated. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

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

                This code works well on another project too. i'm looking for a preprocessor or an option who should modify my string but i have no idea.

                C 1 Reply Last reply
                0
                • L Lost User

                  This code works well on another project too. i'm looking for a preprocessor or an option who should modify my string but i have no idea.

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

                  That code must work. Are you sure there aren't aother statements between assignment and check?

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  L 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    Ahryman40k wrote:

                    But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char *

                    How are you verifying this?

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

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

                    With the VC++ 2003 debugger. In step by step debug mode. Just after memcpy, i'm looking the content of my pointer. but datas allocated are different from original datas. I'm looking for an option or a preprocessor whitch could modify datas ... This problem appears in a project that i don't maintain. In this project when copying "ç" character in a char pointer, the result become "ç". I have all code sources. the whole project is well compilated. I don't know what it happens in this project ...

                    1 Reply Last reply
                    0
                    • C CPallini

                      That code must work. Are you sure there aren't aother statements between assignment and check?

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

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

                      This is my problem. It MUST work but it doesn't work ! :s no override on new operator. just this simple code: char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå",17); who do not its job ! Is there any VC++ options who can change this ?? ( this is not a UNICODE problem and i really have no idea where the problem comes from )

                      1 Reply Last reply
                      0
                      • L Lost User

                        The problem is not what the debugger diplay, but the content displayed. i know my data is not null-terminated, but each character should be the same as the original string.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Ahryman40k wrote:

                        i know my data is not null-terminated...

                        Then you know why it appears wrong. The terminating nul character has been overwritten.

                        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        1 Reply Last reply
                        0
                        • M MANISH RASTOGI

                          First check memory is allocated or not. Then check ASCII value stored in pByte memory location in memory debug window.

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

                          Memory is well allocated, the value stored in memory location is provided in the first post.

                          1 Reply Last reply
                          0
                          • L Lost User

                            hi, in my code i do the following thing char* pByte = new char[16]; memset( pByte, 0, 16 ); memcpy( pByte,"çj]%Þ6f$.JÛ.$(ñå", 16); So, the content of my pByte pointer should be "çj]%Þ6f$.JÛ.$(ñå" (and all following characters until editor reach a null-terminated string character). But if i check in memory what my pointer contains, i see the following value : + pByte 0x04b78f98 "çj]%Ãz6f$.JÃ>.$ýýýý««««««««îþîþ" char * :doh: I don't understand why my content is modified. I'm using VC++ 2003. On another project i do exactly the same thing and my pointer is not modified. the two project have exactly the same settings ... This problem make me crazy X|, all suggestions are welcome. Thanks.

                            M Offline
                            M Offline
                            Michael Dunn
                            wrote on last edited by
                            #13

                            Your source code file has been saved as UTF-8, but the compiler is reading it as Windows-1252 or whatever your local code page is. Don't use characters outside of ASCII in source code, use the \x escape instead (or \u if your compiler supports it).

                            --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ I work for Keyser Söze

                            L 1 Reply Last reply
                            0
                            • M Michael Dunn

                              Your source code file has been saved as UTF-8, but the compiler is reading it as Windows-1252 or whatever your local code page is. Don't use characters outside of ASCII in source code, use the \x escape instead (or \u if your compiler supports it).

                              --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ I work for Keyser Söze

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

                              Michael Dunn wrote:

                              Your source code file has been saved as UTF-8, but the compiler is reading it as Windows-1252 or whatever your local code page is. Don't use characters outside of ASCII in source code, use the \x escape instead (or \u if your compiler supports it).

                              You're right, my source file was save as UTF-8, that's why what i see was different than what my string appenned. Thank you. :laugh:

                              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