471,306 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

unsafe code and array

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
Jan 4 '06 #1
4 2008
"Jerome" <jl*******@wanadoo.fr> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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
Jan 4 '06 #2
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 news:
Oj****************@tk2msftngp13.phx.gbl...
"Jerome" <jl*******@wanadoo.fr> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Jan 4 '06 #3
"Jerome" <jl*******@wanadoo.fr> a écrit dans le message de news:
ec**************@TK2MSFTNGP11.phx.gbl...

|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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '06 #4
"Jerome" <jl*******@wanadoo.fr> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl...
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
Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

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.