Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Advanced C

Reply
Thread Tools

Advanced C

 
 
Malcolm
Guest
Posts: n/a
 
      11-27-2005
"pemo" <(E-Mail Removed)> wrote
>
> However, I've [these days] rarely encountered unions in live code, or in
> finding I'm using a library that requires them etc. So, are unions
> something that ppl. find they deal with as commonly as, say, structs?

Almost never.
The main use of a union is to define a data "packet" for some type of
messaging system. However normally you want such messages passed over a
wire, so they need to be serialised, and unions are not a portable way of
doing that. So instead you specify the messge format byte byte byte, and
then unpack.


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-27-2005
pemo wrote:
> [...]
>
> Off the top of my head, here are some other things that are on my general
> candidate list [knowing the audience (mostly Oxford grad students)] - some
> to do with general programming things, others to do more with C ...
>
> function pointers and signal() etc.
>
> defensive programming techniques.
>
> mixing in assembler with C.
>
> writing modular code, using your own header files effectively.
>
> data-structures - like linked-lists, trees and graphs.
>
> keywords like _Bool, restrict, register, ...
>
> more on manipulating strings (strtok(), etc).
>
> interacting with the operating system.
>
> debugging using gdb etc.
>
> source control.
>
> lint.
>
> using thrid party libraries [for stats].


Of these, the only unequivocally "about C" items are
the first and sixth, with a possible nod to the seventh.
The other nine or nine and a half are not about C at all
(no, not even the third). That's not to say it couldn't
be a useful course, but "Advanced C" isn't the title I'd
give it.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Marc Boyer
Guest
Posts: n/a
 
      11-28-2005
Eric Sosman <(E-Mail Removed)> wrote:
> It's hard for me to imagine a responsibly-taught course
> that omitted unions and bit-fields. Even a very short course
> of half a dozen lectures should at least mention them, even
> if it didn't spend time on them.


Why ?
What is the benefit of union, except memory saving and
some advanced POO-like programming technics ?
And with bit-fields, what is the use except on some
raw I/O, where endianess, sign encoding, bit padding and
memory alignements are also needed ?

Marc Boyer
 
Reply With Quote
 
Ingo Menger
Guest
Posts: n/a
 
      11-28-2005

Marc Boyer schrieb:

> Eric Sosman <(E-Mail Removed)> wrote:
> > It's hard for me to imagine a responsibly-taught course
> > that omitted unions and bit-fields. Even a very short course
> > of half a dozen lectures should at least mention them, even
> > if it didn't spend time on them.

>
> Why ?
> What is the benefit of union, except memory saving and
> some advanced POO-like programming technics ?


They are almost indespensable for implementing sum data types (as
opposed to product data types which are often implemented using
structs).
For example:

enum literalkind { Float, Int, Char };
struct literal {
enum literalkind kind;
union {
float u_flt;
int u_int;
char u_chr;
} l_val;
};

 
Reply With Quote
 
Marc Boyer
Guest
Posts: n/a
 
      11-28-2005
Ingo Menger <(E-Mail Removed)> wrote:
> Marc Boyer schrieb:
>> Eric Sosman <(E-Mail Removed)> wrote:
>> > It's hard for me to imagine a responsibly-taught course
>> > that omitted unions and bit-fields. Even a very short course
>> > of half a dozen lectures should at least mention them, even
>> > if it didn't spend time on them.

>>
>> Why ?
>> What is the benefit of union, except memory saving and
>> some advanced POO-like programming technics ?

>
> They are almost indespensable for implementing sum data types (as
> opposed to product data types which are often implemented using
> structs).
> For example:
>
> enum literalkind { Float, Int, Char };
> struct literal {
> enum literalkind kind;
> union {
> float u_flt;
> int u_int;
> char u_chr;
> } l_val;
> };


Yes, but how often are 'sum data types' usefull ?
I am in the context of 'a very short course'.
My opinion (can be discussed) is that, in real life,
when unions are needed, then so many others also are that
this is no more for beginners.

Marc Boyer
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-28-2005
Marc Boyer wrote:
> Eric Sosman <(E-Mail Removed)> wrote:
>
>> It's hard for me to imagine a responsibly-taught course
>>that omitted unions and bit-fields. Even a very short course
>>of half a dozen lectures should at least mention them, even
>>if it didn't spend time on them.

>
>
> Why ?
> What is the benefit of union, except memory saving and
> some advanced POO-like programming technics ?
> And with bit-fields, what is the use except on some
> raw I/O, where endianess, sign encoding, bit padding and
> memory alignements are also needed ?


IMHO, a responsibly-taught C course would cover gets().
I do not advocate using gets(), but if the neophyte programmer
has never been taught to recognize it and warned against it,
he has not been prepared against a serious peril.

Similarly with unions: Their legitimate uses are few,
but their hackish uses are frequent. The programmer will
certainly encounter unions, and will have been ill-prepared
if the course never mentioned them.

Let me turn the question around: If you were preparing
a course on C, what features would you omit altogether?
Would you omit `do...while'? It's infrequently used, after
all. How about `goto'? It's even rarer, yet your students
will eventually run into it. `longjmp'? I hope you'd at
least mention that it exists, perhaps with a caution that
it's tricky to use well. `long double'? `<stdarg.h>'?
`continue'?

I'm not saying that an introductory course should cover
all such matters in great detail. More attention should be
paid to mundane matters, like how `switch' works or why it's
a bad idea to return a pointer to an `auto' variable. But
even an introductory course ought to erect a few signposts
to describe the unexplored territory, so the graduate will not
be utterly lost when he parachutes into it.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Marc Boyer
Guest
Posts: n/a
 
      11-28-2005
Le 28-11-2005, Eric Sosman <(E-Mail Removed)> a crit*:
> Marc Boyer wrote:
>> Eric Sosman <(E-Mail Removed)> wrote:
>>
>>> It's hard for me to imagine a responsibly-taught course
>>>that omitted unions and bit-fields. Even a very short course
>>>of half a dozen lectures should at least mention them, even
>>>if it didn't spend time on them.

>>
>>
>> Why ?
>> What is the benefit of union, except memory saving and
>> some advanced POO-like programming technics ?
>> And with bit-fields, what is the use except on some
>> raw I/O, where endianess, sign encoding, bit padding and
>> memory alignements are also needed ?

>
> IMHO, a responsibly-taught C course would cover gets().
> I do not advocate using gets(), but if the neophyte programmer
> has never been taught to recognize it and warned against it,
> he has not been prepared against a serious peril.


Why presenting gets() if fgets() is presented ?
man gets

> Similarly with unions: Their legitimate uses are few,
> but their hackish uses are frequent. The programmer will
> certainly encounter unions, and will have been ill-prepared
> if the course never mentioned them.


Yes, but in a limited teaching time, must we spend time
to describe hacks or good practices ?

> Let me turn the question around: If you were preparing
> a course on C, what features would you omit altogether?


I am teaching C.

> Would you omit `do...while'? It's infrequently used, after
> all.


Yes, but this is a 0-overhead since the notion have been
presented with Pascal.

> How about `goto'? It's even rarer, yet your students
> will eventually run into it.


Not presented.

> `longjmp'? I hope you'd at
> least mention that it exists, perhaps with a caution that
> it's tricky to use well.


Neither.

> `long double'?


Yes, also a 0 overhead notion (float, double, long-double).

> `<stdarg.h>'?, `continue'?


No

> I'm not saying that an introductory course should cover
> all such matters in great detail. More attention should be
> paid to mundane matters, like how `switch' works or why it's
> a bad idea to return a pointer to an `auto' variable. But
> even an introductory course ought to erect a few signposts
> to describe the unexplored territory, so the graduate will not
> be utterly lost when he parachutes into it.


My choice is (but can be discussed) is that there is a
lot of C documentation (and some is given), and I prefer to
takes time on usefull and hard notions, and rely on the
adaptation capacity of the students for the others.
With any C documentation with an index, you can understand
what 'continue' does. With sizeof, malloc and others,
this is not the case.

Marc Boyer
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-28-2005

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Marc Boyer wrote:
>> Eric Sosman <(E-Mail Removed)> wrote:
>>
>>> It's hard for me to imagine a responsibly-taught course
>>>that omitted unions and bit-fields. Even a very short course
>>>of half a dozen lectures should at least mention them, even
>>>if it didn't spend time on them.

>>
>>
>> Why ?
>> What is the benefit of union, except memory saving and
>> some advanced POO-like programming technics ?
>> And with bit-fields, what is the use except on some
>> raw I/O, where endianess, sign encoding, bit padding and memory
>> alignements are also needed ?

>
> IMHO, a responsibly-taught C course would cover gets().
> I do not advocate using gets(), but if the neophyte programmer
> has never been taught to recognize it and warned against it,
> he has not been prepared against a serious peril.
>
> Similarly with unions: Their legitimate uses are few,
> but their hackish uses are frequent. The programmer will
> certainly encounter unions, and will have been ill-prepared
> if the course never mentioned them.
>
> Let me turn the question around: If you were preparing
> a course on C, what features would you omit altogether?
> Would you omit `do...while'? It's infrequently used, after
> all. How about `goto'? It's even rarer, yet your students
> will eventually run into it. `longjmp'? I hope you'd at
> least mention that it exists, perhaps with a caution that
> it's tricky to use well. `long double'? `<stdarg.h>'?
> `continue'?
>
> I'm not saying that an introductory course should cover
> all such matters in great detail. More attention should be
> paid to mundane matters, like how `switch' works or why it's
> a bad idea to return a pointer to an `auto' variable. But
> even an introductory course ought to erect a few signposts
> to describe the unexplored territory, so the graduate will not
> be utterly lost when he parachutes into it.


Good points from everyone - thanks for the input/effort so far.

FYI, I have included gets(), buffer-overruns, fgets(), do ... while and
goto! However, I'm sure that I ought to have included some things that I've
recently realised are missing, e.g., I haven't mentioned enums yet, or
returning the address of a frame variable (or unions, bit-fields ...)!

However, it's hard to get *everything in* that perhaps ought to be - esp.
true if you yourself don't frequently use some features. E.g., I rarely use
enums/unions or *goto* etc BTW, the goto appeared [as an aside] when we
were looking at loops.

The difficulty in the development of this course has been slightly
compounded by the fact that I originally 'inherited' it. I've spent two
terms re-doing many parts of it (as well as doing my own research stuff),
but it's very far from perfect. Even further hampered by the fact that my
serious usage of C predates the latest stuff, esp. c99 - c.l.c has been
invaluable here!

Lastly, if every student took both courses, they'd have 21 hours of
tutorials and practicals - it's rather hard [frightening] to think that
they'll be let loose on department code after such a short time: so, it's
even more important to only include useful stuff (maybe goto should go!!)





 
Reply With Quote
 
Ingo Menger
Guest
Posts: n/a
 
      11-28-2005

Marc Boyer schrieb:

> Ingo Menger <(E-Mail Removed)> wrote:
> > Marc Boyer schrieb:
> >> Eric Sosman <(E-Mail Removed)> wrote:
> >> > It's hard for me to imagine a responsibly-taught course
> >> > that omitted unions and bit-fields. Even a very short course
> >> > of half a dozen lectures should at least mention them, even
> >> > if it didn't spend time on them.
> >>
> >> Why ?
> >> What is the benefit of union, except memory saving and
> >> some advanced POO-like programming technics ?

> >
> > They are almost indespensable for implementing sum data types (as
> > opposed to product data types which are often implemented using
> > structs).



> Yes, but how often are 'sum data types' usefull ?


Hmmm...
I'd say, before I learned the concept through Haskell (where they are
known as algebraic data types), I didn't use them at all. Nowadays I
find myself cursing the dumb and stupid language I have to work with,
and that doesn't have them, such as for example java.

It's quite comparable to class types and object oriented programming.
How often were objects useful in the seventies and eighties? But today,
everything ought to be an object.

> My opinion (can be discussed) is that, in real life,
> when unions are needed, then so many others also are that
> this is no more for beginners.


Sure, but the topic is "Advanced C".
And certainly, unions deserve much more attention than, for example
bitfields. Whereas the former is an concept that supports advanced
(altough rarely known) programming paradigms, the latter is just
another way of storing a bunch of unsigned numbers using less space.

 
Reply With Quote
 
Marc Boyer
Guest
Posts: n/a
 
      11-28-2005
Ingo Menger <(E-Mail Removed)> wrote:
> Marc Boyer schrieb:
>> Ingo Menger <(E-Mail Removed)> wrote:
>> > Marc Boyer schrieb:
>> >> Eric Sosman <(E-Mail Removed)> wrote:
>> >> > It's hard for me to imagine a responsibly-taught course
>> >> > that omitted unions and bit-fields. Even a very short course
>> >> > of half a dozen lectures should at least mention them, even
>> >> > if it didn't spend time on them.
>> >>
>> >> Why ?
>> >> What is the benefit of union, except memory saving and
>> >> some advanced POO-like programming technics ?
>> >
>> > They are almost indespensable for implementing sum data types (as
>> > opposed to product data types which are often implemented using
>> > structs).

>> Yes, but how often are 'sum data types' usefull ?

> Hmmm...
> I'd say, before I learned the concept through Haskell (where they are
> known as algebraic data types), I didn't use them at all. Nowadays I
> find myself cursing the dumb and stupid language I have to work with,
> and that doesn't have them, such as for example java.


OK, but can't you do the same with dynamic polymorphism ?

And, since we are on clc, which use in C ?

>> My opinion (can be discussed) is that, in real life,
>> when unions are needed, then so many others also are that
>> this is no more for beginners.

>
> Sure, but the topic is "Advanced C".


I could have changed the subject, but I was answering
a post on 'Even a very short course [...] should at least
mention them'.

> And certainly, unions deserve much more attention than, for example
> bitfields. Whereas the former is an concept that supports advanced
> (altough rarely known) programming paradigms, the latter is just
> another way of storing a bunch of unsigned numbers using less space.


Yes

Marc Boyer
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Advanced Python Programming Oxford Lectures [was: Re: *Advanced*Python book?] Michele Simionato Python 1 03-27-2010 06:10 AM
Centon Advanced A2X512S4400HB 1GB Dual Channel Kit @ Insa... Silverstrand Front Page News 0 10-24-2005 01:45 PM
Centon Advanced A2X512S4000LLU 1GB Dual Channel Kit Silverstrand Front Page News 0 10-21-2005 01:32 PM
Antec P180 Advanced Super Mid Tower @ ThinkComputers.org Silverstrand Front Page News 0 10-14-2005 12:53 AM
Centon Advanced GEMiNi PC3200 Dual-Channel Kit Silverstrand Front Page News 0 08-08-2005 09:25 PM



Advertisments