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. How to allocate memory for array..?How to delete that array.? Am i correct in this code?

How to allocate memory for array..?How to delete that array.? Am i correct in this code?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancetutorialquestion
8 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.
  • G Offline
    G Offline
    G Haranadh
    wrote on last edited by
    #1

    ... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ... Is this correct code. which I am posting here above. Please let me know?


    Nice talking to you. :-O
    If you judge people, you have no time to love them. -- Mother Teresa

    C T 2 Replies Last reply
    0
    • G G Haranadh

      ... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ... Is this correct code. which I am posting here above. Please let me know?


      Nice talking to you. :-O
      If you judge people, you have no time to love them. -- Mother Teresa

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

      G Haranadh wrote:

      pbyCmd = new BYTE[byCount+1];

      Why +1 ?

      G Haranadh wrote:

      if (pbyCmd) delete pbyCmd;

      No. if (pbyCmd) delete [] pbyCmd;

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      G 1 Reply Last reply
      0
      • G G Haranadh

        ... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ... Is this correct code. which I am posting here above. Please let me know?


        Nice talking to you. :-O
        If you judge people, you have no time to love them. -- Mother Teresa

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

        BYTE *pbyCmd = NULL; //Always allocate a default value,
        // or directly assign a value

        pbyCmd = new BYTE[byCount+1]; //why +1 ?

        if (pbyCmd) {
        delete**[]** pbyCmd;
        pbyCmd = NULL; //this is not done automatically by delete
        // but you should do it for security, and if
        // you want to be able to test if NULL or not
        }


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

        C G P 3 Replies Last reply
        0
        • C Christian Graus

          G Haranadh wrote:

          pbyCmd = new BYTE[byCount+1];

          Why +1 ?

          G Haranadh wrote:

          if (pbyCmd) delete pbyCmd;

          No. if (pbyCmd) delete [] pbyCmd;

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          G Offline
          G Offline
          G Haranadh
          wrote on last edited by
          #4

          Christian Graus wrote:

          Why +1 ?

          This is existing project. And we need one byte extra allocation at that situation. :) Thanks.

          Christian Graus wrote:

          if (pbyCmd) delete [] pbyCmd;

          Thanks:-D. I am replacing this code with this. :cool:


          Nice talking to you. :-O
          If you judge people, you have no time to love them. -- Mother Teresa

          1 Reply Last reply
          0
          • T toxcct

            BYTE *pbyCmd = NULL; //Always allocate a default value,
            // or directly assign a value

            pbyCmd = new BYTE[byCount+1]; //why +1 ?

            if (pbyCmd) {
            delete**[]** pbyCmd;
            pbyCmd = NULL; //this is not done automatically by delete
            // but you should do it for security, and if
            // you want to be able to test if NULL or not
            }


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

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

            Good point, how did I forget it ?

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • T toxcct

              BYTE *pbyCmd = NULL; //Always allocate a default value,
              // or directly assign a value

              pbyCmd = new BYTE[byCount+1]; //why +1 ?

              if (pbyCmd) {
              delete**[]** pbyCmd;
              pbyCmd = NULL; //this is not done automatically by delete
              // but you should do it for security, and if
              // you want to be able to test if NULL or not
              }


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

              G Offline
              G Offline
              G Haranadh
              wrote on last edited by
              #6

              Thanks.:rose:After long time we met:cool:. Ok If i said pbyCmd = new BYTE[1]; then? delete pbyCmd; //or delete[] pbyCmd;


              Nice talking to you. :-O
              If you judge people, you have no time to love them. -- Mother Teresa

              T 1 Reply Last reply
              0
              • G G Haranadh

                Thanks.:rose:After long time we met:cool:. Ok If i said pbyCmd = new BYTE[1]; then? delete pbyCmd; //or delete[] pbyCmd;


                Nice talking to you. :-O
                If you judge people, you have no time to love them. -- Mother Teresa

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

                new -> delete new[] -> delete[]


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

                1 Reply Last reply
                0
                • T toxcct

                  BYTE *pbyCmd = NULL; //Always allocate a default value,
                  // or directly assign a value

                  pbyCmd = new BYTE[byCount+1]; //why +1 ?

                  if (pbyCmd) {
                  delete**[]** pbyCmd;
                  pbyCmd = NULL; //this is not done automatically by delete
                  // but you should do it for security, and if
                  // you want to be able to test if NULL or not
                  }


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

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #8

                  toxcct wrote:

                  if (pbyCmd) { delete[] pbyCmd;

                  Checking for NULL while deleting pointers feels unnecessary. There is no harm deleting NULL pointers. And if its dangled/uninitialized pointer, that will cause problem any way.

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  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