鱼C论坛

 找回密码
 立即注册
查看: 546|回复: 1

allocator写这个的时候出错了但是不明白哪里错了,报错信息很奇怪

[复制链接]
发表于 2024-9-27 19:39:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <stdio.h>
using namespace std;

template<typename T>
class Allocator {
public:
        T* allocate(size_t _size = 10)
        {
                return (T*)malloc(sizeof(T) * _size);
        }
        void freeallocate(void* p)
        {
                free(p);
        }
        void construct(T* p, const T& value)
        {
                new (p) T(value);
        }
        void deleteallocate(T* p)
        {
                p->~T();
        }
};

template<typename T, typename Alloc = Allocator<T>>
class vector {
private:
        T* _first;
        T* _last;
        T* _end;
        Alloc _alloc;

        void expand()
        {
                int _size = _end - _first;
                _size = _size * 2;
                //T* _new_first = new T [_size];
                T* _new_first = _alloc.allocate(_size);
                int _len = _last - _first;
                for (int i = 0; i < _len; i++)
                {
                        //_new_first[i] = _first[i];
                        _alloc.construct(_new_first[i], _first[i]);
                }
                for (T* p = _first; p != _last; ++p) {
                        _alloc.deleteallocate(p);
                }
                _alloc.freeallocate(_first);
                _first = _new_first;
                _last = _first + _len;
                _end = _first + _size;
        }
public:
        vector<T>(int _size = 10)
        {
                //_first = new T [_size];
                _first = _alloc.allocate(_size);
                _last = _first;
                _end = _first + _size;
        }
        vector<T>(const vector<T>& st)
        {
                int _size = st._end - st._first;
                //_first = new T [_size];
                _first = _alloc.allocate(_size);
                int _len = st._last - st._first;
                for (int i = 0; i < _len; i++)
                {
                        //_first[i] = st._first[i];
                        _alloc.construct(_first[i], st._first[i]);
                }
                _last = _first + _len;
                _end = _first + _size;
        }

        vector<T>& operator=(const vector<T>& st)
        {
                if (st == *this)
                        return *this;
                //delete[] _first;
                for (T* p = _first; p != _last; ++p) {
                        _alloc.deleteallocate(p);
                }
                _alloc.freeallocate(_first);
                int _size = st._end - st._first;
                //_first = new T [_size];
                _first = _alloc.allocate(_size);
                int _len = st._last - st._first;
                for (int i = 0; i < _len; i++)
                {
                        //_first[i] = st._first[i];
                        _alloc.construct(_first[i], st._first[i]);
                }
                _last = _first + _len;
                _end = _first + _size;
                return *this;
        }
        ~vector<T>()
        {
                for (T* p = _first; p != _last; ++p) {
                        _alloc.deleteallocate(p);
                }
                _alloc.freeallocate(_first); // 释放内存
                _first = nullptr;
                _last = nullptr;
                _end = nullptr;
        }

        void push(T val)
        {
                if (_end == _last)
                {
                        expand();
                }
                //*_last++ = val;
                _alloc.construct(_last++, val);

        }
        void popback()
        {
                if (_first == _last)
                        return;
                _last--;
                _alloc.deleteallocte(_last);
        }
        T back()
        {
                return *(_last - 1);
        }
        bool empty()
        {
                return _first == _last;
        }
        int size()
        {
                return _last - _first;
        }

};



int main()
{
        vector<int> p;
        for (int i = 0; i < 15; i++)
        {
                p.push(rand() % 100);
        }

        cout << p.size() << endl;
        while (!p.empty())
        {
                cout << p.back() << "  ";
                p.popback();
        }
        cout << endl;

}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-27 19:45:38 | 显示全部楼层
你好,鱼油。你提供的代码有一些小问题导致编译错误或运行时错误。以下是改进的代码以及一些注意事项:

1. 确保包含所需的头文件。
2. 修复模板定义中的语法错误。
3. 修复删除析构函数时的拼写错误。
4. 修复分配和构造对象的代码。
5. 修正比较运算符中的逻辑错误,以及打印输出的部分。

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;

  4. template <typename T>
  5. class Allocator {
  6. public:
  7.     T* allocate(size_t _size = 10)
  8.     {
  9.         return (T*)malloc(sizeof(T) * _size);
  10.     }

  11.     void freeallocate(void* p)
  12.     {
  13.         free(p);
  14.     }

  15.     void construct(T* p, const T& value)
  16.     {
  17.         new (p) T(value);
  18.     }

  19.     void deleteallocate(T* p)
  20.     {
  21.         p->~T();
  22.     }
  23. };

  24. template <typename T, typename Alloc = Allocator<T>>
  25. class vector {
  26. private:
  27.     T* _first;
  28.     T* _last;
  29.     T* _end;
  30.     Alloc _alloc;

  31.     void expand()
  32.     {
  33.         int _size = _end - _first;
  34.         _size = _size * 2;
  35.         T* _new_first = _alloc.allocate(_size);
  36.         int _len = _last - _first;
  37.         for (int i = 0; i < _len; i++)
  38.         {
  39.             _alloc.construct(_new_first + i, _first[i]);
  40.         }
  41.         for (T* p = _first; p != _last; ++p) {
  42.             _alloc.deleteallocate(p);
  43.         }
  44.         _alloc.freeallocate(_first);
  45.         _first = _new_first;
  46.         _last = _new_first + _len;
  47.         _end = _new_first + _size;
  48.     }

  49. public:
  50.     vector(int _size = 10)
  51.     {
  52.         _first = _alloc.allocate(_size);
  53.         _last = _first;
  54.         _end = _first + _size;
  55.     }

  56.     vector(const vector& st)
  57.     {
  58.         int _size = st._end - st._first;
  59.         _first = _alloc.allocate(_size);
  60.         int _len = st._last- st._first;
  61.         for (int i = 0; i < _len; i++)
  62.         {
  63.             _alloc.construct(_first + i, st._first[i]);
  64.         }
  65.         _last = _first + _len;
  66.         _end = _first + _size;
  67.     }

  68.     vector& operator=(const vector& st)
  69.     {
  70.         if (this == &st)
  71.             return *this;

  72.         for (T* p = _first; p != _last; ++p) {
  73.             _alloc.deleteallocate(p);
  74.         }
  75.         _alloc.freeallocate(_first);

  76.         int _size = st._end - st._first;
  77.         _first = _alloc.allocate(_size);
  78.         int _len = st._last - st._first;
  79.         for (int i = 0; i < _len; i++)
  80.         {
  81.             _alloc.construct(_first + i, st._first[i]);
  82.         }
  83.         _last = _first + _len;
  84.         _end = _first + _size;
  85.         return *this;
  86.     }

  87.     ~vector()
  88.     {
  89.         for (T* p = _first; p != _last; ++p) {
  90.             _alloc.deleteallocate(p);
  91.         }
  92.         _alloc.freeallocate(_first);
  93.     }

  94.     void push(T val)
  95.     {
  96.         if (_end == _last)
  97.         {
  98.             expand();
  99.         }
  100.         _alloc.construct(_last++, val);
  101.     }

  102.     void popback()
  103.     {
  104.         if (_first == _last)
  105.             return;
  106.         _alloc.deleteallocate(--_last);
  107.     }

  108.     T back()
  109.     {
  110.         return *(_last - 1);
  111.     }

  112.     bool empty()
  113.     {
  114.         return _first == _last;
  115.     }

  116.     int size()
  117.     {
  118.         return _last - _first;
  119.     }
  120. };

  121. int main()
  122. {
  123.     vector<int> p;
  124.     for (int i = 0; i < 15; i++)
  125.     {
  126.         p.push(rand() % 100);
  127.     }

  128.     cout << "Vector contains: ";
  129.     while (!p.empty())
  130.     {
  131.         cout << p.back() << " ";
  132.         p.popback();
  133.     }
  134.     cout << endl;
  135.     return 0;
  136. }
复制代码


几个要点说明:
1. 使用 `template <typename T>` 来定义模板。
2. 确认使用 `new (p) T(value)` 来调用构造函数。
3. 注意将 `deleteallocte` 拼写改为 `deleteallocate`。
4. 在拷贝构造函数和赋值运算符中,正确地构造新的元素。
5. 在 `expand` 方法中,正确地将 `_first` 的元素迁移到新分配的内存。

希望这些改动可以解决你的问题!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 00:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表