C/C++函数调用栈的实现方法
本文实例讲述了C/C++函数调用栈的实现方法。可用于实现简单的脚本解释器。分享给大家供大家参考。具体实现方法如下:
头文件声明部分:
#pragma onceconst int BUFFERSIZE = 1024;
const int growfactor = 2;
// this stack is used as call stack.
class TStack{
private:
size_t size; // the stack length
size_t pos; // the stack top position
char *buffer; // the buffer
private:
void push(void* D, size_t bytecount); // the implementation of push
void* pop(size_t bytecount); // the implementation of pop
public:
TStack(size_t _size = BUFFERSIZE, size_t _pos = 0); // initialize
TStack(const TStack& o); // copy
TStack& operator=(const TStack& o); // assignment
void pushInt(int i) { push(&i, sizeof(int)); } // push an int
void pushLong(long l) { push(&l, sizeof(long)); } // push a long
void pushfloat(double f) { push(&f, sizeof(f));} // push a double
void pushPointer(void* p){ push(p, sizeof(p)); }
// int
int popInt() { return *(int *)pop(sizeof(int));} // pop an int
long popLong() { return *(long *)pop(sizeof(long)); } // pop an int
double* popfloat() { return (double*)pop(sizeof(double)); } // pop a double
void* popPointer() { return pop(sizeof(void*)) ; }
void clear() { pos = 0; }
};
以上是 C/C++函数调用栈的实现方法 的全部内容, 来源链接: utcz.com/z/315534.html