Archive for June, 2009

Using Variadic Functions in C

I don’t really need to write on how to use va_list, va_start, va_end, va_arg because other tutorials or references do a good job of explaining it already… however, here are some notes for wrapping Varadic functions.

Firstly, note the difference between:

void myFc( int arg1, ... )

and the va_list version:

void vmyFc( int arg1, va_list args)

gnu stdc libraries (printf, etc) wrap the va_list versions (vprintf, etc) with Variadic versions (printf, etc) with the following pattern:

int vfunc(int arg1, va_list vargs){
   // do real work
}
int func(int arg1, ...){
   int retval;
   va_list vargs;
   va_start(vargs, arg1);
   retval = vfunc(arg1, vargs); 
   va_end(vargs);
   return retval;
}

When wrapping va_list functions, it is important to consider that va_list is consumed and so in the case where you will be using your va_list for multiple functions, you’ll need to save the original pointer.

GNU C doc for stdarg.h — Note the __va_copy macro.

For example, wrapping the snprintf function:

//to find the length of the string, you pass null and length 0 to the function:
len = vsnprintf(NULL, 0, fmt, vargs);

//do the allocation
str = (char*) malloc(len+1);

//and finally read the string:
vsnprintf(str, len+1, fmt, vargs);

note that we use the va_list version of snprintf (vsnprintf).

the 2nd snprintf may (depending on stdarg implementation) cause a segmentation fault. The correct/safe way of doing it would be to copy vargs and use the copy in each snprintf operation:

#ifdef __va_copy
   __va_copy(save,vargs);
#else
   save = vargs;
#endif
len = vsnpritnf(NULL, 0, fmt, save);

str = (char*) malloc(len+1);

#ifdef __va_copy
   __va_copy(save,vargs);
#else
   save = vargs;
#endif
vsnpritnf(str, len+1, fmt, save);

But as my awesome co-worker Geoff asserts, its always better to keep it simple and perhaps there’s a way to accomplish what you’re trying to do without Variadic functions. Check out my other post.

PST~ this was just a brain dump of what was on my mind as I was coding today… if you find this useful and/or find some info lacking OR just incorrect, leave a comment so I can fix it.

, , , , , , , , , , , , , , , , , , , , ,

1 Comment

Fun with Variadic functions

If you’re looking for info on implementing Variadic functions, its not here, but you can find it here (in C) or on wikipedia.

Today @ work, I wrote this awesome library to wrap fprintf, printf, etc and check that line lengths do not exceed 80 chars per line (if length exceeds 80, output warning message to stderr). The first problem I ran into was how to handle the variable number of arguments that can trail in a printf function. stdarg.h comes to the rescue with Variadic functions! Then I ran in to some problems with using the va_list more than once (since the arguments get “consumed” as the va_list pointer gets incremented as each argument is accessed). So this is resolved by copying the va_list pointer using a gnu extension, but my code has to be compiler independent, so an ifdef gets added.. blah blah blah. Because of how complex this solution is getting… I consider using snprintf and then printf the output of that (so i can get the length of the string from snprintf)… but this means I’d have to go through this 3-step process each time something is output (which is bad both because its a pain in the ass and for maintainability).. and so I’d want to wrap this with a macro. But with the variable number of arguments… there wouldn’t be an easy way of doing that either. I finally get my original idea with the Variadic function working… and then I realize, that each output generated by printf is not always one line, I can printf multiple lines… or use multiple printfs to generate one line. AHH.. so, introduce a static int that counts chars per line… OR… all this is being dumped to a file anyway… the simplest solution… JUST READ THE FILE at the very end and check line length!

The lesson here, keep it simple ūüėõ Though my Varidic solution is far more elegant IMO… though excessive for what I was trying to accomplish.

, , ,

1 Comment

The coolest program ever!

Synergy is freakin’ awesome!

A Vista box w/ 23" samsung and MBP w/ 21.5" vertical.

A Vista box w/ 23" samsung and MBP w/ 21.5" vertical.

So I was setup with Vista box @ work today to do IAR/VC++ stuff… so that meant another keyboard and another mouse on my desk. Well that kinda sucks. I have this issue at home too.. I have my desktop and my laptop. I’m usually using both at the same time and I need to switch between keyboards… and blah, its just a mess. I had an idea I was considering for my senior project to develop something like this… because I had no idea it existed! But today one of my co-workers showed me this awesome program called Synergy!

So how does Synergy work?¬† It allows you to take the keyboard and mouse of one computer and over the network control any number of other computers as though they were additional monitors connected to your first computer.¬† So for example… I have my vista box on the left of my macbook pro.¬† As my mouse goes off the left edge of the macbook pro, it comes on to the right edge of the vista box.¬† HOW AWESOME IS THAT!

The cool thing about it is that it also works cross platform too! I’m so excited to set this up on all my computers at home :).

(The photo is a panorama shot before I set up Synergy, I’ll have to get another shot of my desk with the new setup when I go back in on Monday. The photo was shot using in-phone panorama mode)

, , , , , , , , , , ,

No Comments

Vista Machine Initial Setup

I just got a fresh Vista machine for work… so I thought I’d make a list of programs I’d install for general usability on the machine:

I downloaded all the PuTTy clients (putty, pscp, psftp) to the C:\Windows folder so I can hit WIN+R and type putty (server) and go.

Installed a sane browser. Chrome doesn’t work on this machine for some reason, but I usually use Firefox on Windows anyway. But I also tried Safari 4 which runs surprisingly well on this machine (was sluggish on my XP machine at home) so I guess I’ll stick to that as my default browser.

I’ll continue to update this post with the core things I install.

, , ,

No Comments

Stripping C/C++ Comments

Here’s some code to strip comments from a c/c++ file. Code is adapted from a posting at http://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments

import re

# adapted from: http://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments
# strips c/c++ comments

def strip_comment(text):
    rep = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
    pattern = re.compile(rep, re.DOTALL | re.MULTILINE)
    return re.sub(pattern,
        lambda match:(match.group(0),"")[match.group(0).startswith('/')],
        text)

, , , , ,

1 Comment

Why settle for the standard? — Picking a point and shoot.

If you shoot with a DSLR, you know its not always easy to carry a fatty DSLR with you everywhere.¬† Sometimes you want something that you can just slip in your pocket and go.¬† There are many point and shoot cameras out there and they rarely stand up to the optical performance, speed and quality of SLR systems with their nice lenses and large image sensors.¬† So when buying a point and shoot as a camera you can always have on you, why settle for a normal camera?¬† What do I mean by normal?¬† I’m currently looking for a point and shoot for myself (I shoot with the 40D and RebelXT) and what’s really catching my eye is all these cameras that have been specialized for various purposes.¬† I’m looking for a point and shoot camera that can do things my DSLR can not.¬† As a photographer I want to expand my shooting capabilities so I’m simply not interested in just buying a camera that’s a downgrade from my DSLR.¬† So far the following features have caught my eye:


Waterproof: There are waterproof enclosures for DSLRs but they’re obscenely expensive and often cost more than the DSLR body.

Pentax Optio W60 Waterproof 10MP Digital Camera with 5x Wide Angle Optical Zoom (Ocean Blue)

Olympus Stylus 550 WP 10MP Waterproof Digital Camera with 3x Optical Zoom and 2.5-Inch LCD (Blue)

High Speed Video: The 5dmk2 can shoot HD video but you have no control over frame rate.¬† You’re stuck shooting 30fps.¬† These new Casio Exilim cameras can shoot up to 1200fps!¬† They also shoot Full HD @ 30fps.

Casio High-Speed Exilim EX-FC100 9.1 MP Digital Camera with 5x Optical Image Stabilized Zoom and 2.7-inch LCD (Black) (check out the cool video on the product page)

Casio Exilim Pro EX-F1 Digital Camera, 6.0 MP, with 60fps High Speed Burst Mode, Full HD Movies, 12x Optical, 4x Digital Zoom, 2.8 — though this one might be a bit bulky for a pocket-camera.

Other cool features that I saw are touch screen cameras that implement touch AF.  A problem I usually have when I use point and shoot cameras is trying to get a focus lock on my subject when there are other things in the frame.  This would be a nice feature to have in a point and shoot.

(The new iPhone has this feature, but I’m pretty sure I heard of a Nikon camera that has it too — I’ll post a link when I have a chance to find it).

If you’ve tried out or are using any of the cameras I’ve listed above (or would like to recommend another cool camera) please leave me some comments!

, , , , , , , , , , , , , , , ,

No Comments

Graduation Cap Ideas?

This guy has inspired me:

I’m thinking, maybe I’ll build a webserver on my cap with a wifi access point. ūüėõ This should be fun.

,

8 Comments

Maybe I should consider Lenovo?

What are you doing Apple? ¬†It seems like your product lines are having an identity crisis. ¬†It is my understanding that the MacBook Pro (MBP) was meant to be a “pro” quality laptop… and now some lower-end models with integrated graphics are being merged into the pro line? ¬†And what’s up with the SD card slot replacing the ExpressCard/34 slot? ¬†If your product is truly targeted at creative professionals as it seems to be a popular choice among¬†photographers¬†and videographers, why does your laptop not support the standard which is CF — or at the least have a multi-card reader? ¬†SD cards are most¬†prominent¬†in lower-end cameras which a professional would not¬†primarily¬†use. ¬†And what the heck is up with the built-in battery? ¬†What¬†benefit¬†to the user could a built-in battery¬†possibly¬†provide? ¬†Also… the ultra-glare-prone glossy screen is quite obnoxious — especially when you’re paying $2k for a laptop and there’s no option for matte unless you get their top-of-the-line 17″ model… but even then, they charge you extra for the matte option.

I love my current MacBook Pro but its getting kind of outdated after 3 years. ¬†I hope Apple will fix their “pro” line by next year when I buy myself a new laptop. ¬†Otherwise I’m really considering getting a Lenovo instead.

, , , , , , , , ,

No Comments

ACM‚ÄďWGBH Initiative Finds Large Gender Gap

An ACM press release today announced that they have findings which confirm that there is low interest among females in computer science. Well we already knew that… so how do we fix it? That’s the real question.

And another interesting point from the press release:

80 percent of today‚Äôs college freshman‚ÄĒthe very students that grew up with computers‚ÄĒsaid they had no idea what computer science majors actually do.

Awesome.

Anyway, I’m excited to be a part of our school’s outreach initiatives to increase interest in computer science. Hopefully, more incoming students will actually be aware of what computer science really is.

Links: Press Release <http://www.acm.org/press-room/news-releases/nic-interim-report/view>

, , , , , ,

1 Comment

Triggering a Canon camera using Pocketwizard without an $80 cable.

So you have PocketWizzardbut you don’t want to buy their expensive pre-trigger cable.. so what do you need to do to trigger your camera without an obscenely overpriced cable?

For the Rebel series, its easy… just buy a cheep 3.5mm to 2.5mm headphone adapter… they sell them at Radio Shack.

Connecting a EOS Rebel to a Pocketwizard_MG_0288

For any other Canon camera (with the N3 plug) its a bit trickier… its not exactly a standard connector… so after some hunting, I finally found this sweet adapter on Amazon which only cost $8.49 (as of 5/25): Camera Adapter for Canon with N-3 Plug

N3 to 3.5mm adaptor!

(In comparison to the pricey Pocket Wizard 804-511 CM-N3-P Canon N3 Pre-Releaseable Motor Drive Cord)

Just connect a normal 3.5mm audio cable (either mono or stereo work fine) to your Pocketwizzard and you’re good to go!

, , , , , , , , , , , , , , , , , , , , , , ,

17 Comments