>> Single Point of Failure >>


Recent Entries



Popular Tags

Art Blog Blogging Book Books C++ C/C++ Calvin Cartoon Complaints Daughter Definition Development Event Experiment Family Favorites Firefox Focus Fun Google Humor Introspection Life Linux Movie Movies Music Organization Photo Photography Programming Python Reading Software Tips Tools Travel TV Ubuntu Vacations Video Water White Work

Snippets…

Ξ March 3rd, 2013 | → 0 Comments |
Programming |, , , , |


Ever had a programming argument at the water cooler, one that couldn’t be solved without some coding to check what’s what? Or did you ever reached some dark corner of your favorite language that needed to be quickly enlightened by a few lines of code?

Most of the times, solving those technical difficulties would require an “environment” with an editor, with the right compiler and a sandbox for my experiments.

That is no longer the case since I discovered the help I could get from the several available online compilers. My favorite is the one provided by Live Work Space, that provides support for C, C++, Python, Ruby, D, Go, Fortran, and more… with several compilers for most of the supported languages.

There are others that also provide interesting features, such as Ideone. And if you need a more elaborate setup, Compilr provides the full development environment.

[edit on 2013-09-30]

gcc.godbolt.org is another excellent alternative when the above mentioned systems are offline.

 


So little time for so many projects…

Ξ January 1st, 2013 | → 0 Comments |
Development |, , , , , |


I really love my Raspberry Pi. As expected, the first “projects” were simple and easy to setup: 

  1. the raspbmc-based media center (instructions here)
  2. the samba server for home backups (instructions here)
  3. setting up the wireless dong (no so easy as expected)

 

Now, the world awaits… 

  • To do some programming on the damn thing, I’ll have to setup a decent development/build environment. Considering the shortage of resources on the board, and how easy it is to copy an executable using the samba share, I’ll explore a cross-compilation approach.
  • There are also some home automation that I have in mind. I bet half of them are simply crazy, but maybe a few a them are actually feasible and practical.
  • And finally, how about building a small robot capable of cruising the living room on its own? That would be fantastic.

 

It’s really nice getting into the metal and all the rust, and it’s totally amazing how much fun one can get from a couple of flashing LEDs.

 


Aha! Excellent book…

Ξ May 5th, 2012 | → 0 Comments |
Books |, , , |


I’ve been reading some of the classics, including the amazing Jon Bentley’s Programming Pearls (2nd Edition). The book provides awesome insight to the mind of a programmer. Moreover, by providing an excellent list of problems the author incites the reader to experiment with the contents of the book.

Here’s my version of the anagram finder from Column 2: “Aha! Algorithms”.

#include <iostream>
#include <fstream>

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

typedef std::vector<std::string> anagram_vector_t;

typedef std::unordered_map<std::string, anagram_vector_t> anagram_map_t;


void
load_words_from_stream(anagram_vector_t& words, std::istream& input)
{
  std::string word;
  
  while(input >> word)
  {
    words.push_back(word);
  }
}

void
load_words_from_file(anagram_vector_t& words, const std::string& filename)
{
  std::ifstream input(filename);
  
  load_words_from_stream(words, input);
}

void
process_anagrams(const anagram_vector_t& words, anagram_map_t& anagrams)
{
  std::for_each(std::begin(words),
                std::end(words),
                [&anagrams](const std::string& word) {
    
    // get key by sorting the string value
    std::string key = word;
    std::sort(key.begin(), key.end());
  
    // store (key, value)
    anagrams[key].push_back(word);
  });
}

void
print_anagrams(const anagram_map_t& anagrams)
{
  for(auto i = std::begin(anagrams); i != std::end(anagrams); ++i)
  {
    const anagram_vector_t& words = i->second;
    
    if( words.size() > 1 )
    {
      std::for_each(std::begin(words),
                    std::end(words),
                    [&anagrams](const std::string& word) {
      
        std::cout << word << " ";
      });
      std::cout << std::endl;
    }
  }
}

void do_anagrams(const std::string& filename)
{
  anagram_map_t anagrams;
  anagram_vector_t words;
  
  load_words_from_file(words, filename);
  process_anagrams(words, anagrams);
  print_anagrams(anagrams);  
}

int main(int argc, char *argv[])
{
  if( argc != 2 )
  {
    std::cout << "Usage: " << argv[0] << " <words-file>" << std::endl;
    return -1;
  }
  
  do_anagrams(argv[1]);

  return 0;
}

 


Big Ball of Yarn

Ξ January 2nd, 2012 | → 0 Comments |
Work |, , , |


New year, new project, right? Well… that not totally true for me this year! Let’s say that I’ve inherited a Big Ball of Yarn to play with. The goal is to “reuse” an existing project, remove unnecessary functionality (a.k.a. dead code), and plug-in some extra “algorithms”, but just lot at the #include dependencies of the “reusable” source code. This will be fun!

(click to enlarge)

 


Just had a nice time with volatile!

Ξ December 17th, 2011 | → 0 Comments |
Programming |, , , |


I’ve never had the chance (or in fact the need) to investigate the use of the volatile keyword in C/C++. Today I found some very interesting articles about the subject.

 - Andrei Alexandrescu’s article “volatile: The Multithreaded Programmer’s Best Friend” at Dr. Doobs’s.
Explains that using volatile prevents compiler optimizations that might render code incorrect in the presence of certain asynchronous events. And presents a technical discussion on how to use the volatile qualifier to create critical sections and avoid race conditions.

 - Arch Robison’s “Volatile: Almost Useless for Multi-Threaded Programming” at Intel Software Blogs.
Provides an example where volatile does not work, and defends that volatile does not provide a sound base to create correct multi-threaded programs. The discussion that follows is very interesting, spawning comments for several years, and including several very interesting references to how the C++ communities discussed/evolved volatile understanding.

 - Nigel Jones’ “Introduction to the Volatile Keyword” (here) and San Saks’ “Use voatile Judiciosly” (here).
Show interesting examples of what is and how to use the
volatile qualifier, providing examples of use in the scope of embedded systems development.

         


        Once virtual, always virtual…

        Ξ October 23rd, 2011 | → 0 Comments |
        Programming |, , , |


        What will be the output of the code below?

        #include <iostream>
        #include <memory>
        
        using namespace std;
        
        namespace abyss {
        
        class A {
        public:
          virtual ~A() {}
          virtual void f() { cout << "A::f" << endl; } // base is virtual
        };
        
        class B: public A {
        public:
          virtual ~B() {}
          void f() { cout << "B::f" << endl; } // this is also virtual
        };
        
        class C: public B {
        public:
          virtual ~C() {}
          void f() { cout << "C::f" << endl; } // continues to be virtual
        };
        
        }
        
        int main() {
          auto_ptr<abyss::A> base(new abyss::C);
          base->f();
           // method called using pointer to base gets dispatched to C::F
        
          return 0;
        }
        

        Take notice of the “once virtual always virtual” rule that states that once a method is qualified as virtual there is no way to turn off the dispatching mechanism provided by the language for virtual functions. Unlike Java, C++ does not have the notion of final (which interrupts the dispatching). This means that, even though neither B or C classes qualify f() as virtual, the call to base->f() shall be dispatched as if it was.

        I like to follow the convention to place an explicit virtual qualifier to make clear the expected behaviour. It would be nice to have some kind of warning from the compiler, but no issue is raised (at least when using g++ 4.5.1).

         


        Best Reading

        Ξ April 8th, 2011 | → 0 Comments |
        Programming |, , , , |


        Today I finished one of the best C++ books that I’ve ever read. “Modern C++ Design” by Andrei Alexandrescu is amazing. The techniques presented are ground breaking, and I must admit the joy of learning about the described techniques was immense. This is certainly a must-read book for all C++ afficionados, like myself.

        Reading this book made me believe (I was sure of it from the start, even before starting) that there’s still much to learn about C++. This book is certainly not for the faint of heart. In order to keep up with the huge amount of new concepts, techniques and approach, I had to “build” a mind-map. Check it out to have just a glimpse of the books contents, as “seen” by me. 

         


        A Puzzle…

        Ξ November 6th, 2010 | → 0 Comments |
        Programming |, , , |


        The Exceptional C++ book series by Herb Sutter are amazing. The three books* provide (sometimes not so easy but) very interesting puzzles related to the possible uses of modern C++ programming. Its perfect to the passionate programmer, and you can be sure to learn and enjoy the time spent reading these books.
        Here is an excerpt from Item 4 of More Exceptional C++, which intends to find a mechanism to check, at compile time, that a given class D is derived from a base class B.

        template<typename D, typename B>
        class IsDerivedFrom
        {
          class No { };
          class Yes { No no[2]; };

          static Yes Test( B* ); // declared, but not defined
          static No Test( … ); // declared, but not defined

        public:
          enum{ Is = sizeof(Test(static_cast(0))) == sizeof(Yes) };
        };

        I have to admit that it took me a while to understand what the hell was going on in this piece of code. Can you see how this solved the issue? Very clever, right!?

         


        * Exceptional C++, More Exceptional C++ and Exceptional C++ Style

         


        int64_t vs long long

        Ξ February 21st, 2010 | → 0 Comments |
        Programming |, , , , |


        This last monday morning, a colleague enters in my office and asked for my… how should I put it… let’s say technical advise.

        This time the discussion was about using int64_t and double in some deprecated code involving type casts and trunc of doubles into integers.
        This discussing made me research a bit about C and C++ data types regarding integers, since somewhere along the way I listened to something like “I was told to use int64_t because long long is architecture dependent”. It struck deep.

        Let’s start with some simple clarifications:

        •  int, or signed int, type must contain at least 16 bits (since the standard mandates that in must support values ranging from -32,767 to +32,767)
        •  long, or long int or signed long int, type must contain at least 32 bits (since the standard mandates that in must support values ranging from -2,147,483,647 to 2,147,483,647)
        •  long long, or long long int or signed long long int, type must contain at least 64 bits (since the standard mandates that in must support values ranging from -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807). Note that this data type was first defined by C99 standard.

        © kainet

        Regarding int64_t, as it seems, C99 standard defines a header files called stdint.h as part of the C standard library, that contains portable definitions for integer types. Portable in the sense of explicitly stating the number of bits used to represent a given integer. The definitions are in the form of intN_t and uintN_t, for N bit integers and unsigned integers, respectively. This way the programmer can ensure that, whatever the wordsize used by the processor, the right range of values can be represented. The standard garantees that the widths for these types must be ≥ N.

        However well supported by modern compilers, take notice that stdint.h is not officially in the latest C++ standard (know by C++03).

        Looking again at the statement that aroused my curiosity, what is the relation between int64_t and long long?
        Well, they represent the same thing in most architectures! Both int64_t and long long first got defined by C99 standard, but int64_t definition ensures that 64 bits are used for represent the integer value while with long long at least 64 bits must be used.

        For example, some architectures represent long and int with the same number of bits (32 bits). The standard only requires at least N bits. :-)

         


        To i++ or to ++i…

        Ξ April 25th, 2009 | → 0 Comments |
        Programming |, , , |


        The question is very simple. When writing a for loop, such as the following one

        for( int i = 0; i < 10, /* increment i */ )

        Between i++ or ++i, which increment operator would you use? Why?

        If you choose the answer “It’s the same! Both increment i.“, or if your answer was “I have always used i++, because /* some place ulterior reason here, like ‘I’m programming in c++ and not in ++c’ */“, then your might thing improving your c++ knowledge a little bit. :-)

        I just learned it myself, so I’ll share. In fact, it’s so simple it makes me wonder how many of these small details are missed every time my C++ source code lines are incremented. The best increment operator to be used in the pre-increment.

        Why? Because it avoids the creation of a temporary variable. Post-increment (i++) has to create a temporary variable to store the return result (that you won’t be using), while incrementing this as expected. Pre-increment simply increments the value of this and returns.

        So, systematically using ++i, instead of i++, should improve your runtime performance – even if so slightly.

         


        On the nightstand...


        «« Previous Entries