Surface pro 3 power cord, 2 pin and 3 pin plug

Problem:

  • Have to plug the surface pro 3 into different wall plug standard (say, 2 pin and 3 pin type).
  • Do not want to use the adapter as it is bulky.
Solution: Buy a power cord of MacBook charger. It fits perfectly.

I got my surface pro 3 with a three-pin plug in the power cord, as I live in Singapore. Unfortunately, I have to move to other country, and this country uses different type of socket. I hate using an adapter, because it make the plug, original bulky enough, more bulky. 


I try to buy a new cord for the charger, but as surface pro 3 is new, it is hard to find one. The only option is to buy a new charger from Microsoft. Luckily, I saw the charger of a Macbook and try it. It fits to the surface pro 3 charger. The best thing is that this cord can be found easily, from ebay or electronic shop.


The only problem is the color, Mac love white color while Mic love black :).

std:: shared pointer and container like std::vector

How to use shared pointer with container:
  • Use it as normal, except
  • When insert or push it to a container, using:
    • push_back(std::move(p))

Shared pointer is a feature that is included in C++ 11. Using shared pointer is great, but it has some problems with standard container like vector, map or queue in std.

The problem is that when we push a shared pointer to a container, the references counter of the shared pointer does increase, but when the container is out of scope, the counter does not decrease. It make the memory leak, as the shared pointer can not be released.

The solution is to use: std::move

Be note that the shared pointer that is moved will be invalid. You need to get it back or insert it to container at the end of function.

Assertion, exception and error handling

How to use them in your code:


Debug break point

Exception

  • Assertions are for debugging. The user of your shipped code should never see them. If an assertion is hit, your code needs to be fixed.
  • Exceptions are for exceptional circumstances. If one is encountered, the user won't be able to do what she wants, but may be able to resume somewhere else.
  • Error handling is for normal program flow. For instance, if you prompt the user for a number and get something unparsable, that's normal, because user input is not under your control and you must always handle all possible situations as a matter of course. (E.g. loop until you have a valid input, saying "Sorry, try again" in between.)