tags:

views:

480

answers:

12

~80% of the code I write is in C#. The other ~20% is in C++. Whenever I have to switch from C# to C++, it takes me quite a while to mentally "shift gears" to thinking in C++. I make simple mistakes using pointers and memory allocation that I would not have made when I was in university. After the adjustment period, I am fine and writing in native code comes naturally.

Is this normal? Does anyone else experience something similar and if so, what do you do to cut down on the time this wastes?

Edit: I'm not saying that I cannot work with memory allocation and pointers. I comfortably use them often in my C++ code. Just not immediately after working in C# for long periods of time.

+1  A: 

Take your time and double check your work. Once you get back into the groove you'll speed up in no time.

ChaosPandion
Being careful is important. I'd add that having someone else review your code might be particularly helpful.
Steven Sudit
+2  A: 

Yeah, it's too bad so many colleges and universities fail to instruct students in resource management.

RAII will help you if you use it consistently.

Noah Roberts
7 votes??? People should really read questions a bit more carefully.
Ash
+22  A: 

I have the same problem. I use completely different color schemes for Visual Studio (dark-on-light for C++; light-on-dark for C# and VB).

Seems to help my brain ease the switch.

BlueRaja - Danny Pflughoeft
+1 for a nice "physical" implementation
ahsteele
Lots of good answers here, but I chose this as my answer because it's simple and surprisingly effective.
Glenn Sandoval
Great tip, i will try it too !
Cesar
+6  A: 

Have a checklist of frequent errors that you do when switching from one to the other, and refer to it each time. Simple yet effective.

JRL
A: 

I've experienced this myself. Its not just C# to C++ or managed to unmanaged. It happens at every change. I had this problem when moving from VB to VC++, CC++ to C#, C# to VC++, and even c# to VB.net(true story - courtesy ';').

Its only the time taken for adjustment that differs...

Srikanth Venugopalan
+1  A: 

I feel your pain; when I go c# -> c++ I code like I'm drunk.

I usually start the switch by reading a few pages of code before I dive in and start.

Pierreten
A: 

This is one of the reasons I do far more work in VB.NET than in C#. I don't forget what language I'm in. If you have that option I recommend it. There really isn't anything you can do in C# that you can't do in VB.NET and the radically different syntax helps me context switch.

Kate Gregory
A: 

I have to go from coding Oracle-based SQL to Sybase-based SQL all the time.

What I find that helps is to have a same procedure sitting on my secondary monitor, with all the good stuff in it, simple cursor, proc setup, if-then statements.. all valid code in that language, so if I forget which environment has the ELSIF or ELSE IF variant, I can check my template source to find out.

glasnt
+4  A: 

I do this all the time. The c++ projects I work on have several things in them to ease the memory / resource stuff

a) use RAII wrappers

b) systematically use boost shared_ptr

These 2 things allow for fire and forget programming on dynamically allocated things (we have well defined idioms that everybody knows)

Plus we have a well debugged library for doing locks, threads, etc

Helps a lot

And of course every now and then I complain about

  • C++ not having try finally
  • c# not having deterministic destructors

but - hey - thats life

pm100
I know it's proprietary, but MS C++ does support _try/_finally (http://msdn.microsoft.com/en-us/library/9xtt5hxz(VS.80).aspx). as for destructors, I'm sure you already know about IDisposable and `using`.
Steven Sudit
using only works for locally scoped things tho. C++ destructors work for things scoped at any level. Plus the user has to remember to do it. With desctuctirs the class designe forces it to happen (and he is the one that knows its needed)
pm100
+7  A: 

I make simple mistakes using pointers and memory allocation that I would not have made when I was in university.

Well, there's your problem. Learn C++ well enough to see how you can avoid messing around with pointers and memory allocations in the first place.

Then switching between managed and unmanaged code becomes trivial.

It is only a problem for you because you're switching between C#, where you don't need to worry about anything whatsoever, and C++, which you don't know well enough to use safely.

Memory management in C++ is not difficult. If you're getting it wrong, it's because you haven't learned it properly. In well-designed C++ code, it simply is not a concern. It happens automatically, just like it does in C#.

jalf
Read further. The problem is not that I do not know how to use these things. The whole point of switching to C++ is so that I can use memory allocation because there are situations where this is useful.
Glenn Sandoval
"just like it does in C#" - presumably you would say that any C++ program in which it is necessary to break a reference loop with weak pointers is by definition "not well-designed"?
Steve Jessop
@Glenn: No, I stand by what I said. My point is that when you use the correct techniques to *manage* memory allocations (which obviously have to happen), then it becomes trivial enough that the transition to and from C++ is almost trivial.When you try to painfully manage everything by keeping track of pointers with no clear ownership semantics and without using RAII classes, you're making things very difficult for yourself, and then, yes, the leap from C# is much greater.
jalf
@Steve: Sort of. Of course what I said was a simplification, and I know you can list any number of exceptions and contradictions. :) But often, a well-designed C++ app can avoid *having* the reference loop in the first place. Often, you can minimize the reliance on shared ownership, which means these "ownership loops" (which are the real problem, not simple reference loops) will not occur (as often)
jalf
A typical case would be something like a modifiable DOM implementation. In C# or any other mark-sweep GC language you would keep whatever references you need parent <-> child, and just detach chunks willy-nilly. In C++ however you handle it, you have to think about it and hence (I claim) it's not "just like C#" ;-). For 95%+ of data models I agree, though, you don't need to "switch resource-handling modes" between C++ and a GC language, so much as "switch resource-handling modes" between writing RAII classes and everything else. Most of your C++ coding is part of "everything else"...
Steve Jessop
... in practice it's syntax which messes me up as I switch languages, more than practical issues like resource management. For example at the moment I'm deeply paranoid about the way that SQL has me writing `if x = 0`. Back when I was switching constantly between C and a kind of heavily-macroised assembler, it took an effort of will in C to write more than one function call in a single line of C, since in that assembler a function call (or anything with a side-effect) required a full statement to itself.
Steve Jessop
A: 

Whenever I switch from C++ to Java, I always still think of where the memory is being allocated. I find it easiest just to not switch to thinking of the memory as managed...think about where it's going, and think about where you would be releasing it if you were writing it in a language without managed memory. And just don't write the lines of code you otherwise would.

foxwoods
A: 

It's normal to have problems switching from one language to another. C# seems close enough to C++, but in reality it isn't.

But bear in mind that in "modern" C++ most of the time you won't use memory management. It will be hidden from you, so normally I don't deal with pointers much ( at worst with smart pointers). But I know how they work. And this you should also, maybe you need a bit of practice with, as another participant said, the C language to have it right.

But in C++ there are also things like the templates, functors, algorithms, streams, that needs a bit of thinking because it's not java-C# like you may used to. And of course the use of the Standard Template Library and eventually boost.

But well, the "good" C++ developer should know why he doesn't use pointers :)

Nikko
I would be interested in hearing more on why pointers should not be used.
Glenn Sandoval
I suspect Nikko might have meant raw pointers, as opposed to smart pointers. The latter use RAII to avoid leaks.
Steven Sudit
Yes, and a warning against the overuse of pointers when they are not needed (of course, in some cases, they are needed)
Nikko