|
-
October 28th, 2009, 02:19 PM
#1
Problem with delete operator
Hello,
I have a source code like this:
void func()
{
char* str;
str=new char[5*sizeof(char)];
.
.
.
delete line;
}
When the program get to the "delete line", It shows the following error:
Debug assertion failed!
expression: _BLOCK_TYPE_IS_VALID(phead->nblockuse)
Why does I get the error message?
As I understand, I can use the delete operator for every varabile that was allocated with the "new" operator...
Thnk you,
-
October 28th, 2009, 02:21 PM
#2
Re: Problem with delete operator
Match new with delete and new[] with delete[].
On the other hand, if you are going to create an array of exactly 5 chars and destroy it at the end of the function, you might as well write:
Code:
char str[5];
// ...
-
October 28th, 2009, 03:48 PM
#3
Re: Problem with delete operator
If you want an array with 5 chars, you should use:
Code:
char a[5]; // allocated on stack, no need delete
or
Code:
char *a = new char[5]; // allocated on heap
delete a;
-
October 28th, 2009, 03:51 PM
#4
Re: Problem with delete operator
 Originally Posted by felixb
Code:
char* str;
str=new char[5*sizeof(char)];
.
.
.
delete line;
Why does I get the error message?
Because you new "str" and delete "line"?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
October 28th, 2009, 04:29 PM
#5
Re: Problem with delete operator
 Originally Posted by Emerald214
If you want an array with 5 chars, you should use:
Code:
char *a = new char[5]; // allocated on heap
delete[] a;
see laserlight's post
-
October 28th, 2009, 04:54 PM
#6
Re: Problem with delete operator
it's just an example, I haven't posted the full source.
In the full source, it isn't "char[5*sizeof(...)" but it is: "char[i*sizeof(...)]", so I do need dynamic allocation here.
I changed the source to this:
void func()
{
char* str;
str=new char[5*sizeof(char)];
.
.
.
delete[] str;
}
But I still receive the assertion error.
-
October 28th, 2009, 05:04 PM
#7
Re: Problem with delete operator
then, as the usual requirement, post the minimal complete code to reproduce the problem.
-
October 28th, 2009, 05:34 PM
#8
Re: Problem with delete operator
 Originally Posted by felixb
it's just an example, I haven't posted the full source.
In the full source, it isn't "char[5*sizeof(...)" but it is: "char[i*sizeof(...)]", so I do need dynamic allocation here.
Is there some reason why a std::string or std::vector<char> won't work?
-
October 29th, 2009, 04:22 AM
#9
Re: Problem with delete operator
Don't use "new" with "sizeof". new is not malloc. If you need 5 new objects, then new will create 5 of these objects, regardless of their size.
for example:
Code:
int* integers = new int[5*sizeof(int)];
Most likely, you will actually create 20 ints.
-
October 29th, 2009, 10:03 AM
#10
Re: Problem with delete operator
this is the full code:
char** conv(int* size,char** a,char* b[])
{
char* line;
int m=0,l=0;
int curr,next;
line=new char[m*sizeof(char)];
a=new char*[l*sizeof(char)];
for(int i=1;i<*size;i++)
{
while(i<*size && (strcmp(*(&b[i]),"end") != 0))
{
curr=strlen(line);
next=strlen(b[i]);
line=realloc_func(line,curr,curr+next);
strcat(line,b[i]);
i++;
}
line[strlen(line)+1]='\0';
text=realloc_func(a,l);
text[m]= new char[m+1];
strcpy(a[l],line);
l++;
}
*size=l;
delete[] line;
return a;
}
(realloc_func - is function that get some pointer to array and increase it, by creating bigger one and copying the old to the new)
I get the error when I reach the "delete[] line" ...
-
October 29th, 2009, 10:27 AM
#11
Re: Problem with delete operator
from what you posted here I'd say the problem is inside the realloc_func. What exactly does that do (in code)?
-
October 29th, 2009, 10:30 AM
#12
Re: Problem with delete operator
Code:
int m=0,l=0;
int curr,next;
line=new char[m*sizeof(char)];
a=new char*[l*sizeof(char)];
m and l are both zero.
So you aren't creating memory, not even sure what the result of this is
-
October 30th, 2009, 05:46 AM
#13
Re: Problem with delete operator
 Originally Posted by felixb
this is the full code:
You didn't answer a question that was asked of you in a previous post. Why not just use std::string (or std::vector<char>), instead of fooling around with new[], delete[], strcat(), char pointers, etc.?
What exactly are you trying to accomplish with this code? In C++,, you have the std::string class that does everything that your code is trying to do.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|