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. Good Programming Practice

Good Programming Practice

Scheduled Pinned Locked Moved C / C++ / MFC
jsonperformancequestion
11 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.
  • P Peter Liddle

    Hey Guys Just a quick question about good programming practice should i delete all variables i use in a function and program or just deallocate memory i have pointed to an initalise the pointers to null and let the rest be deallocated when the function/program drops out. Peter

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #2

    delete everything you allocate. don't let the lazy people tell you different. -c


    //The Mandelbrot set
    o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
    (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
    for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

    Smaller Animals Software

    P C 2 Replies Last reply
    0
    • P Peter Liddle

      Hey Guys Just a quick question about good programming practice should i delete all variables i use in a function and program or just deallocate memory i have pointed to an initalise the pointers to null and let the rest be deallocated when the function/program drops out. Peter

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #3

      What do you mean by "deleting variables" in opposition to "deallocating memory"? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • P Peter Liddle

        Hey Guys Just a quick question about good programming practice should i delete all variables i use in a function and program or just deallocate memory i have pointed to an initalise the pointers to null and let the rest be deallocated when the function/program drops out. Peter

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

        You can only delete the variables you initialised in pointers. This won't work: int i; i = 0; ExternalFunction(i); delete i; // WONT WORK !!! non pointers are deleted for you when they go out of scope. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

        P 1 Reply Last reply
        0
        • C Chris Losinger

          delete everything you allocate. don't let the lazy people tell you different. -c


          //The Mandelbrot set
          o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
          (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
          for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

          Smaller Animals Software

          P Offline
          P Offline
          Peter Liddle
          wrote on last edited by
          #5

          Cheers Chris thats what i needed to know Peter

          1 Reply Last reply
          0
          • C Chris Losinger

            delete everything you allocate. don't let the lazy people tell you different. -c


            //The Mandelbrot set
            o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
            (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
            for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

            Smaller Animals Software

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

            Chris, the way I read his question, he knows to delete pointers, but wants to try and delete variables where he has not allocated any memory dynamically. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

            C 1 Reply Last reply
            0
            • C Christian Graus

              Chris, the way I read his question, he knows to delete pointers, but wants to try and delete variables where he has not allocated any memory dynamically. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #7

              sure. but i did say "delete everything you allocate". which excludes things he didn't allocate. i suppose i'm assuming he knows that "allocate" means "things you had to use 'new', 'malloc', 'GlobalAlloc' and friends to get". maybe i'm wrong. i hope so. i'd like to see what it's like. ;) -c


              //The Mandelbrot set
              o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
              (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
              for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

              Smaller Animals Software

              C 1 Reply Last reply
              0
              • C Christian Graus

                You can only delete the variables you initialised in pointers. This won't work: int i; i = 0; ExternalFunction(i); delete i; // WONT WORK !!! non pointers are deleted for you when they go out of scope. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                P Offline
                P Offline
                Peter Liddle
                wrote on last edited by
                #8

                Ah okay Thats even better, so basically delete anything that is a pointer otherwise if it is just a variable inistialised on the stack i leave it to go out of scope Peter

                P 1 Reply Last reply
                0
                • P Peter Liddle

                  Ah okay Thats even better, so basically delete anything that is a pointer otherwise if it is just a variable inistialised on the stack i leave it to go out of scope Peter

                  P Offline
                  P Offline
                  Peter Liddle
                  wrote on last edited by
                  #9

                  Right i just read Chris Losinger second post and fully get it now in which case i have doen the right thing cause i already deallocted all pointers. The biggest thing was i basically have structures initalised not allocated and was wondering if they needed deleting but obviously since they are initialized and not allocated i just leave them to go out of scope. I have one other question though my structure contains pointers at the moment i am doing delete[] on the pointers in teh structure and then settign the pointer to null and letting the rest go out of scope. Is this the right thing to do? Peter

                  C 1 Reply Last reply
                  0
                  • P Peter Liddle

                    Right i just read Chris Losinger second post and fully get it now in which case i have doen the right thing cause i already deallocted all pointers. The biggest thing was i basically have structures initalised not allocated and was wondering if they needed deleting but obviously since they are initialized and not allocated i just leave them to go out of scope. I have one other question though my structure contains pointers at the moment i am doing delete[] on the pointers in teh structure and then settign the pointer to null and letting the rest go out of scope. Is this the right thing to do? Peter

                    C Offline
                    C Offline
                    Chris Losinger
                    wrote on last edited by
                    #10

                    "delete [] ptr;" when you allocate an array (CString *ptr = new CString[100]). otherwise use "delete ptr;". -c


                    //The Mandelbrot set
                    o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
                    (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
                    for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

                    Smaller Animals Software

                    1 Reply Last reply
                    0
                    • C Chris Losinger

                      sure. but i did say "delete everything you allocate". which excludes things he didn't allocate. i suppose i'm assuming he knows that "allocate" means "things you had to use 'new', 'malloc', 'GlobalAlloc' and friends to get". maybe i'm wrong. i hope so. i'd like to see what it's like. ;) -c


                      //The Mandelbrot set
                      o(int O){putchar(O);}main(){float _[8],O,I=.05;char l;for(_[6]=15;_[6]<':';o
                      (10),_[5]=-'$'*I+_[6]++*I)for(_[7]=-5;_[7]<'@';_[4]=-'('*I+_[7]++*I,o(l?'?':':'))
                      for(*_=O=0,l=1;++l&&((_[2]=*_**_)+(_[3]=O*O)<4);O=*_*O+_[5]+O**_,*_=_[2]-_[3]+_[4]);}

                      Smaller Animals Software

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

                      Chris Losinger wrote: maybe i'm wrong. i hope so. i'd like to see what it's like. *grin* Well, I'd just presume that someone who asks if they need to delete something they did not allocate probably won't understand what 'allocate' means. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                      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