C/C++函数调用栈的实现方法
- 更新时间:2022-06-22 06:29:08
- 编辑:景冰蝶
本文实例讲述了C/C++函数调用栈的实现方法。可用于实现简单的脚本解释器。分享给大家供大家参考。具体实现方法如下:
头文件声明部分:
#pragma once const 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; } };
实现部分:
#include "stdafx.h" #include "TStack.h" #include "new.h" void TStack::push( void* D, size_t bytecount ) { // if memory is not enough // if run under multithread envionment, // a lock or critical section should be added if (pos + bytecount > size) { size_t oldsize = size; size *= growfactor; char *newbuffer = new char[size]; memcpy(newbuffer, buffer, oldsize); delete buffer; buffer = newbuffer; } memcpy(buffer+pos, D, bytecount); pos += bytecount; } void* TStack::pop( size_t bytecount ) { // need synchronization for multithread environment pos -= bytecount; return &buffer[pos]; } TStack::TStack( size_t _size , size_t _pos ) :size(_size), pos(_pos), buffer(new char[size]) { } TStack::TStack( const TStack &O ) :size(O.size), pos(O.pos) { buffer = new char[size]; if (buffer != NULL) { memcpy(buffer, O.buffer, size); } } TStack& TStack::operator=( const TStack& O ) { if (this == &O) return *this; this->size = O.size; this->pos = O.pos; if (buffer != NULL) { delete buffer; } buffer = new char[this->size]; if (buffer != NULL) { memcpy(buffer, O.buffer, this->size); } return *this; }
希望本文所述对大家的C++程序设计有所帮助。
相关教程
-
C++树之遍历二叉树实例详解
这篇文章主要给大家介绍了关于C++树之遍历二叉树的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
发布时间:2022-04-11
-
用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)
今天小编就为大家分享一篇关于用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测),小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
发布时间:2022-04-21
-
c++11&14-多线程要点汇总
这篇文章主要介绍了c++11&14-多线程的使用方法,文中代码非常详细,方便大家更好的参考和学习,感兴趣的朋友快来了解下
发布时间:2022-04-08
-
老生常谈C++中实参形参的传递问题
下面小编就为大家带来一篇老生常谈C++中实参形参的传递问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2022-04-15
-
c++读取数据文件到数组的实例
今天小编就为大家分享一篇c++读取数据文件到数组的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
发布时间:2021-05-11
-
C++中的类与对象深度解析
给网友们整理关于C++的教程,这篇文章主要为大家详细介绍了C++中的类与对象,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2022-08-17
-
C++标准库中sstream与strstream的区别点总结
以下是对C++标准库中sstream与strstream的区别进行了详细的分析介绍,需要的朋友可以过来参考下
发布时间:2021-06-08
-
如何理解C++ 临时变量的常量性
这篇文章主要介绍了如何理解C++ 临时变量的常量性,帮助大家更好的理解和学习c++ 变量,感兴趣的朋友可以了解下
发布时间:2022-04-02