[CVALE] Question about structured programming... (CProgramming Specifically)

Landon Blake lblake at ksninc.com
Fri May 18 10:42:03 PDT 2007


Thanks Pandora.

A quick internet search turned up this book, which I think will help me
make the jump from Java to C programming:

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

Landon

-----Original Message-----
From: cvale-bounces at lists.fire2wire.com
[mailto:cvale-bounces at lists.fire2wire.com] On Behalf Of Pandora
Sent: Friday, May 18, 2007 10:27 AM
To: cvale at cvale.org
Subject: Re: [CVALE] Question about structured programming...
(CProgramming Specifically)

I find C to be a better language for object oriented programming than 
say, C++ or java for instance. It is possible to have opaque types with 
no conflict between circular dependencies in C. In C++ it's possible, 
but only because you'll be compiling C code with a C++ compiler; all the

"features" of C++ don't work with this method very well. Several 
assumptions C++ programmers are expected to make fall apart when you try

to introduce separation between interface and implementation.  Java has 
support for opaque types and clear separation between interface and 
implementation, but it's got a host of other problems including the 
horrors of namespace orgies.

What I'm talking about is this:

///////////////////////////////////////////////////////
// thingy.h
typedef struct thingy_s *thingy;
thingy thingy_new(...);
thingy_modify(thingy,...);

///////////////////////////////////////////////////////
// thingy.c
#include "thingy.h"

struct thingy_s {
     GUI dependency;
     Either Microsoft or Everyone Else;
     Fast Math library dependency;
};

thingy thingy_new(...) { ... }
thingy_modify(thingy self,...) { ... }
///////////////////////////////////////////////////////

When I do object oriented programming in C, that is the sort of 
technique I use. You end up with something like a java interface, with 
only pure function calls that can get at the implementation, which is 
defined outside of the header file.  Everything else, such as protocols,

templates, private and protected members, inheritance, even strong 
typing, are all largely useless in my book.  Often even my opaque 
structure scheme isn't necessary, because it's only useful in isolating 
circular or complicated dependencies. I don't rely on it to protect my 
data from myself.

My point is that C is excellent at object-oriented programming, and you 
should do what you intend to do, no more and no less. If that involves 
objects so be it. If it involves pure functions, so be it. If it 
involves discrete state changes across multiple processes, you might 
want to consider erlang.

> They don't mimic an object oriented programming technique in the Linux
> kernel, do they?

They don't mimic an object oriented programming technique. They program 
with objects! No mimicking or fakery involved. An example is an object 
known as an "open file". It has the following base class:
///////////////////////////////////////////////////////
// linux/fs.h

struct file {
	/*
	 * fu_list becomes invalid after file_free is called and queued
via
	 * fu_rcuhead for RCU freeing
	 */
	union {
		struct list_head	fu_list;
		struct rcu_head 	fu_rcuhead;
	} f_u;
	struct path		f_path;
#define f_dentry	f_path.dentry
#define f_vfsmnt	f_path.mnt
	const struct file_operations	*f_op;
	atomic_t		f_count;
	unsigned int 		f_flags;
	mode_t			f_mode;
	loff_t			f_pos;
	struct fown_struct	f_owner;
	unsigned int		f_uid, f_gid;
	struct file_ra_state	f_ra;

	unsigned long		f_version;
#ifdef CONFIG_SECURITY
	void			*f_security;
#endif
	/* needed for tty driver, and maybe others */
	void			*private_data;

#ifdef CONFIG_EPOLL
	/* Used by fs/eventpoll.c to link all the hooks to this file */
	struct list_head	f_ep_links;
	spinlock_t		f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
	struct address_space	*f_mapping;
};
///////////////////////////////////////////////////////

Of course, normally you don't throw this huge structure around, instead 
you throw around a descriptor indexed to one of these structures, like 
what is returned from open(2).  So yes, the kernel does use objects, 
whenever there is an object for it to use.

> Should I force myself to write programs in C that
> adhere to the principles of structured programming?

People love making principles; some even do it for a living. Be wary 
that you only apply principles that make sense, even if someone claims 
it will doom you to some horrible fate in the future. If that doom is 
imminent, then that someone will be able to explain why.

_______________________________________________
cvale mailing list
cvale at lists.fire2wire.com
http://lists.fire2wire.com/mailman/listinfo.cgi/cvale


Warning:
Information provided via electronic media is not guaranteed against defects including translation and transmission errors. If the reader is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this information in error, please notify the sender immediately.



More information about the cvale mailing list