2

Whenever I try to run my code I get these error lines. It also seems like that sometimes it crashes the program. Is that possible or something else causes the crash? The error code comes to these lines:

Buffer<char*> s(20,"s.txt","rw");    //20 long char*
   for(unsigned int i=0;i<24;i++){   //it overwrites the size of s after default 20
    s[i]="hey";         //error here: "deprecated conversion from string constant to 'char*'"   

The class looks like this(deleted the unimportant parts":

template<class T>
class Buffer:public File_ptr
{

    unsigned int siz;
    T *data;


public:

    ///constructor(size,filename,openmode)
    Buffer(unsigned int s,const char* n, const char* m):File_ptr(n,m),siz(s)
    {
        data= new T[siz];
       /* for(unsigned int i=0; i<siz; ++i)
        {
            data[i]=0;
        }; */
    };

    ///destructor
    ~Buffer()
    {
        delete[] data;
    }

    ///operator[]
    T& operator[](unsigned int i)
    {
        if(i>siz-1)
        {
            unsigned int newsize=siz*2;
            T* tmp=new T[newsize];
            for(unsigned int j = 0; j < siz; j++)
            {
                tmp[j] = data[j];
            }
            siz=newsize;
            delete[] data;
            data=tmp;
        };
        return data[i];

};     

Any idea why I get the error? Thanks in advance!

  • Why not simply use `std::vector` as a member of `Buffer`? Then just have `Buffer::operator[]` simply call `vector::resize()` if the buffer is not large enough? – PaulMcKenzie May 18 '16 at 21:18
  • sadly, we are forbidden to use STL in this homework. – Peter Gellert May 18 '16 at 21:21
  • Well, you 1) Can't copy buffers around safely, and 2) You can't call `operator []` in a function that uses a `const` Buffer. I leave it for your teacher (or yourself) to address those issues. Also, not to even use `std::string` in this day and age of C++ is simply outrageous. – PaulMcKenzie May 18 '16 at 21:24

2 Answers2

1

You're attempting to store a string literal in a char* variable - indicating that it is writable.

A string literal will typically be stored in the read only data segment of your application binary (See this question)

From the C++ FAQ

...it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified

Attempting to write to a memory location in the read-only data segment will cause your program to terminate - hence the crash.

Community
  • 1
  • 1
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
1

"hey" is a string literal and so the compiler stores it in a different part of the executable which is likely to be stored in write-protected memory along with all other strings. As a result, in C++, in a pointer context it evaluates to const char*.

The solution is to declare

Buffer<const char*> s(20,"s.txt","rw"); 


template<class T>
class Buffer
{

    unsigned int siz;
    T *data;


public:

    ///constructor(size,filename,openmode)
    Buffer(unsigned int s,const char* n, const char* m):siz(s)
    {
        data= new T[siz];
    }

    ///destructor
    ~Buffer()
    {
        delete[] data;
    }

    ///operator[]
    T& operator[](unsigned int i)
    {
        if(i>siz-1)
        {
            unsigned int newsize=siz*2;
            T* tmp=new T[newsize];
            for(unsigned int j = 0; j < siz; j++)
            {
                tmp[j] = data[j];
            }
            siz=newsize;
            delete[] data;
            data=tmp;
        }
        return data[i];
    }
};     

int main() {
    Buffer<char*> s(20,"s.txt","rw");    //20 long char*
    for(unsigned int i=0;i<24;i++){   //it overwrites the size of s after default 20
        s[i]="hey";
    }
}

Live demo: http://ideone.com/Dgdcui

kfsone
  • 23,617
  • 2
  • 42
  • 74