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. VC++ 6.0 string bug ? (take a look plz)

VC++ 6.0 string bug ? (take a look plz)

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debuggingperformancequestion
30 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.
  • L Offline
    L Offline
    Lockhart
    wrote on last edited by
    #1

    Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.

    C N S T 4 Replies Last reply
    0
    • L Lockhart

      Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What is 'result', coming in ? Has it been created, or just declared ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

      L 1 Reply Last reply
      0
      • C Christian Graus

        What is 'result', coming in ? Has it been created, or just declared ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

        L Offline
        L Offline
        Lockhart
        wrote on last edited by
        #3

        void Buggy( char* result ) in the function declaration, where Buggy stores the result of the appending action.

        C 1 Reply Last reply
        0
        • L Lockhart

          void Buggy( char* result ) in the function declaration, where Buggy stores the result of the appending action.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

          N L 2 Replies Last reply
          0
          • C Christian Graus

            Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

            N Offline
            N Offline
            Neville Franks
            wrote on last edited by
            #5

            As Christian suggests the definition of 'result' is most likely where the problem lie. Debug builds initialize the stack, heap etc. whereas release builds don't, which is one of the reasons they behave differently. Neville Franks, Author of ED for Windows. www.getsoft.com

            L 1 Reply Last reply
            0
            • C Christian Graus

              Yes, but do you say char * pResult; , or do you say char * pResult = new char [255]; , for example ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

              L Offline
              L Offline
              Lockhart
              wrote on last edited by
              #6

              Yes, new char[255], is a string, vector of chars. I use Buggy() this way: void CBugDlg::OnButton1() { char* test = new char[255]; Buggy(test); m_sTest = test; delete [] test; UpdateData(FALSE); } or void CBugDlg::OnButton1() { char test[255]; Buggy(test); m_sTest = test; UpdateData(FALSE); } The bug vanishes if I turn Optimizations\MaximixeSpeed to Optimizations\Default. But this still sucks... I must know why :(

              C 1 Reply Last reply
              0
              • L Lockhart

                Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #7

                are you allocating memory for result??? Nish

                My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org

                L 1 Reply Last reply
                0
                • N Nish Nishant

                  are you allocating memory for result??? Nish

                  My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org

                  L Offline
                  L Offline
                  Lockhart
                  wrote on last edited by
                  #8

                  Yes... try the test project if you can, try Debug and Release, pressing the button: it gives no errors, but two different outputs :( http://digilander.iol.it/ilbanca/fuffa/Bug.zip

                  N 1 Reply Last reply
                  0
                  • L Lockhart

                    Yes, new char[255], is a string, vector of chars. I use Buggy() this way: void CBugDlg::OnButton1() { char* test = new char[255]; Buggy(test); m_sTest = test; delete [] test; UpdateData(FALSE); } or void CBugDlg::OnButton1() { char test[255]; Buggy(test); m_sTest = test; UpdateData(FALSE); } The bug vanishes if I turn Optimizations\MaximixeSpeed to Optimizations\Default. But this still sucks... I must know why :(

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    There is your problem - m_sText = test means both variables point to the same allocated data, which delete [] test will delete. You should also always set deleted pointers to NULL. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

                    L 1 Reply Last reply
                    0
                    • C Christian Graus

                      There is your problem - m_sText = test means both variables point to the same allocated data, which delete [] test will delete. You should also always set deleted pointers to NULL. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

                      L Offline
                      L Offline
                      Lockhart
                      wrote on last edited by
                      #10

                      Sorry, I did not specify, but m_sTest is a CString object (DDX for a CEDit), so it copies 'test' and there should be no problems about it.

                      1 Reply Last reply
                      0
                      • N Neville Franks

                        As Christian suggests the definition of 'result' is most likely where the problem lie. Debug builds initialize the stack, heap etc. whereas release builds don't, which is one of the reasons they behave differently. Neville Franks, Author of ED for Windows. www.getsoft.com

                        L Offline
                        L Offline
                        Lockhart
                        wrote on last edited by
                        #11

                        Ok. But this is an initialization: strcpy(result,"anystring"); Then: strcat(result,s1); should append 's1' to 'result', instead it appends the other string 's2' to 'result'... it has to do with Opt/MazimixeSpeed, that in Debug build is Disabled, but for a such simple task I don't figure out why causes this strange effect...

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          I am puzzled. It worked fine. Both in release and in debug Nish :confused:

                          My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org

                          L Offline
                          L Offline
                          Lockhart
                          wrote on last edited by
                          #12

                          :omg: Grrr.... I'm running WinXP, what your OS ?

                          N N 2 Replies Last reply
                          0
                          • L Lockhart

                            Yes... try the test project if you can, try Debug and Release, pressing the button: it gives no errors, but two different outputs :( http://digilander.iol.it/ilbanca/fuffa/Bug.zip

                            N Offline
                            N Offline
                            Nish Nishant
                            wrote on last edited by
                            #13

                            I am puzzled. It worked fine. Both in release and in debug Nish :confused:

                            My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org

                            L 1 Reply Last reply
                            0
                            • L Lockhart

                              Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.

                              S Offline
                              S Offline
                              Stan Shannon
                              wrote on last edited by
                              #14

                              Memory in debug mode is buffered so that memory overflow conditions that can crash in release mode can work fine in debug mode. My first guess is that 'result' does not point to a block of memory of the size you are expecting. Are you sure you allocated sufficient memory for the trailing null character? result will need to point to at least 16 bytes of memory for this code to work properly ( "anything" = 9 s1 = 6 NULL char = 1.) "There's a slew of slip 'twixt cup and lip"

                              L 1 Reply Last reply
                              0
                              • S Stan Shannon

                                Memory in debug mode is buffered so that memory overflow conditions that can crash in release mode can work fine in debug mode. My first guess is that 'result' does not point to a block of memory of the size you are expecting. Are you sure you allocated sufficient memory for the trailing null character? result will need to point to at least 16 bytes of memory for this code to work properly ( "anything" = 9 s1 = 6 NULL char = 1.) "There's a slew of slip 'twixt cup and lip"

                                L Offline
                                L Offline
                                Lockhart
                                wrote on last edited by
                                #15

                                Yes it was large enough, 255 chars. The problem is that doesnt crash, but display a string contained in another char-vector, as if they did overlapped, I don't know. I thought of a NULL terminating that I could have missed so that the string 1 could extend to string 2 that was declared just after, but it's not the case. Turing Optimizations to Default fixes this bug on my machine, while in other pcs runs fine without altering this setting :(

                                S 1 Reply Last reply
                                0
                                • L Lockhart

                                  Yes it was large enough, 255 chars. The problem is that doesnt crash, but display a string contained in another char-vector, as if they did overlapped, I don't know. I thought of a NULL terminating that I could have missed so that the string 1 could extend to string 2 that was declared just after, but it's not the case. Turing Optimizations to Default fixes this bug on my machine, while in other pcs runs fine without altering this setting :(

                                  S Offline
                                  S Offline
                                  Stan Shannon
                                  wrote on last edited by
                                  #16

                                  Sorry, I did not read the last line in your original post, so I misunderstood the nature of the bug. It sounds to me as though you have a damaged stack. The issue is *still* one of debug mode buffering memory more generously than release mode. You are going to need to trace back through the call stack to figure out where the problem is. Stack overflow bugs can cause all kinds of bizarre issue like this. Good hunting.:) "There's a slew of slip 'twixt cup and lip"

                                  1 Reply Last reply
                                  0
                                  • L Lockhart

                                    :omg: Grrr.... I'm running WinXP, what your OS ?

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #17

                                    Frankesk wrote: Grrr.... I'm running WinXP, what your OS ? I ran it on XP [both debug and release] without any problems at all. Nish

                                    My miniputt high is now 29 I do not think I can improve on that My temperament won't hold www.busterboy.org

                                    1 Reply Last reply
                                    0
                                    • L Lockhart

                                      Here the SIMPLE project demo to illustrate the strange bug I noticed just some minutes ago. http://digilander.iol.it/ilbanca/fuffa/Bug.zip Compile in Debug mode and press the button. Compile in Release and press the button. It beheaves in different ways (bad in Release) The function that made me crazy: [code] void Buggy( char* result ) { char s1[6]; char s2[5]; strcpy(s1,"abcde"); strcpy(s2,"BUG!"); // DOESNT WORK strcpy(result,"anystring"); strcat(result,s1); // THIS WAY WORKS // strcpy(result,s1); } [/code] In Debug mode works In Release mode not Setting 'Optimizations' to "Default" instead of "Maximixe Speed" solve the problem, but why? I want to append 's1' to 'result', and it appends 's2' :(((((((((( Bye.

                                      T Offline
                                      T Offline
                                      Tim Smith
                                      wrote on last edited by
                                      #18

                                      Which SP are you running for VC6? Either SP5 or SP6 is the latest. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                                      L 1 Reply Last reply
                                      0
                                      • T Tim Smith

                                        Which SP are you running for VC6? Either SP5 or SP6 is the latest. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                                        L Offline
                                        L Offline
                                        Lockhart
                                        wrote on last edited by
                                        #19

                                        SP5. Is SP6 out ?

                                        1 Reply Last reply
                                        0
                                        • L Lockhart

                                          :omg: Grrr.... I'm running WinXP, what your OS ?

                                          N Offline
                                          N Offline
                                          Neville Franks
                                          wrote on last edited by
                                          #20

                                          Your code works fine for me as well in both Debug and Release builds on WinXP Pro. That is I "anystringabcde" Have you tried rebooting Windows? If all else fails... Neville Franks, Author of ED for Windows. www.getsoft.com

                                          L 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