473,383 Members | 1,859 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,383 software developers and data experts.

Pointers?

How much is Pointers different in C# compared to C++?
Do we have destructors in C#? if yes, for what do we use it?

--
Cheers,
Akhil
Nov 16 '05 #1
7 1743
Hi Akhil,
How much is Pointers different in C# compared to C++?
Very different. Pointers do not exist in C#. Although you can still use pointers in "unsafe" code blocks.
Do we have destructors in C#? if yes, for what do we use it?


Basically no. You make sure there are no more references to an object and the garbage collector will release the memory shortly thereafter. You can still write a kind of destructor that will be called when the garbage collector is about to eat an object.
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
There are a few things to be considered about pointers in C#:
(1) Any code using pointers cannot be 'managed' by CLR. So you've to put it
in 'unsafe' block.
(2) Any object in a program's memory can be garbage collected or moved to a
different location in memory without any notice by the garbage collector of
..NET runtime. To fix a pointer and memory pointed to by the pointer in
memory, you've to use 'fixed' clause. Any pointer declared inside a 'fixed'
expression will be pinned in memory and will not be moved by the GC.
(3) Any value types declared within an 'unsafe' block are automatically
treated as 'fixed' by the runtime and so these cannot appear within a 'fixed'
clause.
(4) As a guideline for use of pointers, you'll have to use them only when
execution speed is utmost important, or you've to call some Windows API
function which requires a pointer as a parameter.

About Destructors in C#:
(1) In general you don't need to worry about freeing memory. However when
your application encapsulates unmanaged resources such as windows, files, and
network connections, you should use destructors to free those resources.
(2) Destructor is called by the GC AFTER an object is marked for
destruction, that is when it is no longer refernced or it is no longer in
scope.
(3) The destructor implicitly calls the Object.Finalize() method.
(4) Destructors cannot be invoked explicitly. They cannot be inherited and
overloaded.
(5) Structs do not have destructors
(6) In general it's better to implement Dispose() method of IDisposable
interface and put the clean up code there, and call it whenever the object is
to be destroyed, because there is no guarantee when the destructor will be
called.

Hope this helps.

"Akhil" wrote:
How much is Pointers different in C# compared to C++?
Do we have destructors in C#? if yes, for what do we use it?

--
Cheers,
Akhil

Nov 16 '05 #3
Thanks a lot Sameeksha......that led to lot of clarity.

--
Cheers,
Akhil
"Sameeksha" <Sa*******@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
There are a few things to be considered about pointers in C#:
(1) Any code using pointers cannot be 'managed' by CLR. So you've to put it in 'unsafe' block.
(2) Any object in a program's memory can be garbage collected or moved to a different location in memory without any notice by the garbage collector of .NET runtime. To fix a pointer and memory pointed to by the pointer in
memory, you've to use 'fixed' clause. Any pointer declared inside a 'fixed' expression will be pinned in memory and will not be moved by the GC.
(3) Any value types declared within an 'unsafe' block are automatically
treated as 'fixed' by the runtime and so these cannot appear within a 'fixed' clause.
(4) As a guideline for use of pointers, you'll have to use them only when
execution speed is utmost important, or you've to call some Windows API
function which requires a pointer as a parameter.

About Destructors in C#:
(1) In general you don't need to worry about freeing memory. However when
your application encapsulates unmanaged resources such as windows, files, and network connections, you should use destructors to free those resources.
(2) Destructor is called by the GC AFTER an object is marked for
destruction, that is when it is no longer refernced or it is no longer in
scope.
(3) The destructor implicitly calls the Object.Finalize() method.
(4) Destructors cannot be invoked explicitly. They cannot be inherited and
overloaded.
(5) Structs do not have destructors
(6) In general it's better to implement Dispose() method of IDisposable
interface and put the clean up code there, and call it whenever the object is to be destroyed, because there is no guarantee when the destructor will be
called.

Hope this helps.

"Akhil" wrote:
How much is Pointers different in C# compared to C++?
Do we have destructors in C#? if yes, for what do we use it?

--
Cheers,
Akhil

Nov 16 '05 #4
there are a lot of misinformation in this post that needs to be corrected.
see inline

"Sameeksha" wrote:
There are a few things to be considered about pointers in C#:
(1) Any code using pointers cannot be 'managed' by CLR. So you've to put it
in 'unsafe' block.
the wording of this seems somewhat misleading. to use pointers in a method
or a class, you need mark them unsafe, but it doesn't mean that CLR
completely ignores these blocks. in fact, the second point shows that they
are still very much affected by the CLR.
(2) Any object in a program's memory can be garbage collected or moved to a
different location in memory without any notice by the garbage collector of
.NET runtime. To fix a pointer and memory pointed to by the pointer in
memory, you've to use 'fixed' clause. Any pointer declared inside a 'fixed'
expression will be pinned in memory and will not be moved by the GC.
this only applies to managed heap.
(3) Any value types declared within an 'unsafe' block are automatically
treated as 'fixed' by the runtime and so these cannot appear within a 'fixed'
clause.
unsafe keyword doesn't intrinsically pin anything. the reason local
variables doesn't need to be fixed is because it's located on the stack, and
the stack is not subject to the GC.
(4) As a guideline for use of pointers, you'll have to use them only when
execution speed is utmost important, or you've to call some Windows API
function which requires a pointer as a parameter.

About Destructors in C#:
this is an area I feel that the doc is very misleading. there is no
destructor in .NET. C# uses the C destructor syntax to express
Object.Finalize(), but it's not really a destructor.
(1) In general you don't need to worry about freeing memory. However when
your application encapsulates unmanaged resources such as windows, files, and
network connections, you should use destructors to free those resources.
(2) Destructor is called by the GC AFTER an object is marked for
destruction, that is when it is no longer refernced or it is no longer in
scope.
(3) The destructor implicitly calls the Object.Finalize() method.
(4) Destructors cannot be invoked explicitly. They cannot be inherited and
overloaded.
(5) Structs do not have destructors
(6) In general it's better to implement Dispose() method of IDisposable
interface and put the clean up code there, and call it whenever the object is
to be destroyed, because there is no guarantee when the destructor will be
called.

actually, it's better to implement both. you should definitely implement
Dispose to free resource on demand. but you also need Finalize as a fail
safe in case Dispose wasn't called by the programmer. otherwise, you are
leaking resource.

the doc is being overly cautious when saying Finalizers are not guarantteed
to be called. in fact, Finalizers are guarantteed to be called as long as
your program doesn't terminate abnormally, meaning it didn't crash the
runtime, got into a deadlock or any other situation that forces you to kill
the process prematurely. In fact, I'd be very worried if it doesn't <em>at
least</em> guaranttee me that resources will be freed when I close my
application, that'd be a terrible platform.
Hope this helps.

"Akhil" wrote:
How much is Pointers different in C# compared to C++?
Do we have destructors in C#? if yes, for what do we use it?

--
Cheers,
Akhil

Nov 16 '05 #5
Daniel Jin is correct, there is a lot of misinformation. I'll try to clear up any that Daniel missed...

First of all, there are no destructors in C#. There are finalizers which are expressed using C++'s destructor syntax. After the Garbage Collector collects your dead reference
object (no more live references to it), if it implements a finalizer, it is put on the finalization queue and run at an undetermined time in the future by the GC's finalization thread.
actually, it's better to implement both. you should definitely implement
Dispose to free resource on demand. but you also need Finalize as a fail
safe in case Dispose wasn't called by the programmer. otherwise, you are
leaking resource.
Daniel is correct. Please see the Dispose pattern:
http://msdn.microsoft.com/library/de...izedispose.asp
Note it's also a good idea to suppress the finalizer upon successful completion of Dispose, to avoid double freeing resources, and to avoid putting your object ont he
finalization queue, which is expensive, since it is promoted a generation, as are any objects it references.
the doc is being overly cautious when saying Finalizers are not guarantteed
to be called. in fact, Finalizers are guarantteed to be called as long as
your program doesn't terminate abnormally, meaning it didn't crash the
runtime, got into a deadlock or any other situation that forces you to kill
the process prematurely. In fact, I'd be very worried if it doesn't <em>at
least</em> guaranttee me that resources will be freed when I close my
application, that'd be a terrible platform.
Actually, there are other cases when a finalizer may not be run:
-When the appdomain that contains the object is "rudely" unloaded
-There is a finalizer that is starving the finalizers by hogging the finalizer thread (which has a timeout)
-The finalizer thread times out because of too many finalizable objects on the queue.

Don't worry thoug, when the CLR shuts down, unmanaged resources will be released, but buffers may not be flushed, and final writes may be lost.

Hope that helps!
-Chris

there are a lot of misinformation in this post that needs to be corrected.
see inline

"Sameeksha" wrote:
There are a few things to be considered about pointers in C#:
(1) Any code using pointers cannot be 'managed' by CLR. So you've to put it
in 'unsafe' block.


the wording of this seems somewhat misleading. to use pointers in a method
or a class, you need mark them unsafe, but it doesn't mean that CLR
completely ignores these blocks. in fact, the second point shows that they
are still very much affected by the CLR.
(2) Any object in a program's memory can be garbage collected or moved to a
different location in memory without any notice by the garbage collector of
.NET runtime. To fix a pointer and memory pointed to by the pointer in
memory, you've to use 'fixed' clause. Any pointer declared inside a 'fixed'
expression will be pinned in memory and will not be moved by the GC.


this only applies to managed heap.
(3) Any value types declared within an 'unsafe' block are automatically
treated as 'fixed' by the runtime and so these cannot appear within a 'fixed'
clause.


unsafe keyword doesn't intrinsically pin anything. the reason local
variables doesn't need to be fixed is because it's located on the stack, and
the stack is not subject to the GC.
(4) As a guideline for use of pointers, you'll have to use them only when
execution speed is utmost important, or you've to call some Windows API
function which requires a pointer as a parameter.

About Destructors in C#:


this is an area I feel that the doc is very misleading. there is no
destructor in .NET. C# uses the C destructor syntax to express
Object.Finalize(), but it's not really a destructor.
(1) In general you don't need to worry about freeing memory. However when
your application encapsulates unmanaged resources such as windows, files, and
network connections, you should use destructors to free those resources.
(2) Destructor is called by the GC AFTER an object is marked for
destruction, that is when it is no longer refernced or it is no longer in
scope.
(3) The destructor implicitly calls the Object.Finalize() method.
(4) Destructors cannot be invoked explicitly. They cannot be inherited and
overloaded.
(5) Structs do not have destructors
(6) In general it's better to implement Dispose() method of IDisposable
interface and put the clean up code there, and call it whenever the object is
to be destroyed, because there is no guarantee when the destructor will be
called.


actually, it's better to implement both. you should definitely implement
Dispose to free resource on demand. but you also need Finalize as a fail
safe in case Dispose wasn't called by the programmer. otherwise, you are
leaking resource.

the doc is being overly cautious when saying Finalizers are not guarantteed
to be called. in fact, Finalizers are guarantteed to be called as long as
your program doesn't terminate abnormally, meaning it didn't crash the
runtime, got into a deadlock or any other situation that forces you to kill
the process prematurely. In fact, I'd be very worried if it doesn't <em>at
least</em> guaranttee me that resources will be freed when I close my
application, that'd be a terrible platform.
Hope this helps.

"Akhil" wrote:
> How much is Pointers different in C# compared to C++?
> Do we have destructors in C#? if yes, for what do we use it?
>
> --
> Cheers,
> Akhil
>
>
>

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Nov 16 '05 #6
>
Actually, there are other cases when a finalizer may not be run:
-When the appdomain that contains the object is "rudely" unloaded
-There is a finalizer that is starving the finalizers by hogging the finalizer thread (which has a timeout)
-The finalizer thread times out because of too many finalizable objects on the queue.
interesting. I wasn't aware of the last two points. I guess the doc was
right in the cautious approach. more reason not to rely on finalizers too
much. thanks for the information.

is there a reason why there's a timeout on the finalization thread? is it
to avoid a hanging finalizer, like one that is in a deadlock.

Don't worry thoug, when the CLR shuts down, unmanaged resources will be released, but buffers may not be flushed, and final writes may be lost.

good to know nothing is leaked permanently. I can sleep better now.
Hope that helps!
-Chris

Nov 16 '05 #7

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...

Actually, there are other cases when a finalizer may not be run:
-When the appdomain that contains the object is "rudely" unloaded
-There is a finalizer that is starving the finalizers by hogging the finalizer thread (which has a timeout) -The finalizer thread times out because of too many finalizable objects on the queue.

interesting. I wasn't aware of the last two points. I guess the doc was
right in the cautious approach. more reason not to rely on finalizers too
much. thanks for the information.

is there a reason why there's a timeout on the finalization thread? is it
to avoid a hanging finalizer, like one that is in a deadlock.


Exactly. Good example in the August msdnmag .NET Matters column.

Don't worry thoug, when the CLR shuts down, unmanaged resources will be released, but buffers may not be flushed, and final writes may be lost.


good to know nothing is leaked permanently. I can sleep better now.


Everything is still under control of the OS. This has been the case for
Win32 processes since NT3.1, and .NET apps pick it up as well for system
resources.

bob
Nov 16 '05 #8

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.