Managed C# and Unsafe Code

G

Guest

My native language being C++, I've got a few questions that a couple of hours
of searching on msdn didn't answer.

First, when using unsafe code and pointers, what is the C# equivalent to the
C++ "delete" command? I would like to keep my memoryspace clean, but I've
been unable to find a way to do it thus far.

Second, I'm using structs to store a specific set of data. Is there a C#
equivalent to the C++ STL "Vector" (or any of the STLs)? I read about sorted
lists, queues, and the like, though they seem to be only strings. Is there a
way to modify the type?

Third, and most important, I've got a bit of code that uses a large number
of pointers. There are six classes, five derived classes from one base. The
base is the only one with pointers ("Base_ *next", for the obvious linked
list structure). The "base" class for the other functions is an "unsafe
public class", but when iI try to call a pointer to this class in a function
within another class (alsoe "unsafe public class") it gives me an error,
"error CS0208: Cannot take the address or size of a variable of a managed
type ('NPCGen.Base_')" I'm at a loss, expecting it to be unmanaged as I
specified "unsafe". How do I set functions to be unmanaged?
 
N

Niki Estner

CodeTyro said:
My native language being C++, I've got a few questions that a couple of
hours
of searching on msdn didn't answer.

Is it? Mine's German.
First, when using unsafe code and pointers, what is the C# equivalent to
the
C++ "delete" command? I would like to keep my memoryspace clean, but
I've
been unable to find a way to do it thus far.

There is none. Allocating memory using the "new" keyword allocates managed
memory - this is freed by the garbage collector when there are no more live
references. You can also allocate memory using "stackalloc" (in unsafe
code), which allocates memory on the stack; The stack will be cleaned up on
function exit.
Second, I'm using structs to store a specific set of data. Is there a C#
equivalent to the C++ STL "Vector" (or any of the STLs)? I read about
sorted
lists, queues, and the like, though they seem to be only strings. Is
there a
way to modify the type?

Currently, the closest thing is the ArrayList class, but it can only store
objects. Anything can be safely casted to an object and back, so you can
store any kind of data in it. But be aware of boxing if you store value
types like structs in it.

..NET generics (similar to C++'s templates) will be introduced in the next
version of the framework.
Third, and most important, I've got a bit of code that uses a large number
of pointers. There are six classes, five derived classes from one base.
The
base is the only one with pointers ("Base_ *next", for the obvious linked
list structure). The "base" class for the other functions is an "unsafe
public class", but when iI try to call a pointer to this class in a
function
within another class (alsoe "unsafe public class") it gives me an error,
"error CS0208: Cannot take the address or size of a variable of a managed
type ('NPCGen.Base_')" I'm at a loss, expecting it to be unmanaged as I
specified "unsafe". How do I set functions to be unmanaged?

Why do you need pointers at all? Why don't you use references? i.e.
something like this: (untested)

class Base
{
private Base next = null;
....
}

Niki
 
G

Guest

Several reasons that i want to use a linked list is that the classes (were
structs, changed 'em to classes) are going to get very large, very quickly.
I need them to be dynamically allocated and I'd like to have pointers that
can referrence the classes and have many linked lists that aren't individual
units but simply a pointer to the next element and a link to the current
element data.

Because I'm going to be manipulating the data in the structs often and with
many functions (methods, whatever), I am hoping to be able to manipulate the
structs directly rather than have to deal with the copying of structs that
occurs with boxing (assuming my head is screwed on straight and my
understanding of boxing is sound - two possible but unlikely events).

One last reason I want to use pointers is that I will be reordering the
lists regularly and not in very conventional manners. I figure if I can iron
out pointer use in C#, I will be able to keep away from the tedious nature of
MFC.

Any suggestions that would allow me to use the current C# framework rather
than the C++ hack version I'm trying to throw together would be great. Using
Murach's C#, google, and MSDN unfortunately gain me less than I need, though
far more than I had before.

Thanks for your help, any more would also be greatly appreciated.
 
N

Niki Estner

CodeTyro said:
Several reasons that i want to use a linked list is that the classes (were
structs, changed 'em to classes) are going to get very large, very
quickly.
I need them to be dynamically allocated and I'd like to have pointers that
can referrence the classes and have many linked lists that aren't
individual
units but simply a pointer to the next element and a link to the current
element data.

You don't need pointers to build a linked list. Well, let's say C#
terminology is a little different from what you as a C++ programmer might be
used. C#'s references to classes have almost the same semantics as C++
pointers. There are a few things that can't be done with C# references,
especially pointer arithmetic (you can't increase/decrease references) and
unchecked casts (like casting to void*, or casting an int* to a char*). If
you don't need these, C# references will do fine.
Just keep in mind .NET classes will always be allocated on the heap.
Because I'm going to be manipulating the data in the structs often and
with
many functions (methods, whatever), I am hoping to be able to manipulate
the
structs directly rather than have to deal with the copying of structs that
occurs with boxing (assuming my head is screwed on straight and my
understanding of boxing is sound - two possible but unlikely events).

Yes, preventig boxing sounds like a good idea then.
One last reason I want to use pointers is that I will be reordering the
lists regularly and not in very conventional manners. I figure if I can
iron
out pointer use in C#, I will be able to keep away from the tedious nature
of
MFC.

Any suggestions that would allow me to use the current C# framework rather
than the C++ hack version I'm trying to throw together would be great.
Using
Murach's C#, google, and MSDN unfortunately gain me less than I need,
though
far more than I had before.

Why did you look for? Searching for "C# linked list" yielded several
results. I had a quick look at this one:
http://csharpcomputing.com/Tutorials/Lesson9.htm
Looks like all you need.

Niki
 
G

Guest

Nope, in an attack of extreme stupity, i never thought to run a search for a
linked list within the bounds of c#, which only makes sense (20/20
hindsight) since everything is essentially a pointer and i must allocate a
new object for every class.

You are my hero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top