Note that there are some explanatory texts on larger screens.

plurals
  1. POcrash, delete [ ] this->str
    primarykey
    data
    text
    <p>Hello I've got some <strong>troubles to delete a member string</strong> of my class which is isn't null and which points to the right location since <strong>I can display my string "hello world" just before.</strong></p> <p>I call the function mystringclass::alloc() from another member function afterwards this->str was supposed to get another string content larger.</p> <p><strong>The process worked fine a first time when I resized the same way "hello" to get "hello world".</strong> But now I want to enlarge it again it doesn't. So I'm confused.</p> <p>Please help me.</p> <pre><code>void mystringclass::alloc(long newsize) //newsize includes the +1 char { cout &lt;&lt; "old size was: " &lt;&lt; this-&gt;size &lt;&lt; endl; //displays "12" if(this-&gt;str) cout &lt;&lt; this-&gt;str &lt;&lt; endl; //displays "hello world" all is right till here if(this-&gt;str) delete [] this-&gt;str ; //it crashes here cout &lt;&lt; "str deleted\n"; //never show up on screen this-&gt;str = new char[newsize + 1]; this-&gt;size = newsize; this-&gt;str[0] = 0; } </code></pre> <hr> <p>Thanks for your answers, I tried to clear my code to post it here. My bug disappeared but another one came up and having something to do with the rule of three:</p> <pre><code>int main() { stringclass str = "Hello"; stringclass str2 = str; return 0; } </code></pre> <p>I display info all along procedures <strong>So the problem is that str2 is already equal to "hello" even before affecting the content of str in my copy constructor. And is empty after the copy.</strong> What's wrong? As some may say, I learned c++ in a magicbox. I'm using codeblocks 10.05</p> <p><strong>Full code :</strong></p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; class stringclass { protected : inline bool success() { failbit = false; return true; } inline bool fail() { failbit = true; return false; } public : bool failbit; char * mystring; long memsize; long length; void alloc(long newsize); void reset(); void copy(const stringclass &amp; other); stringclass() {reset(); } stringclass(const stringclass &amp; other) {copy(other); } stringclass(const char str[]); ~stringclass() {delete [] mystring;} inline long get_length() { if(mystring) length = strlen(mystring); return length;} inline long get_memsize() const { return memsize; } friend ostream&amp; operator &lt;&lt; (ostream&amp; out, stringclass &amp; sc){out &lt;&lt; sc.mystring; return out;} stringclass &amp; operator = (stringclass &amp; other) { copy(other); return *this;} }; void stringclass::reset() { delete [] mystring; mystring = NULL; length = 0; memsize = 0; } void stringclass::alloc(long newsize) { cout&lt;&lt; "\nalloc(long newsize)... \n" &lt;&lt; endl; cout &lt;&lt; "memsize was : " &lt;&lt; memsize &lt;&lt; endl; cout &lt;&lt; "length was : " &lt;&lt; length &lt;&lt; endl; if(mystring) cout &lt;&lt; "mystring = " &lt;&lt; mystring &lt;&lt; endl; delete [] mystring; cout &lt;&lt; "mystring deleted...\n"; mystring = new char[newsize]; cout &lt;&lt; "mystring has been resized\n"; mystring[0] = 0; memsize = newsize; length = strlen(mystring); cout &lt;&lt; "memsize is now : " &lt;&lt; memsize &lt;&lt; endl; cout &lt;&lt; "length is now : " &lt;&lt; length &lt;&lt; endl; cout&lt;&lt; "\nend of alloc()... " &lt;&lt; endl; cout &lt;&lt; "\n"; } void stringclass::copy(const stringclass &amp; other) { cout &lt;&lt; "\n"; cout &lt;&lt; "copy(const stringclass &amp; other)...\n" &lt;&lt; endl; cout &lt;&lt; "other.mystring = "&lt;&lt; other.mystring &lt;&lt; endl; if(other.mystring == NULL || other.memsize == 0) { reset(); return; } alloc(other.memsize); strcpy(mystring, other.mystring); cout &lt;&lt; "mystring = "&lt;&lt; mystring; length = strlen(mystring); cout &lt;&lt; "length: " &lt;&lt; length &lt;&lt; endl; cout&lt;&lt; "\nend of copy()... " &lt;&lt; endl; cout &lt;&lt; "\n"; } stringclass::stringclass(const char str[]) : mystring(NULL), memsize(0), length(0) { if(str == NULL) reset(); else { alloc(strlen(str) + 1); strcpy(mystring, str); length = strlen(mystring); } } int main() { stringclass str = "Hello"; stringclass str2 = str; cout &lt;&lt; "\nback to main()...\n"; cout &lt;&lt; "str = " &lt;&lt; str &lt;&lt; "\n"; cout &lt;&lt; "str2 = " &lt;&lt; str2 &lt;&lt; "\n"; cout &lt;&lt; endl; system("PAUSE"); return 0; } </code></pre> <p><strong>Result on screen :</strong></p> <pre><code>alloc(long newsize)... memsize was : 0 length was : 0 mystring deleted... mystring has been resized memsize is now : 6 length is now : 0 end of alloc()... copy(const stringclass &amp; other)... other.mystring = Hello alloc(long newsize)... memsize was : 3214960 length was : 2293560 mystring = Hello mystring deleted... mystring has been resized memsize is now : 6 length is now : 0 end of alloc()... mystring = length: 0 end of copy()... back to main()... str = str2 = Appuyez sur une touche pour continuer... </code></pre> <p>I've just realized that the following code isn't necessary for you :</p> <pre><code> protected : inline bool success() { failbit = false; return true; } inline bool fail() { failbit = true; return false; } public : bool failbit; </code></pre> <p>So <strong>I put off these two functions and this variable</strong>, and guess what.. all worked fine, <strong>no bug</strong>. They are not even used once. <strong>I put it back and the problem came back</strong> as well. How could you explain that?! I'm already losing my hairs.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload