Good Programming Practice
-
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
-
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
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]);} -
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
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
-
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
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
-
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]);}Cheers Chris thats what i needed to know Peter
-
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]);}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
-
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
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]);} -
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
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
-
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
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
-
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
"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]);} -
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]);}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