Thread: How people tackle C limitations?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    How people tackle C limitations?

    I'm still learning new things about C many months later and It seems that for every big feature that C doesn't support, there is a way to implement it or find another way to do it (that will give you the same result). So my question is the following. How people that are very experienced and create big and complicated software solve these limitations? I'm talking about stuff like Generics/Templates, OOP, RAII and any other big and "modern" feature. One way would be to use a C++ compiler and use only the features that I need but I would like to avoid that as lately I'm very interested on cproc (cproc: Small C11 compiler based on QBE) which use QBE (QBE - Compiler Backend) as a backend (TLTR: 3+ faster compilation time than GCC/Clang and 80% of the performance. These are rough numbers and based on my experience tho) so I would like to avoid using C++. Any ideas and advises?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Some things you can fake up in C, other things you can't do without effectively writing an interpreter for another language in C. The main C limitation versus C++ that can't be overcome is the lack of destructors. In C you have to free memory you allocate explictly. So you can implement a "closure" in C (a functionpointer and a void pointer), but you can't destroy the data the void pointer points to except from the place you set it up. So in practice C "closures" are quite restricted.

    You can implement OO with structures containing function pointers. That's a C++ "virtual" function. You can also give structures a field giving the structure size. That allows for future expansion without affected existing code. Microsoft do this a lot in the Windows API.

    RAII is easy to do (just have function which allocates the resources and returns a pointer). But the corollary is DIRE (deletion is resorouce expiry), and that you can't do as easily. You have to write a "kill" function and call it explictly.

    You generally don't need templates in C because all the data is what C++ calls "plain old data". Functions like qsort() and memcpy() work fine
    with C, but not with C++ classes.

    There are generics in the latest C standard. However generally treating all your data as double and using ints for counts of thing sin memory or index values works very well.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Quote Originally Posted by Malcolm McLean View Post
    The main C limitation versus C++ that can't be overcome is the lack of destructors.
    RAII is easy to do (just have function which allocates the resources and returns a pointer).
    That sounds like a contradiction to me. RAII is based on destructors.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thmm View Post
    That sounds like a contradiction to me. RAII is based on destructors.
    You need to read the next sentence for the context, i.e., Malcolm McLean is trying to address the criticism that the name "RAII" is misleading by giving it a more limited meaning and thus is able to contrast it with "DIRE" as a corollary, whereas the traditional definition combines both. As you have experienced, the attempt to avoid confusion is itself confusing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    As you have experienced, the attempt to avoid confusion is
    itself confusing.
    @laserlight,
    You are 100% right.
    I don't like the term RAII. A better term might be Automatic Resource Management.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with strategy to tackle a project
    By mc61 in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2008, 10:44 AM
  2. When You're ready to tackle games
    By blankstare77 in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2005, 12:05 PM
  3. complete noobie, confused on how to tackle c++
    By fletchpac in forum C++ Programming
    Replies: 7
    Last Post: 07-04-2004, 07:48 AM
  4. How to tackle a debug assertion failure?
    By juhigarg in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 12:59 PM
  5. IT people - weird genius or simply narrow-minded, antisocial, lazy people
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2001, 05:00 AM

Tags for this Thread