Writing a generic container in pure C is hard, and
it’s hard for two reasons:
1. The language doesn’t offer any real support for encapsulation or
information hiding. That means that the data structures expose
information about internal representation right there in the interface
file for everyone to see and manipulate. The best we can do is
document that the data structure should be treated as an abstract
data type, and that the client shouldn ’t directly manage the fields.
Instead, he should just rely on the functions provided to manage the
internals for him.
2. C doesn’t allow data types to be passed as parameters. That means
a generic container needs to manually manage memory in terms of
the client element size, not client data type. This translates to a
bunch of malloc, realloc, fr ee, memcpy, and memmove calls
involving void *s. This is the very type of programming that makes
C difficult.
本文深入探讨了使用纯C语言编写通用容器的困难,主要体现在语言缺乏封装支持和数据类型参数传递限制上。通过实例分析,提出了避免直接管理内部字段和手动内存管理的策略。
1877

被折叠的 条评论
为什么被折叠?



