unsafe code and array

J

Jerome

Hi all,

I *have* to use unsafe code, but I have a questions : it seems I can't use
delete to free something like : int * myarray = new int[size].
What should I do if I wanna create/delte/resize such an array ? I'm afraid I
didn't get all about the unsafe code !
Clearly, how should I use the unsafe code to do something like that (in c++)
:
....
if(myArray != null)
delete[] myArray;
myArray = new int[newsize]
....

Thank you for your help !

Jérôme
 
R

Richard Blewett [DevelopMentor]

Jerome said:
Hi all,

I *have* to use unsafe code, but I have a questions : it seems I can't use
delete to free something like : int * myarray = new int[size].
What should I do if I wanna create/delte/resize such an array ? I'm afraid
I didn't get all about the unsafe code !
Clearly, how should I use the unsafe code to do something like that (in
c++) :
...
if(myArray != null)
delete[] myArray;
myArray = new int[newsize]
...

Thank you for your help !

Jérôme

why do you need to use unsafe code?

You don;t need to delete here because .NET is a GC'd environment and the
allocated array memory will get garbage collected once its eligible.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
J

Jerome

I agree, but maybe i missed something important : what happens if you use
the new operator 2 times on the same object ?
for instance :
int[] myArray = new int[50];
myArray = new int[25];

Can I assume it'll create a first array of 50 int, then delete it a create
an array of 25 int and GC will manage the whole memory proccess?

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> a écrit dans le message de OjGN%[email protected]...
Jerome said:
Hi all,

I *have* to use unsafe code, but I have a questions : it seems I can't
use delete to free something like : int * myarray = new int[size].
What should I do if I wanna create/delte/resize such an array ? I'm
afraid I didn't get all about the unsafe code !
Clearly, how should I use the unsafe code to do something like that (in
c++) :
...
if(myArray != null)
delete[] myArray;
myArray = new int[newsize]
...

Thank you for your help !

Jérôme

why do you need to use unsafe code?

You don;t need to delete here because .NET is a GC'd environment and the
allocated array memory will get garbage collected once its eligible.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
J

Joanna Carter [TeamB]

"Jerome" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I agree, but maybe i missed something important : what happens if you use
| the new operator 2 times on the same object ?
| for instance :
| int[] myArray = new int[50];
| myArray = new int[25];
|
| Can I assume it'll create a first array of 50 int, then delete it a create
| an array of 25 int and GC will manage the whole memory proccess?

Yes, otherwise the Array.Resize method couldn't work.

Joanna
 
R

Richard Blewett [DevelopMentor]

Jerome said:
I agree, but maybe i missed something important : what happens if you use
the new operator 2 times on the same object ?
for instance :
int[] myArray = new int[50];
myArray = new int[25];

Can I assume it'll create a first array of 50 int, then delete it a create
an array of 25 int and GC will manage the whole memory proccess?

You can't use the new operator on the same *object* the new operator creates
a new object. What you are referring to is assigning the new object to the
same *reference*. And what happens in that case?

Well assuming that the no other references exist to the old array then next
time the GC runs it will collect it (this is a simplification of the process
but in principle this is what happens).

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top