473,804 Members | 2,024 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 2121
"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
1917
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
3151
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 inside the main() method) be collected by the garbage collector after it's been used? Also, how do...
2
1277
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
1498
maxamis4
by: maxamis4 | last post by:
Does anyone know how to create a multi column array?
0
9591
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4306
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
3
3001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.