Re: Data abstraction in C



"jacob navia" <jacob@xxxxxxxxxxxxxxxx> wrote in message
news:431601bc$0$27440$8fcfb975@xxxxxxxxxxxxxxxxxx
....
> AND NOW (at last) you use it like this:
>
> #include "list.h"
> int main(void)
> {
> LIST *myList = newList(12);
>
> myList->lpVtbl->Append(myList,"Element1");
> myList->lpVtbl->Append(myList,"Element2");
> int d = myList->lpVtbl->Length(myList); // Should be 2
> }
....
> C is more explicit than C++, and there is no compiler help.
> This is less of a problem than what it seems at first sight,
> and MUCH more powerful. You are in control. There is no
> black box of a compiler to keep you from doing what you want.
>
> Also, there are no rules other than the rules you decide to follow.

Quite right. classes/objects, inheritance, etc etc, and the whole OOP aren't
magic and C++ and OOP aided languages aren't the only to benefit from the
OOP ideas and OOP-like functionality. Things like that can be implemented in
C and, obviously, :) in ASM. It's just that langs like C++ were
intentionally made to support OOP natively and easily, freeing the
programmer from writing some low level code and making the compiler take
care of that instead. But indeed, if the programmer isn't knowledgeable
enough, certain implicit things may appear obscure and unclear.

....
> For an example of this kind of programming see the source code
> of the container library in lcc-win32.

Great. In fact, I've done a lot of similar coding, and it seems to be the
proper way of doing things. Often times, the code needs not only be correct
but also needs to exhibit certain properties, such as multiple
instanciation, reentrancy, etc. Keeping the state (and everything that may
change) in the dedicated structure makes things easier. The only
global/static info hence should be:
- code itself
- constants
- a few variables that are initialized before the module is used and from
there on remain unchanged until the use is finished and some cleanup is due

Using pointers for the code, not just the data, (I mean the function
pointers) can make the code more flexible and general. This even helped me
to bugfix/patch some ROMed code at work. If the code was using direct calls,
the only option to change the behavior would be to drop the old code and use
the new. But with the function pointers, the cost of the fix was lowered. It
saved lots of memory (something you don't normally think of on a PC :) and
the embedded device cost.

Alex



.



Relevant Pages

  • Re: Pointer of Reference ?
    ... the problem is that pnly "traditionally trained C programmers" as a class see pointers ... If the compiler has to dereference a pointer to achieve that goal, ... ASSERT wants a Boolean value, ... the programmer is forced to actually READ THE DOCUMENTATION OR THE FUNCTION DEFINITION, ...
    (microsoft.public.vc.mfc)
  • Re: decrement past beginning is valid?
    ... >> What I meant by legal is that a compiler will compile it. ... arrays are passed into functions as pointers and that you can use ... When I went to school, ... any programmer worth his/her salt could learn any language in 3 days. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Smart Pointers: Is there something similar to smart pointers in C?
    ... model helps in some ways to understand pointers), ... comment does not imply that you need to understand a machine model ... C compiler, even when given the additional advantage of using an existing C ... "Can a C programmer write a good C program without understanding ...
    (comp.lang.c)
  • Re: Learning to use PICS
    ... >>doesn't generate a ton of code to build and pass the pointers and then ... >>compiler trap silly errors and generate better code. ... > Assembly language programmers do that in 3 instructions. ... Then yes the assembler programmer could reduce it to 3 machine code ...
    (sci.electronics.basics)
  • Re: Whats all this fuss about Object-Oriented languages, please?
    ... Alexander Damyanovich wrote: ... Ideally no one would give you any pointers! ... practice of OOP. ...
    (comp.programming)