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!
9 1665
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.
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.
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
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!
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
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?
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().
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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?...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |