Singleton类,用于将堆移动到堆栈 [英] Singleton class for moving heap to stack

查看:74
本文介绍了Singleton类,用于将堆移动到堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些将堆分配的东西移到堆栈的类(希望:)).该类是单例的,因为仅此类应负责保存和管理堆栈的一部分.我的问题是:我的代码正确吗?就编程而言,代码是正确的(无编译错误,无内存错误和泄漏(由valgrind检查)).但是代码是否真的将堆移到了堆栈上?这是代码:

I have written some class that moves heap allocated stuff to stack (hopefully :) ). This calss is singleton because only this class should be responsible for holding and managing part of stack. My question is: Is my code correct? Code is correct in the sense of programming (no compile errors, no memory errors and leaks (checked by valgrind)). But does the code really moves heap to stack? Here's the code:

stack.hpp:

stack.hpp:

class CStack{
public:
  void* getAlloc(long);
  static CStack* Instance();

private:
  static bool _data[5*sizeof(double)];
  static CStack* m_pInstance;

  CStack(){};
  CStack(const CStack&);
  CStack& operator=(const CStack&);
};

stack.cpp:

stack.cpp:

#include <iostream>
#include "stack.hpp"

CStack* CStack::m_pInstance = 0;

bool CStack::_data[ 5*sizeof(double) ] = { 1 };

CStack* CStack::Instance(){
  if (!m_pInstance)
    m_pInstance = new CStack;
  return m_pInstance;
}

void* CStack::getAlloc(long size){
  std::cout << "  CStack::getAlloc, " << _data << std::endl;
  _pos+=size;
  return &_data[0];
}

store.hpp

store.hpp

class CStore{
public:
  CStore();
  double* myAddr();
  void toStack();
  void out();
  ~CStore();
private:
  double *_data;
  bool _stack;
};

store.cpp:

store.cpp:

#include <iostream>
#include <cstring>
#include "store.hpp"
#include "stack.hpp"

CStore::CStore(){
  _data = new double[4];
  _data[0] = 0.1;
  _data[1] = 1.1;
  _data[2] = 2.1;
  _data[3] = 3.1;
  _stack = 0;
}

double* CStore::myAddr(){ return _data; }

void CStore::toStack(){
  double *tmp;

  tmp = (double*)CStack::Instance() -> getAlloc(4*sizeof(double));

  memcpy(tmp, _data, 4*sizeof(double));
  delete [] _data;
  _data = tmp;
  _stack = 1;
}

CStore::~CStore(){
  if (!_stack)
    delete [] _data;
}

void CStore::out(){
  std::cout << _data[0] << " " << _data[1] << " " << _data[2] << " " << _data[3] << std::endl;
}

main.cpp:

#include <iostream>

#include "stack.hpp"
#include "store.hpp"

using namespace std;

int main(){
  CStack::Instance();
  CStore a;
  double stack;

  cout << &stack << endl;
  cout << "Adresa a " << a.myAddr() << endl;
  a.out();

  a.toStack();
  cout << "Adresa a " << a.myAddr() << endl;
  a.out();

  return 0;
}

推荐答案

不,这完全是胡说八道.

No it's utter nonsense.

首先,这些代码甚至都没有使用堆栈(只要我们说的是执行堆栈),并且您显然会误解了Singleton设计模式.

First of all this code doesn't even use the stack (as long as we are speaking of the execution stack), and you clearly misunderstood the Singleton desing pattern.

当您的程序中只需要单个实例,但又想授予对该实例所有功能的访问权限时,可以使用Singleton.它就像一个全局变量,但是您可以限制访问.

You use Singleton, when you only need a single instance in your program, but you want to grant access to all functions to that single instance. Its like a global variable, but you can restrict the access.

第二,如果您需要在C ++中使用强制转换,例如:

Second, if you need to use casting in C++ like:

tmp = (double*)CStack::Instance() -> getAlloc(4*sizeof(double));

您显然错了.现在不要在C ++中使用void*.当您变得更好时,您可以稍后使用它,而不是现在.

You've clearly gone wrong. Don't use void* in C++ for now. You can use it later, when you've gotten better, not for now.

主要问题为什么不将变量移至堆栈?如果您只是想动态分配,为什么还要将其限制在范围内,请使用智能指针,例如std :: auto_ptr.

The main question WHY do you wan't to move variables to the stack? if you simply want to allocate dynamicly, why still limiting it to the scope, use smart pointers, like std::auto_ptr.

如果要创建堆栈,并使用该堆栈存储双打,则可以编写:

if you want to create a stack, and use that to store your doubles you can write:

#include <vector>

int foo()
{
  std::vector<double> stack;

  // push values on the stack
  stack.push_back(1.1);
  stack.push_back(1.2);
  stack.push_back(1.3);

  // remove values from the stack
  stack.pop_back();
  stack.pop_back();
  stack.pop_back();
}

这篇关于Singleton类,用于将堆移动到堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆