(C++) Constructers, Deconstructers?

Started by
11 comments, last by HydroX 22 years, 3 months ago
Then I suppose, DrPizza, that you'd be surprised how incredibly often I see people get it messed up. Beyond that, why dynamically allocate when you simply don't need to? I'm not saying that you should never do it, but just do it when it's actually necessary to avoid the possibility of screw-ups—since it's impossible to screw up stack-based objects in this regard, and possible to screw up dynamically-allocated ones, the latter are intrinsically and inarguably more "risky."

Edited by - merlin9x9 on February 7, 2002 11:24:33 PM
Advertisement
thx alot guys!
I understand now
quote:Original post by merlin9x9
Then I suppose, DrPizza, that you''d be surprised how incredibly often I see people get it messed up.

Yes, really. Unless you''re doing something where you can''t simply write the delete after the new, there''s no reason to write memory leaks.

If you''re doing something more complex (for instance, I have code where I have two threads communicating through an I/O completion port; one thread creates objects and posts pointers to them to the port, the other thread waits on the port and manipulates the objects then deletes them -- there is complication because the allocation and deallocation are necessarily divorced from each other), you have to be more careful, certainly. But for most cases, it''s as simple as writing the delete straight away.

quote:Beyond that, why dynamically allocate when you simply don''t need to? I''m not saying that you should never do it, but just do it when it''s actually necessary to avoid the possibility of screw-ups—since it''s impossible to screw up stack-based objects in this regard, and possible to screw up dynamically-allocated ones, the latter are intrinsically and inarguably more "risky."

Because running out of stack space is generally more likely than fragmenting my memory space such that I can no longer allocate more memory.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement