Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #5275
| From | Arved Sandstrom <asandstrom3minus1@eastlink.ca> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Android—Why Dalvik? |
| References | (17 earlier) <isck8i$92j$1@dont-email.me> <isee3b$sj9$1@lust.ihug.co.nz> <isnvt2615kt@news2.newsguy.com> <3KTHp.3576$PA5.1424@newsfe01.iad> <it0b3c01q2f@news2.newsguy.com> |
| Message-ID | <GU8Jp.50669$5f.28538@newsfe19.iad> (permalink) |
| Organization | Public Usenet Newsgroup Access |
| Date | 2011-06-12 16:59 -0300 |
On 11-06-11 02:38 PM, Michael Wojcik wrote: > Arved Sandstrom wrote: >> >> I might note that I ran across one comment by an Axis 2 C developer >> where he said that the model to be followed was >> >> "typedef done inside the header and the struct declaration is in >> source...in a case you still want to move the struct to the header (it >> is not a much recommended approach in c programming) ... [Ed. How-To >> description of procedure follows]" >> >> Maybe I missed something in my years away from C, but those >> recommendations were new to me. > > His description's not quite right, but incomplete structure > declarations are an important aspect of encapsulation in C. > > Ignore any mention of "typedef" for a moment. typedef is a misnomer, > since it doesn't define a type, just an alias for an existing type. > (It's also of questionable utility. Some people think it's useful for > defining complex function-pointer types; I say if you don't understand > C's function-pointer syntax, don't write C code.) Yeah, the typedef mention I ignored. I would use it myself with structs for the reason that everyone does, so that you don't have to retype 'struct' all the time. What the Axis 2 C developer meant by that bit is basically the incomplete structure declaration that you mention below. Which makes sense in general. > In C, new types are defined using the struct keyword (and sometimes > union, but that's really just a specialized struct). The struct > keyword can do either or both of two things: > > - define a structure type > - introduce a type name into the struct-tag namespace > > The former is necessary if you want to inspect the contents of an > object of the type, evaluate its size or the size of the type, etc. > But it is not necessary to define certain derivative types, such as > the const-qualified equivalent type, or the associated pointer type. > > The latter is what lets you encapsulate. In a header, you provide an > incomplete structure declaration and an API that uses the pointer type > derived from it: > > struct foo; > struct foo *CreateFoo(); > DoThingToFoo(struct foo *, ...); > PureFunctionOnFoo(const struct foo *, ...); > DeleteFoo(struct foo *); > > Consumers of your API have no access to the implementation of struct > foo, so they're insulated from any changes to it. And since struct foo > * is a perfectly good object pointer, they can do whatever they'd do > with any other pointer, except dereference it. > > (You can of course wrap "struct foo" in a typedef, if the people who > use your API are too lazy to type the word "struct".) Which is what the Axis 2 C headers do. Except that they are quite inconsistent in their use of the typedef; makes you wonder why they threw it in. > Note the initial "struct foo;" is necessary. Otherwise the use of an > unknown "struct foo" in the declarations of the API would only > introduce the type name in "prototype scope", which ends at the end of > each declaration. Prototype scope is basically useless. > > This is a useful and fairly widely used technique - though not nearly > as widely as it should be. Of course, the API has to provide for > whatever its consumers need, since the consumers don't have direct > access to the contents of the structure, can't allocate or copy one, etc. > I'm cool with all this. Good explanation. I learnt C quite a long time ago and don't remember that incomplete types were being strongly pushed back then. Nor have I used C much in over 20 years. I must admit, I'm much more aware of comparable techniques in C++, and simply never made the backwards leap. Bit too much of not seeing the forest for the trees. I suspect that when I did write a fair bit of C, that I wrote the client code in such a way that I usually needed the full declaration anyway - that is, no particular APIs to wrap the anticipated handling of the struct. You make a good point about writing the APIs in such a way that client code can do what it needs to do with only the pointer. This is where Axis 2 C made this mistake, as I described. Their WSDL stub generator (and probably their skeleton generator also) produces code that deferences at least one very important struct pointer. The relevant header (and accompanying implementation) has no provision for supplying a suitable function that would obviate the need to dereference. As I see it you'd have 2 options here - (1) expose the complete struct in the header, and not change the client, or (2) add a function to carry out the operation that the client code requires, which means modifying both the client (the code generator actually if you're going to do this a lot) and the library. Either way you have to modify the library code, which is not something you should require of your library users. AHS
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-05-31 13:56 +1200
Re: Android—Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-05-31 11:10 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 07:13 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-05-31 12:43 -0700
Re: Android?Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 08:00 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-05-31 13:33 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 09:29 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-05-31 17:13 -0700
Re: Android?Why Dalvik? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-05-31 22:03 +0000
Re: Android?Why Dalvik? BGB <cr88192@hotmail.com> - 2011-05-31 16:08 -0700
Re: Android—Why Dalvik? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-05-31 21:09 +0000
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 09:27 +1200
Re: Android—Why Dalvik? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-05-31 22:25 +0000
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-05-31 15:20 -0700
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-05-31 12:11 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 07:59 +1200
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-05-31 15:01 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-01 15:05 +1200
Re: Android—Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-01 00:58 -0400
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-01 03:21 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 09:40 -0400
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-03 12:17 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 17:06 -0400
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-03 16:04 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-07 11:42 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-02 11:54 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-01 17:43 -0700
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-01 17:43 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-03 15:08 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-02 20:50 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-03 17:34 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-02 23:20 -0700
Re: Android?Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-03 18:43 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-03 08:27 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-04 16:02 +1200
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-03 22:24 -0700
Re: Android?Why Dalvik? Gene Wirchenko <genew@ocis.net> - 2011-06-06 13:29 -0700
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-06 14:15 -0700
Re: Android?Why Dalvik? Gene Wirchenko <genew@ocis.net> - 2011-06-07 13:59 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-08 12:55 +1200
Re: Android—Why Dalvik? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-08 06:18 -0300
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-08 07:06 -0700
Re: Android?Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-08 10:25 -0400
Re: Android?Why Dalvik? Gene Wirchenko <genew@ocis.net> - 2011-06-08 10:56 -0700
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-08 14:11 -0700
Re: Android?Why Dalvik? Steve Sobol <sjsobol@JustThe.net> - 2011-06-08 14:09 -0700
Re: Android?Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 09:46 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-04 16:08 +1200
Re: Android—Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-04 02:40 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-05 15:46 +1200
Re: AndroidWhy Dalvik? Gene Wirchenko <genew@ocis.net> - 2011-06-06 13:26 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-07 10:23 +1200
Re: AndroidWhy Dalvik? Gene Wirchenko <genew@ocis.net> - 2011-06-07 13:55 -0700
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-08 12:55 +1200
Re: Android—Why Dalvik? "H.J. Sander Bruggink" <sander.bruggink@uni-due.de> - 2011-06-06 11:21 +0200
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-07 13:40 +1200
Re: Android—Why Dalvik? "H.J. Sander Bruggink" <sander.bruggink@uni-due.de> - 2011-06-07 10:16 +0200
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-07 01:30 -0700
Re: AndroidWhy Dalvik? rossum <rossum48@coldmail.com> - 2011-06-02 10:35 +0100
Re: Android�Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-02 03:32 -0700
Re: Android�Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-02 11:07 -0400
Re: Android�Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-02 10:07 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 09:38 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-04 12:21 +1200
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-07 11:48 -0400
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 09:31 -0400
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-03 12:45 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-03 17:14 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-04 12:23 +1200
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-03 19:01 -0700
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-07 11:59 -0400
Re: Android—Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-04 02:44 -0400
Re: Android—Why Dalvik? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-05 11:11 +1200
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-08 10:10 -0400
Re: Android—Why Dalvik? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-08 20:38 -0300
Re: Android—Why Dalvik? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-08 17:28 -0700
Re: Android—Why Dalvik? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-08 23:41 -0300
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-11 13:38 -0400
Re: Android—Why Dalvik? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-12 16:59 -0300
Re: Android—Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-15 14:01 -0400
Re: Android—Why Dalvik? BGB <cr88192@hotmail.com> - 2011-06-08 22:46 -0700
Re: Android---Why Dalvik? Michael Wojcik <mwojcik@newsguy.com> - 2011-06-11 13:39 -0400
csiph-web