Have you ever noticed how some applications seems to be aware of how you pass them data? Take the su
program, for instance:
$ echo 'my password' | su
su: must be run from a terminal
Just another WordPress.com site
January 19, 2012
Have you ever noticed how some applications seems to be aware of how you pass them data? Take the su
program, for instance:
$ echo 'my password' | su
su: must be run from a terminal
January 18, 2012
November 24, 2011
Links, Programming languages Erlang Leave a comment
That guy seems to be pretty much aware of what’s Erlang:
http://www.javalimit.com/2011/05/erlang-is-not-a-concurrent-functional-programming-language.html
Worthy read.
November 18, 2011
Erlang/OTP has a nice pattern which strictly resambles Java’s Interfaces: behaviors. I’m using a behavior (“conf
“) inside my application for {enter technical details here}, so I stumbled into a little itchy issue during compile time:
The solution comes strightforward: compile src/conf.erl
before anything else. How to do that? Well, I surfed the Internet, searching for solutions. It turns out that, for instance, ejabberd is using Autotools instead of the standard Erlang compile system. Autotools are a really nice piece of crap software, but I don’t have time to discover how to use it with Erlang.
Then I just reconnected my brain…
Note: Place the
-pa
flag before
-make
, otherwise the make command will not consider the output directory in the path.
November 8, 2011
Zen coding python Leave a comment
For some applications you may have big sparse matrices, basicallly it’s filled of zeros everywhere, except for some points in which you setted some value, depending on your application logic. In this case a whole N×M matrix is a waste of memory (expecially for huge N and M) not to mention the fact you may want a matrix with more than two dimensions!
As you probably know, a good idea here would be using a mapping instead, say a Hash table, to store the elements you would put in the matrix with the coordinates as keys. In order to explain this concept to a friend of mine, I sketched an implementation of this principle in Python, and soon I realized it’s extremely easy!
June 25, 2011
I heard about Python‘s with statement, however I didn’t know about it. I suggest this link: it explains how that works in an effective and concise way.
June 1, 2011
Tutorials, Zen coding C++, disassemble, object code, Stack Overflow, static Leave a comment
The wise C programmer knows how souce code gets linked, so he or she is also aware of how symbols are managed into object code. In our module we typically want to export some symbols, like for instance a function or, if you really need to do it a global variable. Note also that types are not object symbols. More
June 1, 2011
Tutorials C++, Doxygen 1 Comment
Sometime you need to use the attributes for structs or functions. By example you may define:
typedef struct { uint8_t foo; uint16_t bar; } __attribute__((packed)) foo_t;
From the doxygen point of view this could be nasty, since it will start thinking __attribute__((packed)) is the name of the struct.
Doxygen provides a solution to this through a special command inside the Doxyfile:
# The PREDEFINED tag can be used to specify one or more macro names that # are defined before the preprocessor is started (similar to the -D option of # gcc). The argument of the tag is a list of macros of the form: name # or name=definition (no spaces). If the definition and the = are # omitted =1 is assumed. To prevent a macro definition from being # undefined via #undef or recursively expanded use the := operator # instead of the = operator. PREDEFINED =
So we can just put a PREDEFINED = DOXYGEN and modify our code as follows:
typedef struct { uint8_t foo; uint16_t bar; } #ifndef DOXYGEN __attribute__((packed)) #endif foo_t;
I learned this trick by reading Alsa ASoundLib’s source code.
May 23, 2011
Tutorials, Zen coding python 2 Comments
Sometimes it’s useful to define an associative array which maps a certain key to a list of elements. This is used basically everywhere you need to achieve some categorization. Just to give an example, suppose we want to group a bunch of people by name. Each person has an unique id, while there may be multiple guys with the same name.
May 6, 2011
Programming languages C++ Leave a comment
As you probably know, in C++ there’s a standard way of implementing a comparison semantics for two objects, and this is achieved by means of the operator overloading.
The following is a valid program:
#include struct Foo { int x; void test() const { std::cout << "test: " << x << std::endl; } bool operator== (const Foo &other) { test(); return x == other.x; } }; int main () { Foo f, g; f.x = 3; g.x = 4; std::cout << "Eq1: " << (f == g) << std::endl; std::cout << "Eq2: " << (g == f) << std::endl; return 0; }
The output of the program will be:
test: 3 Eq1: 1 test: 4 Eq2: 1
Now, did you ever try to print something with the std::cout stream? Provided we include the fstream header, we can write the following code:
std::ostream & operator<< (std::ostream &o, const Foo f) { return (o << "Foo instance with x=" << f.x); }
which allows us to write something like:
Foo f = {3}; std::cout << f << std::endl;
thus obtaining the following output:
Foo instance with x=3
This is good because, in principle, we cannot modify the fstream class adding a method for newly defined type Foo. So far I asked myself: can I do the same thing with arbitrary operators?
I removed the method bool Foo::operator== (const Foo &other) from the Foo struct, and added the following code:
bool operator== (const Foo &f0, const Foo &f1) { return f0.x == f1.x; }
…and this works! Nice!
The following question is: what if I define both the bool Foo::operator== (const Foo &other) method and the bool operator== (const Foo &f0, const Foo &f1) function?
It turns out the compiler likes best the class-defined method. This is pretty reasonable, since gives the developer a better control of the defined class, while externally defined behaviors get honoured only if no behavior is provided.