Archived

This topic is now archived and is closed to further replies.

grady

Resizing an array in C#

Recommended Posts

Will the miraculous garbage collector understand what has just occured: double [,] mat; mat = new double[3,3]; mat = new double[2,2];

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why not simply delete it before you call new again? Is it really that hard to get into the habbit of freeing up memory that you allocate?

Billy - BillyB@mrsnj.com

ps. With something so simple, why would you want to rely on some GARBAGE collector? Just don''t write garbage and you won''t have to worry about it cleaning up after you .

Share this post


Link to post
Share on other sites
So I should use the delete statement that doesn't exist, right?

quote:
ps. With something so simple, why would you want to rely on some GARBAGE collector? Just don't write garbage and you won't have to worry about it cleaning up after you


That is an example so that you will understand basically how I am guessing you resize an array in C#. I don't want to just keep allocating memory without doing anything in the actual program. I regret to inform you that some how you have missed the point of this example. Maybe you should try to build up your intellegence as best as you can before you give anymore advice.

Edited by - grady on February 20, 2002 8:50:08 PM

Share this post


Link to post
Share on other sites
Yes, if you declare a variable, and initialize it then re-initialize it, the GC will view the entire first initialization as collectable...it won''t preserve your objects in the original array.


Epolevne

Share this post


Link to post
Share on other sites
Just guessing, but I''d say that if that code occurs exactly as you have it, that it won''t even allocate the 3,3 array so there''s no issue of garbage collection.

Share this post


Link to post
Share on other sites
What Epolevne stated is true - the first array will appear to the gc as unreachable from any of the application roots, and will therefore be collected.
It is no different from this:
  
Foo foo = new Foo("bleh");
//...

foo = new Foo("blah");

The "bleh" object will not be reachable from any of the roots(foo being the only root that ever pointed to it), and will be collected.


The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
Right, I don''t actually not use the 2x2 in the program, I have a matrix class and compute a determinant for it. But the user can keep on calculating determinants as much as they want and these repitive calculations all use the same Matrix object. So it will be resized every time the user wants to input another matrix. Thanks alot for all the help.

Share this post


Link to post
Share on other sites