为什么用纯c写一个通用的容器非常难?

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写一个通用的容器非常难?)