Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Friday, November 6, 2009

C# Marshalling is no fun

I've been spending the past week working on Mac which is very close to FreeBSD, and I have been working on the simple goal of getting a MAC address for a network interface. I was able to find plenty of sample code on the net on how to do it, but they were all written in C. My gaol then became to do the same thing in C# using P/Invoke and Marshalling. I've been working on this for three days and I have learned two things. One, P/Invoke and Marshalling is extremely hard and confusing because you lose code reuse, meaning that every structure has to be redefined, along with macros and function signatures. System level code uses tons of nested structures, and defined variables and so on. So implementating just one P/Invoke function which takes a structure can get messy really quickly. I guess back then developers assumed everyone would be using header files which would automatically resolve all of these special structures macros and so on.

The second thing that I learned is that trying to work with pointers in C# is (dangerous) and extremely annoying. C allows developers to do many hackish things with pointers in order to cast, or just to different places in memory and so on. C# has various limitations on how to use pointers; so there is no direct mapping from C pointer handling to C# pointer handling. With that said, once again, converting system level C code to C# requires good understanding and experience with C pointers, and C# marshalling in order to get things done. Overall, I'm learning a lot, and spending a lot of time learning about different marshalling options but I did not expect it to be this hard. Also, marshalling tends to get confusing because they are so many different ways to represent data, you have IntPtr, char arrays, byte arrays, keywords such as in, out, ref, and functions such as Marshal.PtrToStructure, Marshal.Copy, Marshal.ReadByte, and so much stuff.

In the end, my advice is, do not underestimate marshalling (or anything else in programming), and pointers are extremely annoying to deal with. So try to avoid marshalling and pointers as much as possible

Sunday, October 25, 2009

The perils of using Managed Languages for young programmers

Although I'm not really a young programmer per say (since I've been programming since 2002), I still feel young due to my inexperience with programming. The majority of my programming has been with managed languages such as Java and C#. Now I love using managed languages because they make programming so much easier since you don't have to deal with memory; that's the whole point of managed languages is to manage memory. I have always been curious about unmanaged languages such as C/C++, but I never got the opportunity to work with them until now. I'm currently looking into using a network modeler written in C++ and I am starting to understand how complicated memory management could be. The main thing that I learned is that local objects to a function are allocated on the stack instead of the heap. Since both Java and C# uses the "new" keyword for declaring objects, I was always under the impression that the heap was always used for all objects, and only generic types such as int, char, and so on were stored on the stack. It turns out that in C++, objects can also be declared on the stack and that using the stack is much faster than the heap. C++ provides two ways of declaring an object, with the new keyword that requires using the "delete" keyword to reclaim the memory, or without it and it gets auto-managed. Nobody ever told me there was auto-management in C++. I also learned about auto_ptr and so on. Anyways, my point is that just using managed languages is great and I would use them for my application development projects, but being oblivious to C/C++ can be a great disservice. It's sad that noone emphasizes learning C/C++ and everyone is depending on Java/C# to do the dirty work.