由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 用c怎么实现generic stack (转载)
相关主题
One question about Void pointer为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?
请教函数 INIT 怎么能free memorysizeof()的问题
C 里面有办法永久改变一个指针的属性吗?difference between FILE and struct FILE
请问const myClass &src 和myClass const &src有什么区别?请问可以这样定义struct吗?
一个partial specialization的问题[合集] Interview Question
C++ Template Questionask a question about struct in C programming
self defined data type problemtypedef
how to initialize this struct.typedef struct的问题
相关话题的讨论汇总
话题: stackname话题: stack话题: void话题: datatype话题: lwsosgvd
进入Programming版参与讨论
1 (共1页)
l******d
发帖数: 530
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: lwsOsgvd (lwsOsgvd), 信区: JobHunting
标 题: 用c怎么实现generic stack
发信站: BBS 未名空间站 (Tue Apr 24 16:42:43 2012, 美东)
用设计个stack,要求是The stack should be able to take as input a wide
variety of data types: it could range from byte sized to an n-byte sized
structure。
刚好在PIE书里面看到这个
typedef struct Element {
struct Element *next;
void *data;
} Element;
不知道算不算达到要求。
多谢!
d****n
发帖数: 1637
2
generic C 有两类,一类是 void *, 但是有转换消耗。
另一类是 C marco,但是难debug
对于你的要求,可变的size,当然要用C marco
///file gstack.c
#define _STACK_STRUCT(STACKNAME, DATATYPE) \
typedef struct STACKNAME { \
struct STACKNAME * next;\
DATATYPE value;\
}STACKNAME##_t;
#define __STACK_POP(STACKNAME, DATATYPE ) \
void pop_##STACKNAME( STACKNAME##_t *node ) \
{ \
;\
}
#define __STACK_PUSH(STACKNAME, DATATYPE ) \
void push_##STACKNAME( STACKNAME##_t *node ) \
{ \
;\
}
///define more functions here
#define DEFINE_STACK(STACKNAME, DATATYPE) \
_STACK_STRUCT(STACKNAME, DATATYPE ) \
__STACK_POP(STACKNAME,DATATYPE)\
__STACK_PUSH(STACKNAME,DATATYPE)
///install more function declarations
///example usage
DEFINE_STACK(lwsOsgvd , int)
gcc gstack.c -E
# 1 "gstack.c"
# 1 ""
# 1 ""
# 1 "gstack.c"
# 24 "gstack.c"
typedef struct lwsOsgvd { struct lwsOsgvd * next; int value;}lwsOsgvd_t;
void pop_lwsOsgvd( lwsOsgvd_t *node ) { ; }
void push_lwsOsgvd( lwsOsgvd_t *node ) { ; }
X****r
发帖数: 3557
3
一个栈能不能压入异质的数据类型,是根本不同的要求,没法比较。看原题的意思应该
是要能压入异质的数据类型的,不是像你这样的。
而且这个void *的转换消耗不知道是从何说起啊。

【在 d****n 的大作中提到】
: generic C 有两类,一类是 void *, 但是有转换消耗。
: 另一类是 C marco,但是难debug
: 对于你的要求,可变的size,当然要用C marco
: ///file gstack.c
: #define _STACK_STRUCT(STACKNAME, DATATYPE) \
: typedef struct STACKNAME { \
: struct STACKNAME * next;\
: DATATYPE value;\
: }STACKNAME##_t;
: #define __STACK_POP(STACKNAME, DATATYPE ) \

d****n
发帖数: 1637
4
Void pointer can be flexibly converted into vary types with the cost of type
cast.
Also, the data structure size is not changing, but a two pointers: one is
the necessary next pointer, the other is datum.
here is the what I said "cost" from one of my favorite person's blog.
"We usualy use void* to implement generic containers in C. To do in this way
, we need to pay an overhead on retrieving data pointed by a void* pointer.
We often, but not always, need a function call for operations on void* data,
such as copying, comparing or hashing. This adds additional overhead on
speed. Furthermore, if the size of an object is small, we would rather put
the whole object in the container instead of wasting the memory for an
addition pointer: using void* may also bring memory overhead."
Using void* in Generic C Programming may be Inefficient depends on how heavily type cast called. Instead, C macro is
part of the idea how c++ implement its generic template mechanism.
X****r
发帖数: 3557
5
cast指针本身没有什么额外消耗,除了多打几个字。存指针而不是数据本身的消耗你的
宏实现里不是一样也有吗?

type
way
.
data,

【在 d****n 的大作中提到】
: Void pointer can be flexibly converted into vary types with the cost of type
: cast.
: Also, the data structure size is not changing, but a two pointers: one is
: the necessary next pointer, the other is datum.
: here is the what I said "cost" from one of my favorite person's blog.
: "We usualy use void* to implement generic containers in C. To do in this way
: , we need to pay an overhead on retrieving data pointed by a void* pointer.
: We often, but not always, need a function call for operations on void* data,
: such as copying, comparing or hashing. This adds additional overhead on
: speed. Furthermore, if the size of an object is small, we would rather put

d****n
发帖数: 1637
6
DATATYPE can be anything , pointer or a complex data struct.
And there is not cast need in my Code.
Indeed, Type Cast DOES cost overhead. And if constantly doing that, codes
will become bitterly disgusting for debugging by programmers. For example,
the "undefined member" issues will happen alot when programmer forget the
original definition of the data type need to be converted.
Also, you already conclude yourself that "除了多打几个字". If comparing with
easiness of developing, more typing means more mistakes to occur.
Moreover, Readability decrease in some degree.
I am not showing how extremely I hate the void * approach. Actually,
in some of my project, I do use void * because It is fast to do. If given
more time, I would rather invent
a generic C lib using C macro.
Back to his question, as far as I understand, Using C macro is the way to
success it. unless I misunderstand the specifications ~~

【在 X****r 的大作中提到】
: cast指针本身没有什么额外消耗,除了多打几个字。存指针而不是数据本身的消耗你的
: 宏实现里不是一样也有吗?
:
: type
: way
: .
: data,

X****r
发帖数: 3557
7
I think you misunderstood the question.

with

【在 d****n 的大作中提到】
: DATATYPE can be anything , pointer or a complex data struct.
: And there is not cast need in my Code.
: Indeed, Type Cast DOES cost overhead. And if constantly doing that, codes
: will become bitterly disgusting for debugging by programmers. For example,
: the "undefined member" issues will happen alot when programmer forget the
: original definition of the data type need to be converted.
: Also, you already conclude yourself that "除了多打几个字". If comparing with
: easiness of developing, more typing means more mistakes to occur.
: Moreover, Readability decrease in some degree.
: I am not showing how extremely I hate the void * approach. Actually,

1 (共1页)
进入Programming版参与讨论
相关主题
typedef struct的问题一个partial specialization的问题
一个简单的C编程问题C++ Template Question
有段c++代码看不懂self defined data type problem
请教一个c++ 里functor的问题how to initialize this struct.
One question about Void pointer为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?
请教函数 INIT 怎么能free memorysizeof()的问题
C 里面有办法永久改变一个指针的属性吗?difference between FILE and struct FILE
请问const myClass &src 和myClass const &src有什么区别?请问可以这样定义struct吗?
相关话题的讨论汇总
话题: stackname话题: stack话题: void话题: datatype话题: lwsosgvd