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 operator

delete operator

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
7 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.
  • I Offline
    I Offline
    iayd
    wrote on last edited by
    #1

    Hi, I wrote this code in the Timer which is giving an error while it is running.

    char \*wFile = new char\[22\];
    wFile = "010B000304140601000000";
    WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
    delete\[\] wFile;
    

    I know that delete operator causes this error.How can I solve this? Thanks

    T C J C D 5 Replies Last reply
    0
    • I iayd

      Hi, I wrote this code in the Timer which is giving an error while it is running.

      char \*wFile = new char\[22\];
      wFile = "010B000304140601000000";
      WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
      delete\[\] wFile;
      

      I know that delete operator causes this error.How can I solve this? Thanks

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      iayd wrote:

      is giving an error

      WHICH error ? how can we guess if you don't tell us all we need to know ?

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      1 Reply Last reply
      0
      • I iayd

        Hi, I wrote this code in the Timer which is giving an error while it is running.

        char \*wFile = new char\[22\];
        wFile = "010B000304140601000000";
        WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
        delete\[\] wFile;
        

        I know that delete operator causes this error.How can I solve this? Thanks

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Woaw, that's very bad code X| First, you allocate a buffer of 22 characters and tries to assign 22 characters in it (you didn't take into consideration the terminating zero). BUT, you in fact don't copy the string into your buffer. You simply just assign a pointer into wFile (your string is in fact a pointer). Later you try to delete this pointer, which of course will fail because your string was not allocated on the heap (not allocated with new). So, two things to take into consideration: 1) Always make your buffer 1 place larger to be able to store the null-terminating zero 2) Use strcpy whenever you want to copy a string. This will copy the contents of your string and not simply make a pointer assignement.

        Cédric Moonen Software developer
        Charting control [v1.4]

        1 Reply Last reply
        0
        • I iayd

          Hi, I wrote this code in the Timer which is giving an error while it is running.

          char \*wFile = new char\[22\];
          wFile = "010B000304140601000000";
          WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
          delete\[\] wFile;
          

          I know that delete operator causes this error.How can I solve this? Thanks

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #4

          strlen is expecting a null terminated string, your new statement allocates 22 entries, your assignment fills all 22 places with non null data, I think you have to make the first line something like char[23] enough space to include the terminating null character.

          1 Reply Last reply
          0
          • I iayd

            Hi, I wrote this code in the Timer which is giving an error while it is running.

            char \*wFile = new char\[22\];
            wFile = "010B000304140601000000";
            WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
            delete\[\] wFile;
            

            I know that delete operator causes this error.How can I solve this? Thanks

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

            iayd wrote:

            wFile = "010B000304140601000000";

            The above line is a mistake. You should do

            char *wFile = new char[23];
            strcpy(wFile, "010B000304140601000000");

            BTW even correct, the above code is wasteful, using a two lines you can do all the stuff:

            const char * pStr = "010B000304140601000000";
            WriteFile(HD,(LPVOID) pStr , strlen(pStr),&Bytes,NULL);

            :)

            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

            modified on Friday, May 16, 2008 10:49 AM

            1 Reply Last reply
            0
            • I iayd

              Hi, I wrote this code in the Timer which is giving an error while it is running.

              char \*wFile = new char\[22\];
              wFile = "010B000304140601000000";
              WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL);
              delete\[\] wFile;
              

              I know that delete operator causes this error.How can I solve this? Thanks

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

              iayd wrote:

              char *wFile = new char[22];

              wFile points to an address that can hold 22 characters.

              iayd wrote:

              wFile = "010B000304140601000000";

              You've now assigned wFile to a different address.

              iayd wrote:

              delete[] wFile;

              delete is expecting to delete from the address returned from new, but will fail because wFile points someplace else.

              "Love people and use things, not love things and use people." - Unknown

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

              I 1 Reply Last reply
              0
              • D David Crow

                iayd wrote:

                char *wFile = new char[22];

                wFile points to an address that can hold 22 characters.

                iayd wrote:

                wFile = "010B000304140601000000";

                You've now assigned wFile to a different address.

                iayd wrote:

                delete[] wFile;

                delete is expecting to delete from the address returned from new, but will fail because wFile points someplace else.

                "Love people and use things, not love things and use people." - Unknown

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

                I Offline
                I Offline
                iayd
                wrote on last edited by
                #7

                Thank you all.

                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