| Deutsch English Français Italiano |
|
<containers-20240803130427@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: is STC a good supplementary library for C?
Date: 3 Aug 2024 12:06:00 GMT
Organization: Stefan Ram
Lines: 72
Expires: 1 Jul 2025 11:59:58 GMT
Message-ID: <containers-20240803130427@ram.dialup.fu-berlin.de>
References: <j4KdnZzYDexmlDP7nZ2dnZfqn_idnZ2d@brightview.co.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 4vpk+sYF9bJSE+7EiYSDHw3WXyKIdjyxQACpdqBj7Oow5l
Cancel-Lock: sha1:WbxObj6s5BFqsQmNkRYb7B3ANZY= sha256:GLj1t22QMP6E88w/64QxWrg/zBo93pOG5lkjTkgzisc=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 4385
Mark Summerfield <mark@qtrac.eu> wrote or quoted:
>I want a generic (int/str/custom struct) set, map, ordered map, vector.
C is a stripped-down language designed to get close to
the hardware with minimal overhead. It skips many of the
high-level features in C++, like templates, classes, and
operator overloading. Trying to make C act like C++ by
building a generic container library can lead to:
Complexity: Creating generic containers in C often means
dealing with gnarly macros, void pointers, and type casting,
which can make the code a pain to read and maintain.
Inefficiency: Generic containers in C might not be as slick
as those in C++ due to the lack of compile-time type checking
and optimizations. And,
error-prone code: Without the type safety provided by C++ templates,
generic containers in C are more likely to cause runtime errors.
Idiomatic C programming rolls with the language's strengths and
works within its constraints. This often involves:
Explicit typing: C programmers usually define data structures
and functions explicitly for each type they need, rather
than leaning on generic solutions.
Manual memory management: C programmers are used to managing
memory by hand, which can be more predictable and efficient than
relying on a generic container library. And,
simplicity and clarity: C code is often straightforward and simple,
focusing on clear and direct solutions rather than abstract
and generalized ones.
The old pros of C came up with several techniques to handle the
lack of generic containers:
Structs and pointers: Using structs and pointers to create
custom data structures tailored to specific needs.
Function pointers: Employing function pointers to pull off a form
of polymorphism. And,
Macros: Utilizing preprocessor macros to create reusable code
snippets, though this can lead to less readable and maintainable code
if overdone.
Some problems encountered when using generic container libraries
in C might be:
portability: Generic container libraries in C can be less portable
across different compilers and platforms.
Debugging: Debugging generic code in C can be more of a
hassle due to the lack of type information and the use of
void pointers. And,
performance: Hand-crafted, type-specific data structures
and algorithms can often be more performant than generic
implementations.
While it's technically possible to whip up a generic container
library in C, doing so often goes against the grain of
idiomatic C programming.
C shines in simplicity, explicitness, and low-level control.
Embracing these traits leads to more maintainable, efficient, and
understandable code. Instead of trying to force C to act like C++,
it's generally better to leverage C's strengths and use techniques
that are well-suited to the language's design and philosophy.