GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2009, 12:58
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

C# - freeing memory allocated by the 'new' operator


I can use 'new' to allocate memory to the same pointer more than once.
I don't see any way to free an allocation before requesting another.
Is this taken care of by this 'garbage collection' stuff I keep hearing about?
If so , I still haven't recieved the bill. What is the billing cycle?
Seriously ,
Does that happen automatically when I make the new request?
Is there something wrong with cleaning up my own trash?
example:
C-SHARP / C# Code:
using System;

class MyProg
{
static void Main(string[] args)
{
  double [] darr1;        // hmmm by itself gets: declared but never used...
                          // hey , it's a pointer   
  darr1 = new double[8];  // and now we assign addr of block to it... OK

  darr1[4] = 4.567;       // place a value in there

  foreach( double i in darr1 )  // see 8 elements and 5th is 4.567 OK
    Console.Write("{0} ", i);
  Console.Write("\n");

  darr1 = new double[16];       // get new allocation! What happens to old ???

  foreach( double i in darr1 )  // see 16 zeros
    Console.Write("{0} ", i);
  Console.Write("\n");
}
}
Output:
Code:
~> mcs array_declaration_scoping.cs ~> mono array_declaration_scoping.exe 0 0 0 0 4.567 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  #2  
Old 15-Oct-2009, 13:02
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: C# - freeing memory allocated by the 'new' operator


Yes, it is done by the garbage collector. If you want to clean up after an object before the garbage collector gets to it, you can use the Dispose() method of the object if it implements IDisposable.
  #3  
Old 15-Oct-2009, 20:50
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: C# - freeing memory allocated by the 'new' operator


Thanks
I read up a bit on Dispose etc. Interesting.
They talk like I should not worry about arrays as the GC will clean it up if needed.
How handy...
  #4  
Old 16-Oct-2009, 06:27
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: C# - freeing memory allocated by the 'new' operator


Yeah, it makes going back to C++ very difficult for those that learn with C# because they don't understand about freeing resources. Luckily for us, we learned C/C++ before C#.
  #5  
Old 16-Oct-2009, 07:02
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,007
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: C# - freeing memory allocated by the 'new' operator


Personally , for the C# gods to think that we NEED to have GC running around wiping my butt to the point that they don't even give us an easy to use deallocation method because WE might not use it correctly is flippin insulting. Managed code... managed people , like cattle. ms...
  #6  
Old 03-Dec-2009, 04:05
joshuab joshuab is offline
New Member
 
Join Date: Dec 2009
Posts: 1
joshuab is an unknown quantity at this point

Re: C# - freeing memory allocated by the 'new' operator


In .Net frame work the deleting or disposing of objects are taken care by the garbage collector.
So need to worry about your trash.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Complete Guide to SD Memory Cards haulkook Computer Hardware Forum 0 01-Jul-2008 23:27
A Few Tips on Buying Good Memory Cards and other Hi-Tech Electronic Accessories haulkook Computer Hardware Forum 0 02-Jun-2008 03:53
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:05.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.