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. Weird Character Allocation Problem.

Weird Character Allocation Problem.

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebugging
6 Posts 4 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.
  • H Offline
    H Offline
    HAHAHA_NEXT
    wrote on last edited by
    #1

    I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).

    A N 2 Replies Last reply
    0
    • H HAHAHA_NEXT

      I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      HAHAHA_NEXT wrote: _T("Anton"); is a pointer to STATIC memory, u can't free it! HAHAHA_NEXT wrote: ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK this would rather be delete [] ptszVersionInfo; Don't try it, just do it! ;-)

      H 1 Reply Last reply
      0
      • A Alexander M

        HAHAHA_NEXT wrote: _T("Anton"); is a pointer to STATIC memory, u can't free it! HAHAHA_NEXT wrote: ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK this would rather be delete [] ptszVersionInfo; Don't try it, just do it! ;-)

        H Offline
        H Offline
        HAHAHA_NEXT
        wrote on last edited by
        #3

        Is there a way i can allocate _T("Anton") Into a dynamic memory ? because my code is all mixed up. (I allocate the string using both methods).

        D 1 Reply Last reply
        0
        • H HAHAHA_NEXT

          Is there a way i can allocate _T("Anton") Into a dynamic memory ? because my code is all mixed up. (I allocate the string using both methods).

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

          HAHAHA_NEXT wrote: Is there a way i can allocate _T("Anton") Into a dynamic memory ? This does not quite make sense, but I think what you want is:

          TCHAR *p = new TCHAR[6];
          _tcscpy(p, _T("Anton"));
          ...
          delete [] p;


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          H 1 Reply Last reply
          0
          • D David Crow

            HAHAHA_NEXT wrote: Is there a way i can allocate _T("Anton") Into a dynamic memory ? This does not quite make sense, but I think what you want is:

            TCHAR *p = new TCHAR[6];
            _tcscpy(p, _T("Anton"));
            ...
            delete [] p;


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            H Offline
            H Offline
            HAHAHA_NEXT
            wrote on last edited by
            #5

            Ths. It is exactly what i needed. The reason for this kind of weird stuff is because i can allocate the same string 2 ways: the first is p = "Anton" and the second is p = new TCHAR[6]; _stprintf(p, ....) This will avoid me having different variable and one clean delete statement :).

            1 Reply Last reply
            0
            • H HAHAHA_NEXT

              I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).

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

              HAHAHA_NEXT wrote: ... delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; Two problems: 1) You are using ptszVersionInfo after you've done a delete on it. 2) You are doing a second delete on a ptr that you've already done a delete on. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

              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