Thread: OO in C

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Cool OO in C

    Hello,


    I am looking for general advices/resources on how to write C code that closely resembles good object oriented style. I have just started to read book Object Orientated Programming in ANSI-C by Axel-Tobias Schreiner but after quickly skimming the book I have an impression that it is not well written. There are several reasons why I think so but here I decided to focus on just one, please take a look at the code snippet taken directly from the book:


    Code:
    void * new (const void * type, ...){
    int * p; /* & heap[1..] */
    for (p = heap + 1; p < heap + MANY; ++ p)
    if (! * p)
    break;
    assert(p < heap + MANY);
    * p = MANY;
    return p;
    }

    This function takes an argument which is never used. Author doesn't use braces to organize the code, so as a result code is hard to read. Returns pointer to localy created variable o.0 I cannot believe what I see but as you can see it is true!


    Could somebody say that this book is good/recommended? I would say no - it is badly written and teaches really bad practices.


    And going back to the main subject which is OO in C - what would you recommend?

  2. #2
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Anybody?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    What book format do you have?

    I have the pdf version of what I believe is the same work, and the code examples are very nicely formatted.

    Also, the code example you provided is missing some context; this is from the pdf file:
    Code:
    #if ! defined MANY || MANY < 1
    #define MANY 10
    #endif
    static int heap [MANY];
    void * new (const void * type, ...)
    { int * p; /* & heap[1..] */
    for (p = heap + 1; p < heap + MANY; ++ p)
    if (! * p)
    break;
    assert(p < heap + MANY);
    * p = MANY;
    return p;
    }
    The code has lost it's formatting in copying it from the pdf file, but you can see that the pointer p is not pointing to a local variable.

    -

  4. #4
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    megafiddle, thanks for reply. By bad formatting I meant the fact that the author doesn't use curly braces - even in code snippet you have posted, no curly braces in case of for loop, if condition also lack braces. Therefore code is harder to read, bad practice. You are right that his code doesn't retun local variable but it doesn't change the fact that he passes argument and doesn't use it. So to summarize, I don't consider this book as a good one. Not to mention his examples are awkward...

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MartinR View Post
    By bad formatting I meant the fact that the author doesn't use curly braces
    One way to get around this for small amounts of code like this is to type it out yourself. That way you will unconciously reformat it to your liking, add notes as needed, etc. You can easily add braces everywhere you want them, rename variables, etc. Example:

    Code:
    #if !defined MAXHEAP || MAXHEAP < 1
    #define MAXHEAP 10
    #endif
    
    static int heap[MAXHEAP];
    
    void *new_(const void *type, ...) // I'm calling it new_ to make this easier
                                      // to use with C++ compilers.
    {
        for (int *p = heap+1; p<heap+MAXHEAP; ++p) {
            if (*p == NULL) {
                break;
            }
        }
        assert(p < heap+MAXHEAP);
        *p = MAXHEAP; /* Before an object is added to a set, we let it contain the
                         impossible index value MAXHEAP so that new() cannot find
                         it again. (See p.5) */
        return p;
    }
    If you prefer you can also use automatic formatter programs like GNU indent to do much of the work for you.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by MartinR View Post
    megafiddle, thanks for reply. By bad formatting I meant the fact that the author doesn't use curly braces - even in code snippet you have posted, no curly braces in case of for loop, if condition also lack braces. Therefore code is harder to read, bad practice. You are right that his code doesn't retun local variable but it doesn't change the fact that he passes argument and doesn't use it. So to summarize, I don't consider this book as a good one. Not to mention his examples are awkward...
    Ok, sorry. With the indentation present, I found it easy to follow, probably because I generally don't use braces except where necessary.

    Anyway, This may explain the lack of use of the argument type:

    "Just like Set.h this file is protected by a preprocessor symbol NEW_H. The text only
    shows the interesting parts of each new file, the source diskette contains the complete
    code of all examples.


    The full source code may be available somewhere online. I just discovered I need to look for it myself.

    -
    Last edited by megafiddle; 09-20-2016 at 01:36 PM.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I understand the concepts here, but if really want to learn OOP why not learn
    C++ "on the side"? More tools in the language tool kit is never a bad thing, Even
    if you never use C++ professionally, you have an understanding of how deep
    and complex OOP can be.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Tags for this Thread