c++ - 数组字符串转换时出现段错误

标签 c++ segmentation-fault

作为 Accelerated C++ 问题 12.1 的一部分,我正在实现字符串类。主要是这个构造函数:

Str(const char* cp) {
    std::copy(cp, cp+std::strlen(cp), std::back_inserter(*this));
}

back_inserter 调用 push_back 时,这似乎会导致问题:

void Str::push_back( const char& c){
    if ((data + length) == limit){
        grow();
    }
    unchecked_append(c);
}

这似乎会在调用 grow() 时但在执行 grow() 的任何主体之前导致问题。

void Str::grow()
{
    // when growing, allocate twice as much space as currently in use
    size_type new_size = std::max(2 * (limit - data), ptrdiff_t(1));
    // allocate new space and copy existing elements to the new space
    iterator new_data = alloc.allocate(new_size);
    iterator new_avail = std::uninitialized_copy(data, (data+length), new_data);

    // return the old space
    uncreate();
    // reset pointers to point to the newly allocated space
    data = new_data;
    limit = data + new_size;
}

具体来说,它会导致 Windows 提示“Str.exe 已停止工作”,并且导致 Ubuntu 仅报告段错误。

这是我的完整代码:

#ifndef _GUARD_STR_H
#define _GUARD_STR_H

#include <ctype.h>
#include <memory>
#include <iterator>
#include <iostream>
#include <cstddef>
#include <cstring>

class Str {
    friend std::istream& operator>>(std::istream&, Str&);
    public:
        typedef size_t size_type;
        typedef char * iterator;
        typedef const char * const_iterator;
        typedef char& reference;
        typedef const char& const_reference;
        typedef char value_type;

        Str& operator+=(const Str& s){
            std::copy(s.begin(), s.end(),
            std::back_inserter(*this));
            return *this;
        }

        // default constructor; create an empty Str
        Str() { create();}

        // create a Str containing n copies of c
        Str(size_type n, char c){ }

        iterator end() { return data + length; }
        iterator begin() { return data; }
        const_iterator end() const { return data + length; }
        const_iterator begin() const { return data; }

        // create a Str from a null-terminated array of char
        Str(const char* cp) {
            std::copy(cp, cp+std::strlen(cp), std::back_inserter(*this));
        }

        template<class In> Str(In i, In j) {
            std::copy(i, j, std::back_inserter(data));
        }

        std::allocator<char> alloc;

        void push_back( const char&);

        char& operator[](size_type i) { return data[i]; }
        const char& operator[](size_type i) const { return data[i]; }
        size_type size() const { return length; }

    private:
        iterator data;
        size_t length;
        iterator limit;
        void create();
        void create(size_type, char);
        void grow();
        void unchecked_append(const char& c);
        void uncreate();
};

void Str::push_back( const char& c){
    if ((data + length) == limit){
        grow();
    }
    unchecked_append(c);
}

void Str::unchecked_append(const char & val)
{
    alloc.construct((data+(length++)), val);
}

void Str::uncreate()
{
    if (data) {
        // destroy (in reverse order) the elements that were constructed
        iterator it = (data + length);
        while (it != data)
            alloc.destroy(--it);

        // return all the space that was allocated
        alloc.deallocate(data, limit - data);
    }

    // reset pointers to indicate that the Vec is empty again
    data = limit = 0;
}

void Str::create(){
    data = limit = 0;
}

void Str::create(size_type n, char c){
    data = alloc.allocate(n);
    std::uninitialized_fill(data, data + n, c);
}

void Str::grow()
{
    // when growing, allocate twice as much space as currently in use
    size_type new_size = std::max(2 * (limit - data), ptrdiff_t(1));
    // allocate new space and copy existing elements to the new space
    iterator new_data = alloc.allocate(new_size);
    iterator new_avail = std::uninitialized_copy(data, (data+length), new_data);

    // return the old space
    uncreate();
    // reset pointers to point to the newly allocated space
    data = new_data;
    limit = data + new_size;
}

std::ostream& operator<<(std::ostream&, const Str&);
Str operator+(const Str&, const Str&);

std::ostream& operator<<(std::ostream& os, const Str& s)
{
    for (Str::size_type i = 0; i != s.size(); ++i)
        os << s[i];
    return os;
}

std::istream& operator>>(std::istream& is, Str& s)
{
    char c;
    while (is.get(c)){
        s.push_back(c);
    }
    return is;
}

Str operator+(const Str& s,  const Str& t)
{
    Str r = s;
    r += t;
    return r;
}

#endif

什么原因导致段错误?

最佳答案

您似乎没有在任何地方初始化length

关于c++ - 数组字符串转换时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13560916/

相关文章:

c++ - 如何通过透明代理实现SSL隧道?

c++ - 并排显示卡片

c++ - 陷阱无法捕获 SIGSEGV

c++ - C++ 中返回 *ptr 和 &x 的区别

c - 赛格夫 : invalig write of size 8 when adding a node to the end of a linked list

c++ - 错误/var/tmp/kdecache 由 uid 1000 而不是 uid 0 拥有

c++ - 如何找到编译瓶颈?

C++ - 检测死套接字

c++ - 来自给定结构的任何类型的 vector

C: Segmentation Fault -- 堆栈实现链表