473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using unsafe will be faster?

Hi,
NOTE: in the following code Rectangle is a struct inside the System.Drawing
namespace.

I'v a following code:
Rectangle GetFillRectangle(ref Rectangle rect)
{

Rectangle newrect;
//calculate fill dimension for the rect passed and return the new one

return newrect;
}

If I write it using pointers will it be faster? Like:
unsafe Rectangle * GetFillRectangle(ref Rectangle rect)
{
Rectangle newrect;
//calculate fill dimension for the rect passed and return the new one

return & newrect;

}

My confusion: when I write the following code (using the first version of
the function):

....
Rectangle fillrect= GetFillRectangle(ref somerectangle);

fillrect, above, will get a "copy" of the newrect or just a reference of the
newrect created inside the GetFillRectangle() function? My understanding
says that it'll get a "copy" of the newrect because its a value type, and if
this is so there are actually 2 rectangle objects being created and only one
is getting used the first one gets gc. And if this all is true than if I
write the following code:
Rectangle * rect = GetFillRectangle(ref somerectangle);

rect pointer will get a direct reference to the newrect rectangle object
created inside the GetFillRectangle() function. So I'm saved from extra time
& memory that would have taken to create a copy of the rectangle object. Is
that true?
Nov 15 '05 #1
2 1372
What you say is alright. But instead of using unsafe code, you can achieve same effect by using an out parameter.
Nov 15 '05 #2
oh thats greate, thanx I just forgot out, I'll use that.

"Blomberg" <an*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
What you say is alright. But instead of using unsafe code, you can achieve

same effect by using an out parameter.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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?...
3
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...
2
by: mmmobasher | last post by:
Dear sirs after some googling i found some code to using unrar.dll, but i get runtime error System.NullReferenceException in line iStatus=RARReadHeader(lHandle, ref uHeader); hear is the code ...
6
by: Einar Høst | last post by:
Hi I'm trying to learn a bit about performance, hope someone can help me out I have a text file with 8-bit characters in it. In order to improve performance, I'm using a BinaryReader instead of...
1
by: Z | last post by:
Hello, In my C# program I have a function that uses pointers. Is there a way to tell the compiler to compile only this function with the unsafe switch and compile the remaining project as safe...
7
by: TomHL | last post by:
hello, - I have a Bitmap Object named: bmp1 - I already have the numerical values of:red,blue and green colors + I have the X and Y cordinates. How do I set the pixel value via safe code on...
12
by: JS | last post by:
I was writing some routines which could do bitwise boolean operations on byte arrays, and I ran into what I think is a bug with C#'s unsafe code. I am pasting a console application below. Can...
4
by: Jerome | last post by:
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. What should I do if I wanna create/delte/resize such...
4
by: Martin | last post by:
Hello, I haven't found any answer on this - if it is possible or not. I have a PCI card with a memory on it. I have also a driver (written in C++) that maps the memory from the card to user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...

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.