c++ - What happens if i don't free/delete dynamically allocated arrays? -
this code not written bt me! in class webserver overload +=operator. class uses dynamically allocated array of objects of type webpage(another class, composition) defined webpage *wp;
webserver & operator +=( webpage webpage ) { webpage * tmp = new webpage [ count + 1]; (int = 0; < count ; ++) tmp [i] = wp[i]; tmp [ count ++] = webpage ; delete [] wp; wp = tmp; return * ; }
so create new array of dynamically allocated webpages space 1 object, assign them values wp held, , object wanted add array. if remove delete[] wp;
program still works ok. happens if remove line of code? , wp=tmp, mean, wp new name dynamically suit name in class, location in memory still same? or?
so if remove delete[] wp; program still works ok. happens if remove line of code?
you have introduced memory leak. each time operator invoked process waste portion of address space until runs out of memory.
and
wp=tmp
, mean, wp new name dynamically suit name in class, location in memory still same? or?
wp
presumably member (an instance variable) of webserver
holds instances of webpage
objects serves. line replacing previous array of web pages new value (that includes web pages added server).
presumably there other member functions of webserver
read values inside wp
, things them.
as general note, should aware code extremely badly written because not remotely exception safe, it's doing work avoided reasonably smarter implementation , of using homebrew code instead of standard language facilities std::vector
.
Comments
Post a Comment