473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about deleting objects in C#

5
Hello everyone, I just had a question about deleting objects in C#.

For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it.

string[] array = new string[] {"aa", "bb", "cc"};
string[] temp = array;

array = new string[temp.length+1];
array[3] = "dd";

So before the line "array = new string[temp.length+1];" do I have to delete the old array data first to prevent memory leak? Thanks!
Nov 1 '07 #1
9 1665
r035198x
13,262 8TB
Hello everyone, I just had a question about deleting objects in C#.

For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it.

string[] array = new string[] {"aa", "bb", "cc"};
string[] temp = array;

array = new string[temp.length+1];
array[3] = "dd";

So before the line "array = new string[temp.length+1];" do I have to delete the old array data first to prevent memory leak? Thanks!
Throw everything away and use an ArrayList instead.
Nov 1 '07 #2
Hello everyone, I just had a question about deleting objects in C#.

For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it.

string[] array = new string[] {"aa", "bb", "cc"};
string[] temp = array;

array = new string[temp.length+1];
array[3] = "dd";

So before the line "array = new string[temp.length+1];" do I have to delete the old array data first to prevent memory leak? Thanks!
you could get rid of the old array, but there really is no need since there is a garbage collector, no need to worry for such things....you might want to leave temp pointing to null after copying it, so the garbage collector takes care of it faster.
Nov 1 '07 #3
Plater
7,872 Expert 4TB
Doing this part:
array = new string[temp.length+1];
array[3] = "dd";

will wipe out what you had with [0] [1] and [2] (the "aa" "bb" and "cc")


If you want to add/remove items from an array dynamically, look at the Systems.Collections namespace and pick something out of there. ArrayList, LinkedList any number of them
Nov 1 '07 #4
n00b
5
So with the implementation that I have,
ie" array = new string[temp.length+1] does that actually make a new string array in dynamic memory?

Doing this part:
array = new string[temp.length+1];
array[3] = "dd";

will wipe out what you had with [0] [1] and [2] (the "aa" "bb" and "cc")


If you want to add/remove items from an array dynamically, look at the Systems.Collections namespace and pick something out of there. ArrayList, LinkedList any number of them
Err sorry I forgot a for loop in there that copies the 3 elements from temp into array.
Oh man I totally forgot that C# has a built in Linked List library, lol I was thinking of using one but didn't wanna create it by scratch. Thanks for the input everyone!
Nov 1 '07 #5
r035198x
13,262 8TB
So with the implementation that I have,
ie" array = new string[temp.length+1] does that actually make a new string array in dynamic memory?



Err sorry I forgot a for loop in there that copies the 3 elements from temp into array.
Oh man I totally forgot that C# has a built in Linked List library, lol I was thinking of using one but didn't wanna create it by scratch. Thanks for the input everyone!
Yep, that is generally the meaning of "new".
B.T.W You want to use ArrayList for this not LinkedList
Nov 1 '07 #6
n00b
5
Yep, that is generally the meaning of "new".
B.T.W You want to use ArrayList for this not LinkedList
Ah okay that's what I thought, just wanted to double check.

How do you delete the old object? I know in C++ there's a delete command you can type that does that, but doesn't seem to be the case for C#. Or is it just setting it to null?
Nov 1 '07 #7
Plater
7,872 Expert 4TB
Setting an object to null tells the garbage collector that you're done with it and to free up the memory you are using.
(Provided there are not certain circumstances, such as an open file handle or open stream or socket)

Some object also contain a .Dispose() which does a good job at clearing up the memory too.
You can also force a collection with GC.Collect().
Nov 1 '07 #8
n00b
5
Setting an object to null tells the garbage collector that you're done with it and to free up the memory you are using.
(Provided there are not certain circumstances, such as an open file handle or open stream or socket)

Some object also contain a .Dispose() which does a good job at clearing up the memory too.
You can also force a collection with GC.Collect().
Ah i see that sounds good. Thanks everyone for their help!
Nov 1 '07 #9
r035198x
13,262 8TB
Just thought I'd add that calling GC.Collect() simply marks legible objects as free. The actual memory release is done if the OS needs the memory for other programs. In essence, one doesn't know the exact moment that memory is released for a particular object.
Nov 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chaman Singh Verma | last post by:
Hello, I am trying to integrate C++ with Python. I read that Python does automatic garbage collection. I am creating new objects in C++ and passing to Python, I don't know now who should control...
2
by: Eric | last post by:
Hi, I'm used to C/C++ where if you "new" something you really need to "delete" it later. Is that also true in javascript? if i do "mydate = new date();" in a function and dont "delete mydate" when...
22
by: Nunaya | last post by:
We have developed two objects called "customer" and "salesrep". For simplicity, assume that these two objects will talk directly to a database. The database has three tables: customers,...
5
by: deancoo | last post by:
Being fairly new to STL, I'm looking for a little advice on which container to use. Let me set up the scenario for you to consider. Suppose you're trying to model a hotel. The hotel has a fixed...
7
by: Danny J. Lesandrini | last post by:
I know this has been discussed before, as I've seen the Google posts, but they all leave me with an unanswered question: Does my DAO code executed in the front end cause the back end to bloat?...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
0
by: Terry Reedy | last post by:
"Jacob Davis" <j.foster.davis@gmail.comwrote in message news:C673A5C7-9971-4CAA-8CEB-3993C3E93F9C@gmail.com... | I read in some earlier messages that an object in Python is only | removed or freed...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.