Archive for the ‘Random’ Category
C programming hints and code-style (part 1)
Over the years I’ve had to deal with many different coding styles (can something without style be called style?). Here is a list of no-brainer tips that every C programmer should be aware of.
Hints
Update:
Have a copy of the C99 spec handy
Don’t scratch your head when you are not sure if a function is part of the standard, and what is the expected behavior, open the spec an check by yourself: here.
free() can receive NULL
There’s no need to do:
if (pointer)
free(pointer);
This works fine:
free(pointer);
Therefore if you write a struc_free function it makes sense to allow NULL as an argument.
There’s no need to cast a void *
tmp = (EXTEREMELY_UGLY_TYPE *) malloc(10);
malloc() returns a void * there’s no need to cast those:
tmp = malloc(10);
sizeof can receive a variable
size = sizeof(EXTEREMELY_UGLY_TYPE);
No need to burden yourself with that:
size = sizeof(tmp);
If tmp is a pointer you can do sizeof(*tmp) and so on.
Avoid big macros
Have you seen horrendous code like this?
#define ERROR(e, code, text) \
G_STMT_START { \
if ((text)) \
GST_WARNING_OBJECT ((e), "error: %s", (text)); \
gst_element_message (GST_ELEMENT((e)), GST_MESSAGE_ERROR, \
GST_STREAM_ERROR, (code)); \
} G_STMT_END
It’s much cleaner to use an inline function:
static inline void send_error(GstElement *e, int code, const char *text)
{
if (text)
GST_WARNING_OBJECT(e, "error: %s", text);
gst_element_message(e, GST_MESSAGE_ERROR, GST_STREAM_ERROR, code);
}
Yes, you can put inline functions in header files.
C99 is your friend
C99 has nice types such as bool (true and false), uint32_t and similar int types. Also these beauties:
static struct device = {
.base = 0x480bd400,
.irq = 24,
.pdata = {
.name = "isp",
.nr_tlb_entries = 8,
.clk_name = "cam_ick",
},
};
I don’t even want to imagine how horrible the code would look for C89.
Those are the ones I can think about, any suggestions? On the next part I’ll about code-style
Simpsons scenes and their reference movies
Webbalert’s podcast of today has a link to “Simpsons Scenes and their Reference Movies”. It’s simply awesome.
Update: Dario points out that they just copied stuff from actualidad simpson. They have even more stuff! Thanks Dario.
NFS in Fedora 7 (and iptables)
Before I forget how to do it:
Use “lsof -i” to find out the ports you need to open. Look for rpcbind and rpc.mount.
rpcbind 2110 rpc 8u IPv6 7514 TCP *:sunrpc (LISTEN)
rpc.mount 2479 root 7u IPv4 8622 TCP *:59287 (LISTEN)
Now add them to iptables:
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport sunrpc -j ACCEPT
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 59287 -j ACCEPT
I guess you can edit “/etc/sysconfig/nfs” and set MOUNTD_PORT to some value so you don’t have to check it.
That’s all I had to do, but for more information check here.
Unique?
Meet Tim Bray
Linux Journal provides an interesting interview with Tim Bray.
I didn’t know this guy, but his interests seem pretty similar to mine ones. Ruby, XML, Atom, equal oportunity to the world.
I specially liked the quote:
The Net itself is a contribution, by humanity to humanity, the engine of future contribution and experience.
I don’t know about you but I’ll definetly add his blog to my blogroll.
Amazing 3D Sidewalk Art Photos
Neat! I would like to get a photo in the one with Batman and Robin.
These unbelievable photos are chalk drawings done by Julian Beever and Kurt Wenner. Both Julian and Kurt have different styles to create an amazing 3D illusion.
Free TV episodes: Penn & Teller: Bullshit! (fun and enlightening)
I found this Links to free episodes of Penn & Teller: Bullshit!.
I watched a few of them and I must say I love them.
They’re simple, straight, fun, honest, and obvious.
I specially liked the one about the War on Drugs; very enlightening.
I like to respect other people’s opinions, but in this case, I will say that if somebody don’t believe what these guys are saying, that somebody is nuts.
powered by performancing firefox
Photorealistic CG Water
This video is simply amazing. One more thing that looks like real in CG, I really cannot tell the difference.
This video, titled “Flow” shows off new footage of Scanline’s Flowline VFX software. It was shown at SIGGRAPH 2006.
3D Animation of Linux source code development
I remember a long time ago I saw this site, and I thought it was great. I thought that was only the beginning and we would see more of that stuff. Unfortunately that has not been the case.
I hope Pascal Brisset, resumes his effort, it would be pretty nice to see visually how the Linux Kernel has been developing. If he doesn’t I might try to do it.
Anyway, that makes me think about the Free Code Graphing Project. For huge projects a graphical representation of how stuff is organized helps a lot. Even small projects might benefit from that. It would be great to have some kind of IDE plug-in that generates a map so you can easily navigate through the huge code. Until now only Doxygen has helped me with such kind of projects.