473,580 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2112
"Jerome" <jl*******@wana doo.fr> wrote in message
news:%2******** ********@TK2MSF TNGP12.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************* ***@tk2msftngp1 3.phx.gbl...
"Jerome" <jl*******@wana doo.fr> wrote in message
news:%2******** ********@TK2MSF TNGP12.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*******@wana doo.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*******@wana doo.fr> wrote in message
news:ec******** ******@TK2MSFTN GP11.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1904
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged? Is 'unsafe' code the same as unmanaged code ?
3
3145
by: Andre | last post by:
if I have an unsafe method that takes in two arrays, performs some operations and returns; I know that the garbage collector will not collect objects allocated inside the unsafe method. However, if I'm calling this method from a main() method of a class which is safe (i.e managed), will the allocated array (which, suppose, has been allocated...
2
1269
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged? Is 'unsafe' code the same as unmanaged code ?
0
1482
maxamis4
by: maxamis4 | last post by:
Does anyone know how to create a multi column array?
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8296
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5349
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3790
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3806
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.