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. deleting strings

deleting strings

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++visual-studiodata-structures
7 Posts 6 Posters 1 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.
  • Z Offline
    Z Offline
    Zizilamoroso
    wrote on last edited by
    #1

    We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


    [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

    J T L A T 5 Replies Last reply
    0
    • Z Zizilamoroso

      We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


      [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

      J Offline
      J Offline
      John Burton
      wrote on last edited by
      #2

      You can't. Well, not without keeping some extra information anywhere. Really this is a design problem, not an implementation problem - it's good practice to allocate and free memory at the same level of a program, so that level already knows how it allocated the memory. For example char* str = new char[100]; somefunction(str); delete[] str; char* str = "Hello world"; somefunction(str); // No need to delete anything The point is that "somefunction" didn't allocate the memory so it can't know how to free it and shouldn't try. Basically whenever you allocate some memory you need to think about how it's going to be freed, and ensure that it's done at the same level as it was allocated, which is probably the only place that knows how to do it.

      1 Reply Last reply
      0
      • Z Zizilamoroso

        We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


        [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

        T Offline
        T Offline
        Tomasz Sowinski
        wrote on last edited by
        #3

        There's no portable way of determining if object is on the heap or not. Switch to std::string or CString and forget about it. Tomasz Sowinski -- http://www.shooltz.com

        *** Vodka. Connecting people. ***

        1 Reply Last reply
        0
        • Z Zizilamoroso

          We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


          [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          You may want to try with IsBadWritePtr. Do some experimentations with it to see how it handles strings (literal and dynamically-allocated strings). Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
          - TreeBeard

          1 Reply Last reply
          0
          • Z Zizilamoroso

            We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


            [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

            A Offline
            A Offline
            Andreas Saurwein
            wrote on last edited by
            #5

            Or, just

            try{ delete[] str; } catch(...){}


            ...make it about Visual C++, and don't ever mention Visual Basic. Nick Hodapp (MSFT) in Semicolon[^]

            Z 1 Reply Last reply
            0
            • Z Zizilamoroso

              We delete an array of char this way: delete[] str; str=NULL But, if the string is created like str=_T("test"); or #define str _T("test"), trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I should strcpy those strings before use, but lazy me...


              [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

              T Offline
              T Offline
              Todd Smith
              wrote on last edited by
              #6

              Try

              const LPCSTR str = _T("test");

              and when the compiler complains about const do as it says. Don't cast the const away :) Todd Smith

              1 Reply Last reply
              0
              • A Andreas Saurwein

                Or, just

                try{ delete[] str; } catch(...){}


                ...make it about Visual C++, and don't ever mention Visual Basic. Nick Hodapp (MSFT) in Semicolon[^]

                Z Offline
                Z Offline
                Zizilamoroso
                wrote on last edited by
                #7

                I used to code java like that! Does it work for c++ too?


                [VISUAL STUDIO 6.0] [MFC] [WIN98/2]

                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