Linux, programming, computers and life

March 17, 2009

volatile methods in C++

Filed under: programming, c++

Wow, a feature of C++ i did not know, ‘volatile’ methods. Well, i’d say extremely rare one, but still.

July 31, 2008

memset64, anyone?

Filed under: programming, c++

I wonder why memset is the only mean we have in libc in order to fill memory. We are limited to filling the memory with certain char. What if i want to use longer pattern? Is there anything like memset64? Generally, i’d like to fill the buffer with pattern which is 4 or 8 bytes long. I’m pretty sure, that’s the way memset is implemented anyway, for performance.
Maybe i’m missing something, but i was unable to find anything like this in the libc.

August 12, 2007

boost writers are geniuses

Filed under: programming, c++

I’m currently rewriting some python code in c++, due to performance problems. I needed a simple parser and i found spirit which is part of boost. I must say wow WOW. I must learn boost. It’s simply great. While I’m pretty sure it will not be possible to use it at work due to conservative nature (with respect to code and tools) of the management here, it’ll surely give me some ideas :) .

Technorati Tags: , , ,

April 16, 2007

redundant includes in c/c++

Filed under: programming, c++

From time to time, when i remove redundant #include statements from our project, i wonder - is there an automatic tool to do this?
As a first step it’d be nice to have a tool which removes really redundant #includes - those which will not interfere with project build. As a second step - it’d be really nice to have suggestions to use forward declarations where possible instead of #includes.
For a small project the problem can be solved manually, but for a large one, with several build targets… well - automation is a must.

Any suggestions on such a tool?

Technorati Tags: , ,

February 11, 2007

do not overuse memset

Filed under: programming, c++

Just a reminder. C++ is not C. memset is a useful function (you know, take-memory-location-and-length-and-fill-it-with-char). But there’s no reason whatsoever to use it in order to reset an object. Define a Reset() method (or choose your own name) and use it.
I’m not just saying it. It’s coming from a frustrated person after looking for a memory bug for day and a half. The bug was memset which reset some variable which was used in other memset and reset a lot of memory this way.
Two lessons learned:

  1. memset is for buffers, not for objects
  2. electric fence is really helpful

Technorati Tags: , , , ,

January 10, 2007

NEVER again in C++ (reminder for myself)

Filed under: programming, c++

It’s something i have to remember from now on.

  1. Never, and i repeat never make data member public, doesn’t matter how small and insignificant the class is.
  2. Even if you *know* that the class will never change and you are too lazy to write and use mutators and accessors - look item 1

Why? Just wait till you have to change that code.

Technorati Tags: , ,

November 8, 2006

Things i miss in python

Filed under: programming, c++, python

Coming from a C++ programmer:

  • I’m not the first to mention it, i know - missing of begin/end of block. If i copy-paste some code i need to think again how to align it - o/w it’ll not work! And there’s no automatic tool to align the code for me
  • Compile time checks:
  • const checks
  • non-declared variables check. if i write obj.xxx = 12 and the real member variable name is Xxx it takes ages to find the problem :(

Technorati Tags: , ,

November 1, 2006

Memoization in python

Filed under: programming, c++, python

I’m writing a genetic algorithm, which is pretty time consuming. I was thinking - why not use memoization and save some time. It appears pretty easy to do and there’re several examples for copy-paste in the internet, but it appeared that’s it not as simple as i thought it would be.
I’m trying to memoize (i’ll use cache from now) objects of a certain class. The class has some double precision data members, which i want to allow to be a little different, for example abs(self.a - rhs.a) < 0.1. It’s easy, just define __eq__ method. Now, let’s put objects into a dictionary (for caching)… oops - there’s an exception thrown - they are unhashable now.
Fast search in google revealed i need to define __hash__ method which must return equal results for equal objects…. Now it becomes non trivial at all.
In c++ it’s easy since std::map (stl equivalent of python’s dictonary) is based on operator< (or on comparison functor with the same functionality), and in python dictionary is based on hash value, which is faster but not easy calculable in my problem.
Is there something like std::map ready? Currently the solution was to round floating points members and cache them. Any better solutions?

Technorati Tags: Technorati Tags , , ,

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers