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. Delete [] charPtr... Pls help

Delete [] charPtr... Pls help

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresperformancetutorialquestion
20 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.
  • C Offline
    C Offline
    c121hains
    wrote on last edited by
    #1

    All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation and I can't find away to delete the original temp data. It works, but..."I'm stomping on other memory!" :wtf: This should be the easiest thing ever. But i'm worried about a memory leak here. Whenever I try to delete allText before creating a NEW allText allocation, it dies. Any ideas how to fix these memory leaks? char* CFindInWindowDlg::GetTextLinesBuffer() { char* allText = NULL; char* source = NULL; int sizeAllText = 0; if (numLines > 0) { // **** 'lines' is my array of CString objects!!! int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; strcpy(source, lines[0].GetBuffer(0)); for (int a = 1; a < numLines; a++) { // **** 'lines' is my array of CString objects!!! sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); source = allText; } } return allText; } int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) { int newSize; if ((sizeSource == 0) && (*source == NULL)) { *allText = new char[strlen(aLine)]; newSize = strlen(aLine); strcpy(*allText, aLine); return newSize; } newSize = sizeSource + strlen(aLine); *allText = new char[newSize]; strcpy(*allText, *source); strcat(*allText, aLine); return newSize; }

    M D L J 4 Replies Last reply
    0
    • C c121hains

      All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation and I can't find away to delete the original temp data. It works, but..."I'm stomping on other memory!" :wtf: This should be the easiest thing ever. But i'm worried about a memory leak here. Whenever I try to delete allText before creating a NEW allText allocation, it dies. Any ideas how to fix these memory leaks? char* CFindInWindowDlg::GetTextLinesBuffer() { char* allText = NULL; char* source = NULL; int sizeAllText = 0; if (numLines > 0) { // **** 'lines' is my array of CString objects!!! int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; strcpy(source, lines[0].GetBuffer(0)); for (int a = 1; a < numLines; a++) { // **** 'lines' is my array of CString objects!!! sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); source = allText; } } return allText; } int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) { int newSize; if ((sizeSource == 0) && (*source == NULL)) { *allText = new char[strlen(aLine)]; newSize = strlen(aLine); strcpy(*allText, aLine); return newSize; } newSize = sizeSource + strlen(aLine); *allText = new char[newSize]; strcpy(*allText, *source); strcat(*allText, aLine); return newSize; }

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

      c121hains wrote: int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; C-style strings are null-terminated, meaning there is a 0 byte to mark the end of the string. strlen() does not include that byte in the length, so you end up not allocating space for that byte. source = new char[sizeAllText**+1**]; --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ I even hear the Windows "OMG I booted up fine" sound.   -- Paul Watson diagnosing hardware problems.

      C 1 Reply Last reply
      0
      • C c121hains

        All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation and I can't find away to delete the original temp data. It works, but..."I'm stomping on other memory!" :wtf: This should be the easiest thing ever. But i'm worried about a memory leak here. Whenever I try to delete allText before creating a NEW allText allocation, it dies. Any ideas how to fix these memory leaks? char* CFindInWindowDlg::GetTextLinesBuffer() { char* allText = NULL; char* source = NULL; int sizeAllText = 0; if (numLines > 0) { // **** 'lines' is my array of CString objects!!! int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; strcpy(source, lines[0].GetBuffer(0)); for (int a = 1; a < numLines; a++) { // **** 'lines' is my array of CString objects!!! sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); source = allText; } } return allText; } int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) { int newSize; if ((sizeSource == 0) && (*source == NULL)) { *allText = new char[strlen(aLine)]; newSize = strlen(aLine); strcpy(*allText, aLine); return newSize; } newSize = sizeSource + strlen(aLine); *allText = new char[newSize]; strcpy(*allText, *source); strcat(*allText, aLine); return newSize; }

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

        c121hains wrote: All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation... Why are you wanting to do this? The whole reason for using CString is so that you would not need to mess with char variables and memory allocation/cleanup. c121hains wrote: int sizeAllText = strlen(lines[0].GetBuffer(0)); strcpy(source, lines[0].GetBuffer(0)); sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); The calls to GetBuffer() are not necessary. Remove them. c121hains wrote: int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) Change the last parameter to const char * instead.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        C 1 Reply Last reply
        0
        • C c121hains

          All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation and I can't find away to delete the original temp data. It works, but..."I'm stomping on other memory!" :wtf: This should be the easiest thing ever. But i'm worried about a memory leak here. Whenever I try to delete allText before creating a NEW allText allocation, it dies. Any ideas how to fix these memory leaks? char* CFindInWindowDlg::GetTextLinesBuffer() { char* allText = NULL; char* source = NULL; int sizeAllText = 0; if (numLines > 0) { // **** 'lines' is my array of CString objects!!! int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; strcpy(source, lines[0].GetBuffer(0)); for (int a = 1; a < numLines; a++) { // **** 'lines' is my array of CString objects!!! sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); source = allText; } } return allText; } int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) { int newSize; if ((sizeSource == 0) && (*source == NULL)) { *allText = new char[strlen(aLine)]; newSize = strlen(aLine); strcpy(*allText, aLine); return newSize; } newSize = sizeSource + strlen(aLine); *allText = new char[newSize]; strcpy(*allText, *source); strcat(*allText, aLine); return newSize; }

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

          Try this: CString strAll = lines[0] + lines[1] ...

          C 1 Reply Last reply
          0
          • D David Crow

            c121hains wrote: All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation... Why are you wanting to do this? The whole reason for using CString is so that you would not need to mess with char variables and memory allocation/cleanup. c121hains wrote: int sizeAllText = strlen(lines[0].GetBuffer(0)); strcpy(source, lines[0].GetBuffer(0)); sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); The calls to GetBuffer() are not necessary. Remove them. c121hains wrote: int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) Change the last parameter to const char * instead.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            C Offline
            C Offline
            c121hains
            wrote on last edited by
            #5

            When I attach the CStrings together (newString = string[0] + string[1] + string[2] ..), the result does not correctly append the strings. My strings contain '\n' and '\r' characters and it seems that when I use the newString += string, only the first result is copied. Here is an example: string1 = "this is a string \r\n"; string2 = "this is an appended string \r\n"; string1 += string2; string1 still equals "this is a string \r\n" and doesn't append string2!! I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.

            D 1 Reply Last reply
            0
            • M Michael Dunn

              c121hains wrote: int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; C-style strings are null-terminated, meaning there is a 0 byte to mark the end of the string. strlen() does not include that byte in the length, so you end up not allocating space for that byte. source = new char[sizeAllText**+1**]; --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ I even hear the Windows "OMG I booted up fine" sound.   -- Paul Watson diagnosing hardware problems.

              C Offline
              C Offline
              c121hains
              wrote on last edited by
              #6

              Actually the code works fine. Everything is allocated the way i want it to. The only problem is deleting. I can't seem to do that.

              1 Reply Last reply
              0
              • L Lost User

                Try this: CString strAll = lines[0] + lines[1] ...

                C Offline
                C Offline
                c121hains
                wrote on last edited by
                #7

                I tried that but it didn't work. Here is an example of what happens: string1 = "this is a string \r\n"; string2 = "this is an appended string \r\n"; string1 += string2; string1 still equals "this is a string \r\n" and doesn't append string2!! I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.

                M 1 Reply Last reply
                0
                • C c121hains

                  When I attach the CStrings together (newString = string[0] + string[1] + string[2] ..), the result does not correctly append the strings. My strings contain '\n' and '\r' characters and it seems that when I use the newString += string, only the first result is copied. Here is an example: string1 = "this is a string \r\n"; string2 = "this is an appended string \r\n"; string1 += string2; string1 still equals "this is a string \r\n" and doesn't append string2!! I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.

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

                  c121hains wrote: ...and doesn't append string2!! How do you know?


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  C 1 Reply Last reply
                  0
                  • C c121hains

                    I tried that but it didn't work. Here is an example of what happens: string1 = "this is a string \r\n"; string2 = "this is an appended string \r\n"; string1 += string2; string1 still equals "this is a string \r\n" and doesn't append string2!! I need those '\r' and '\n' characters. By putting them all into a char string, it fixes this problem.

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

                    c121hains wrote: string1 still equals "this is a string \r\n" and doesn't append string2 How are you determining this? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Come quietly or there will be... trouble.

                    C 2 Replies Last reply
                    0
                    • C c121hains

                      All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. I'm doing memory allocation and I can't find away to delete the original temp data. It works, but..."I'm stomping on other memory!" :wtf: This should be the easiest thing ever. But i'm worried about a memory leak here. Whenever I try to delete allText before creating a NEW allText allocation, it dies. Any ideas how to fix these memory leaks? char* CFindInWindowDlg::GetTextLinesBuffer() { char* allText = NULL; char* source = NULL; int sizeAllText = 0; if (numLines > 0) { // **** 'lines' is my array of CString objects!!! int sizeAllText = strlen(lines[0].GetBuffer(0)); source = new char[sizeAllText]; strcpy(source, lines[0].GetBuffer(0)); for (int a = 1; a < numLines; a++) { // **** 'lines' is my array of CString objects!!! sizeAllText = MergeLines(&allText, sizeAllText, &source, lines[a].GetBuffer(0)); source = allText; } } return allText; } int CFindInWindowDlg::MergeLines(char** allText, int sizeSource, char** source, char* aLine) { int newSize; if ((sizeSource == 0) && (*source == NULL)) { *allText = new char[strlen(aLine)]; newSize = strlen(aLine); strcpy(*allText, aLine); return newSize; } newSize = sizeSource + strlen(aLine); *allText = new char[newSize]; strcpy(*allText, *source); strcat(*allText, aLine); return newSize; }

                      J Offline
                      J Offline
                      Jose Lamas Rios
                      wrote on last edited by
                      #10

                      c121hains wrote: All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. Try this:

                      void MergeLines(const CString* lines, int nLines, CString& result)
                      {
                         // Get total length
                         int nTotalLength = 1; // at least a null terminator will be needed
                         for (int i = 0; i < nLines; i++)
                            nTotalLength += lines[i].GetLength();
                       
                         result.Empty();
                       
                         // Avoid re-allocating and copying in each iteration
                         result.GetBuffer(nTotalLength);
                         result.ReleaseBuffer();
                       
                         // Concatenate all strings into result
                         for (int i = 0; i < nLines; i++)
                            result += lines[i];
                      }

                      Disclaimer: I didn't compile it. May contain some error[s]. -- jlr http://jlamas.blogspot.com/[^]

                      C 1 Reply Last reply
                      0
                      • M Michael Dunn

                        c121hains wrote: string1 still equals "this is a string \r\n" and doesn't append string2 How are you determining this? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Come quietly or there will be... trouble.

                        C Offline
                        C Offline
                        c121hains
                        wrote on last edited by
                        #11

                        In the debugger.. There is no change to the original string: originalString = "the original string \r\n"; originalString += "a new string \r\n"; ------ originalString still equals "the original string \r\n"; Do you get a different result?

                        J 1 Reply Last reply
                        0
                        • C c121hains

                          In the debugger.. There is no change to the original string: originalString = "the original string \r\n"; originalString += "a new string \r\n"; ------ originalString still equals "the original string \r\n"; Do you get a different result?

                          J Offline
                          J Offline
                          Jose Lamas Rios
                          wrote on last edited by
                          #12

                          It should work. Maybe the debugger is showing only the first line. Try doing a TRACE() and see the result in the output window. -- jlr http://jlamas.blogspot.com/[^]

                          C 1 Reply Last reply
                          0
                          • J Jose Lamas Rios

                            c121hains wrote: All i want to do is get all of the text together from an array of CString objects and put it into a single char* pointing to a string. Try this:

                            void MergeLines(const CString* lines, int nLines, CString& result)
                            {
                               // Get total length
                               int nTotalLength = 1; // at least a null terminator will be needed
                               for (int i = 0; i < nLines; i++)
                                  nTotalLength += lines[i].GetLength();
                             
                               result.Empty();
                             
                               // Avoid re-allocating and copying in each iteration
                               result.GetBuffer(nTotalLength);
                               result.ReleaseBuffer();
                             
                               // Concatenate all strings into result
                               for (int i = 0; i < nLines; i++)
                                  result += lines[i];
                            }

                            Disclaimer: I didn't compile it. May contain some error[s]. -- jlr http://jlamas.blogspot.com/[^]

                            C Offline
                            C Offline
                            c121hains
                            wrote on last edited by
                            #13

                            This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                            J 1 Reply Last reply
                            0
                            • J Jose Lamas Rios

                              It should work. Maybe the debugger is showing only the first line. Try doing a TRACE() and see the result in the output window. -- jlr http://jlamas.blogspot.com/[^]

                              C Offline
                              C Offline
                              c121hains
                              wrote on last edited by
                              #14

                              This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                              1 Reply Last reply
                              0
                              • D David Crow

                                c121hains wrote: ...and doesn't append string2!! How do you know?


                                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                                C Offline
                                C Offline
                                c121hains
                                wrote on last edited by
                                #15

                                This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                                J 1 Reply Last reply
                                0
                                • M Michael Dunn

                                  c121hains wrote: string1 still equals "this is a string \r\n" and doesn't append string2 How are you determining this? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Come quietly or there will be... trouble.

                                  C Offline
                                  C Offline
                                  c121hains
                                  wrote on last edited by
                                  #16

                                  This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                                  M 1 Reply Last reply
                                  0
                                  • C c121hains

                                    This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                                    J Offline
                                    J Offline
                                    Jose Lamas Rios
                                    wrote on last edited by
                                    #17

                                    c121hains wrote: This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; [...] return allText.GetBuffer(0); That may seem to work, but it actually doesn't. You can't return a pointer to the internal buffer in allText, since that variable will be destroyed as soon as the function terminates. Check the code I suggested in a previous post (the function receives a CString reference from the caller and stores the result in it). c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? You don't need to use GetBuffer; that's not the problem. -- jlr http://jlamas.blogspot.com/[^]

                                    T 1 Reply Last reply
                                    0
                                    • C c121hains

                                      This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

                                      J Offline
                                      J Offline
                                      Jose Lamas Rios
                                      wrote on last edited by
                                      #18

                                      c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? Ok. I already answered this. :) Again, I suggest you try the implementation I posted before. -- jlr http://jlamas.blogspot.com/[^]

                                      1 Reply Last reply
                                      0
                                      • J Jose Lamas Rios

                                        c121hains wrote: This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; [...] return allText.GetBuffer(0); That may seem to work, but it actually doesn't. You can't return a pointer to the internal buffer in allText, since that variable will be destroyed as soon as the function terminates. Check the code I suggested in a previous post (the function receives a CString reference from the caller and stores the result in it). c121hains wrote: Any idea why I need to use GetBuffer instead of string1 += string2? You don't need to use GetBuffer; that's not the problem. -- jlr http://jlamas.blogspot.com/[^]

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

                                        A classic case of "MFC must be broken because my code works just fine." Wrong. :) Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                        1 Reply Last reply
                                        0
                                        • C c121hains

                                          This works: char* CFindInWindowDlg::GetTextLinesBuffer() { CString allText = ""; for (int a = 0; a < numLines; a++) { allText += lines[a].GetBuffer(0); lines[a].ReleaseBuffer(); } return allText.GetBuffer(0); } Any idea why I need to use GetBuffer instead of string1 += string2?

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

                                          Well, that only "works" by chance. You're returning a pointer to the local variable allText which immediately goes out of scope. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ I even hear the Windows "OMG I booted up fine" sound.   -- Paul Watson diagnosing hardware problems.

                                          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