-
October 6th, 2015, 03:49 PM
#1
Why is there an exception?
Here is the code,
Code:
bool isRotational(char* s1, char* s2)
{
int len = strlen(s1);
char* s = new char[2 * len + 1];
char* p = s1;
char* q = s;
while (*p)
{
*s++ = *p++;
}
p = s1;
while (*p)
{
*s++ = *p++;
}
*s = '\0';
int len2 = strlen(s2);
int j;
for (int i = 0; i<2 * len - len2 + 1; ++i)
{
for (j = 0; j < len2 && q[i + j] == s2[j]; ++j);
if (j == len2)
return true;
}
delete[] s;
return false;
}
I randomly found that the exception is caused by the statement delete[] s. But when I debug this code, the debugger won't point to the statement delete[] s at all. So I am just wondering how'd I find out the exception is caused by that? Secondly why is there an exception in the code that looks innocent? Thanks!
-
October 6th, 2015, 06:40 PM
#2
Re: Why is there an exception?
What is "s" pointing to ? many times you increment the value of s.
-
October 6th, 2015, 07:07 PM
#3
Re: Why is there an exception?
Originally Posted by Philip Nicoletti
What is "s" pointing to ? many times you increment the value of s.
I got it. Thanks. How am I able to debug such error? It seems the debugger won't point to the correct statement when such exception is thrown.
-
October 7th, 2015, 01:19 AM
#4
Re: Why is there an exception?
Originally Posted by LarryChen
How am I able to debug such error? It seems the debugger won't point to the correct statement when such exception is thrown.
Read Stroustrup's answer to the FAQ How do I deal with memory leaks? The executive summary: use a std::string or a container like std::vector instead of doing manual memory management.
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
|