C/C++函数调用栈的实现方法

所属分类: 软件编程 / C 语言 阅读数: 81
收藏 0 赞 0 分享

本文实例讲述了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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多