Need Suggestions

valdis.kletnieks at vt.edu valdis.kletnieks at vt.edu
Wed Jul 25 18:10:16 EDT 2018


On Thu, 26 Jul 2018 03:07:27 +0530, Himanshu Jha said:

> Now, this part of section is really important and somehow if you think
> you really learnt C well in the course offered at University. Then
> please take a look at implicit conversions:
> https://en.cppreference.com/w/c/language/conversion
>
> 	assert(sizeof(int) > -1);
>
> That should be enough to shatter your confidence.

If you want your confidence shaken:

https://www.lysator.liu.se/c/duffs-device.html

Fortunately, most maintainers will frown on stuff like that unless there's
a *damned* good reason - and Tom Duff had a very good reason indeed.

(Intro-to-c pop quiz - why is strcpy() not usable here?)

gcc 8.1.1 will accept Tom's code (with 3 warnings due to it
being pre-ANSI C).

For those who took a class on compiler design, and *think* they
know how LALR parsers and that stuff works - Examine the lexical
nesting of that, and figure out how the devil the compiler doesn't
get indigestion.. :)

And the K&R book on C does successive trimming of an strcpy(),
and finishes with

void strcpy(char *a, *b) { while (*a++ = *b++); }

The thing that's actually used a lot in the Linux kernel that gives
a lot of C newcomers heartburn is the widespread use of
structures of function pointers.. For example, this from include/linux/fs.h:

struct lock_manager_operations {
	int (*lm_compare_owner)(struct file_lock *, struct file_lock *);
	unsigned long (*lm_owner_key)(struct file_lock *);
	fl_owner_t (*lm_get_owner)(fl_owner_t);
	void (*lm_put_owner)(fl_owner_t);
	void (*lm_notify)(struct file_lock *);	/* unblock callback */
	int (*lm_grant)(struct file_lock *, int);
	bool (*lm_break)(struct file_lock *);
	int (*lm_change)(struct file_lock *, int, struct list_head *);
	void (*lm_setup)(struct file_lock *, void **);
};

That's the structure definition.  Novice challenge: Find one or more places
where this structure is initialized, and understand how and why that works.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 486 bytes
Desc: not available
URL: <http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20180725/f95352e5/attachment.sig>


More information about the Kernelnewbies mailing list