Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, November 24, 2015

The dangerous pointer to vector

This is actually a really obvious "issue" but I found that some developers (usually the inexperienced ones) simply don't think about this horrible source of bugs:). Now let's see the problem: If you run the code snipped above you will see an output similar to:

The value of our int is: 1
The value of our int is: -572662307
The explanation for this is simple. If you think that is a great idea to hold pointers to vector elements, you are wrong. The vector is basically a resizable array and the actual resize operation is:
  • Create a new and bigger array.
  • Copy all content from the current array to the newly allocated array.
  • Delete the current array and replace it with the newly created array.
See the problem? When the resize occurs, the underlying array is deleted and your pointer is invalidated. So be careful and don't use pointer to vectors.

Sunday, November 15, 2015

Programming Tips: Don't be clever

Everybody knows that debugging is a lot harder than writing code and every code needs to be debugged sooner or later. If not debugged you will certainly need to read it, maintain it, update it or optimize it. Let me ask you a question: If you are as clever as you can when you write the code, how you'll be able to debug it? Debugging is a lot harder than writing code! You will need to be a lot smarter than you were when you wrote the code. My advice is: don't be clever! Write code in such way that anybody can read it, write the most simple code you can and even so, the debugging will give you some headaches. Don't try to be a smartass. Write your algorithm as clear and simple as possible. Coding is not a contest, nobody will think you are limited because you don't write some obfuscated condition. Be clear and concise, your code should be readable by your grandmother, don't make simple things complicated, don't be clever!

Friday, October 16, 2015

C++ IDE vs Text Editors ?

As a VIM enthusiast I think that you can do a great job using a plain text editor + a terminal. Both Emacs and VIM are great tools with IDE like features, great shortcuts, nice plugins...

Sunday, May 3, 2015

Does AdMob work on Windows Phone 8.1 ?

Short answer yes. If you are a developer outside the US and you want to write software for Windows Phone 8.1 you'll probably be interested in adding some ads to your application. One of the best mobile ad provider is AdMob from Google. Good news is that AdMob is available on both Windows Phone 8 and 8.1 Silverlight but NOT in the universal applications. Also note that in Windows Phone 8.1 Silverlight the ads will work if you run your application in the emulator but will not work on your own development phone. This is normal, no need to panic, just test it on another phone and you will see it works.