Variadic C/C++

Ξ September 27th, 2009 | → 0 Comments |
Programming |, , |

Did you ever wanted to create your own printf and never knew how to make your functions or methods take a variable number or arguments? Then this test if for you…

If you ever took the chance to read the manual page for printf functions, I may noticed the following definition for the this C standard variadic function (yes, thats’s how functions/methods that a variable number of arguments):

int printf(const char *format, …);

All the magic is done by that ’strange’ three points. Let’s see how one can one them. Imagine you what to implement a logging interface using the following variadic function (note: level_t is an enumerate of the available log levels):

void log(level_t level, const char *format, …);

This allows you to add log entries like:

log(FATAL, “Exiting to unknown problem.”);
log(ERROR, “Unable to open file %s.”, filename);
log(WARNING, “Falling to default configuration.”);
log(INFORMATION, “Computation at step %d : %s.”, step, description);
log(DEBUG, “Running iteration %d.”, i);


After understanding that the ’strange’ three points represent a va_list which contains the list of arguments. The way to access the contents of the three points is as follow:

void log(level_t level, const char *format, …)
{
  va_list args                // 1. create the list of pointers to unnamed arguments
  va_start(args, format);     // 2. initialize the list of pointers to arguments
                              //    note: the second argument must be the last of fixed arguments
 
printf( “Level %d”, level );// 3. use printf to display the required information
  while( !va_end(args) )      // 4. iterate over available arguments, until the end of the list
  {
    const char *str

       = va_arg( args, char * ); // 5. access the argument value
                                              //    note: it is necessary to provide the type of the argument

    printf( “%s\n”, str );       // 6. display the selected argument
  }

  va_end( args )              // 7. cleanup the argument list
}

This is one way to do it, but you can implement the same function based of a printf sibling, called vprintf which allows you to pass the va_list directly to printf. This also solves the problem with the cast required at steo 5. of the previous example. So, this is the resulting implementation:

void log(level_t level, const char *format, …)
{
  va_list args;
  va_start(args, format);
 
printf( “Level %d”, level );
  vprintf( format, args );    // notice the use of vprintf
  va_end( args );
}

There is also the possibility to provide the developer with variadic MACROS. See here for more details.

 

Home Cinema Afternoon #1

Ξ September 26th, 2009 | → 0 Comments |
Life |, , , |


After reading the book, the movie had to be the seen. Thought the first was excellent, and was worried about the latter, but there were no disappointment there. Both have to be considered sci-fi master pieces.

 

Peculiar and Extraordinary

Ξ September 20th, 2009 | → 0 Comments |
Photo |, , , |

One of these nights, I was doing the usual zapping and by chance I stopped at Fashion TV to watch something that seemed to be somehow out of place for this channel. After a minute, it became clear that it was the making of for the Pirelli Calendar for 2009. The pictures are awesome, but the photographic session progressed in a very bizarre manner.

Part 2 & Part 3


As any amateur photographer, I would love to participate in something like this but I don’t believe I’ll ever be that lucky. Again, although I still don’t understand why do the models submit to such a treat, I must admit that the results are extraordinary.

 

Interesting…

Ξ September 20th, 2009 | → 0 Comments |
Fun |, , , |

Just found this video somewhere on the Web and tough it was interesting. It looks to be a pretty interesting book trailer for Leviathan. Never had the change to read something from the work of Scott Westerfeld… this really seems to be a good change to start!

 

High Expectations

Ξ September 1st, 2009 | → 0 Comments |
Expectations, Life |, , , , |

Unlike some people, I prefer reading a book than watching a movie. This is specially true when I know that a book has already been transformed into a movie.
The problem is that after reading a good book my expectations are so high that I get discouraged to watch the movie. I think it’s the fear of getting disappointed with the movie or something like that. There are at least three books/movies that belong to this category. I simply loved the books, but I’ll have to convince myself to watch “Perfume The Story of a Murderer” and “Blindness“. The third movie I also have high expectation is the upcoming “Alice In Wonderland” (from Tim Burton). “Alice” is the current bedside book…

       

 

Everything is about to end…

Ξ September 1st, 2009 | → 0 Comments |
Life |, , , |

Today, this was the reply I got when accessing my GMail account… Oh, no! The world must be at an end! Save yourselves…




Ups! I think I exaggerated a bit. Everything is OK again… All my stuff is there. Maybe it’s time to think about making a few backups. :-)



Mental Note: next time press F5 (at least once) before crying “Wolf”!!!

 

On the nightstand...


    The Art of Agile Development


    Beautiful Architecture


    Modern C++ Design


    Large Scale C++ Software Design

Personal

Friends


Interesting


Shared Readings