|
-
November 28th, 2003, 04:59 PM
#1
Delete fails when char * is used with strcat()
Hi,
This will probably sound like a really n00b question, but here goes... I allocate a char * with new[]. i can then delete it with delete[]. However, if between the new and delete the char * is used with string functions like strcat(), i get an access violation error. I cant find any information on this, can anyone help me??
thanks.
-
November 28th, 2003, 06:19 PM
#2
Re: Delete fails when char * is used with strcat()
Originally posted by mankeyrabbit
Hi,
This will probably sound like a really n00b question, but here goes... I allocate a char * with new[]. i can then delete it with delete[]. However, if between the new and delete the char * is used with string functions like strcat(), i get an access violation error. I cant find any information on this, can anyone help me??
thanks.
post the code between new and delete...
-
November 29th, 2003, 03:09 AM
#3
Sounds like only one guy who is using 2 different IP addresses at the same time. Unbelievable !!
//I did, did see a magenta cat
-
November 29th, 2003, 07:52 AM
#4
Heres where it happens
Code:
if(b != 0) {b = e + 1;}
e = findinstring(' ', b, parms);
if(e == -1) e = strlen(parms);
char *f = new char[e-b];
for(int i = 0; i < (e - b); i++)
{
char c = parms[b + i];
strcpy(&f[i], &c);
}
strcpy(&f[e-b], "\0");
recievedCmd->addParam(f);
if(b == 0) {b++;}
// delete [] f; // ERROR HERE
-
November 29th, 2003, 08:17 AM
#5
OK, what exactly are you trying to accomplish (in terms of what are you trying to do -- search for a string, get a substring of another string, etc.)?
The code you posted is more than likely 3 times more difficult then what it could be.
What is "findinstring"? What is "addParam"?
Code:
strcpy(&f[e-b], "\0");
There is no check whatsoever here to see if you are writing to an invalid address. What is the value of "e"? What is the value of "b"? Does "e-b" make sense (i.e., is it a value you expect it to be)?
Also, if I were you, I would drop using char *, strcpy(), et. al., and just use std::string. It is safer, and with no doubt, would simplify your code. Then, you wouldn't worry about new[] and delete[] since you won't be using them.
Regards,
Paul McKenzie
-
November 29th, 2003, 08:17 AM
#6
Originally posted by mankeyrabbit
Heres where it happens
Code:
if(b != 0) {b = e + 1;}
e = findinstring(' ', b, parms);
if(e == -1) e = strlen(parms);
char *f = new char[e-b];
for(int i = 0; i < (e - b); i++)
{
char c = parms[b + i];
strcpy(&f[i], &c);
}
strcpy(&f[e-b], "\0");
recievedCmd->addParam(f);
if(b == 0) {b++;}
// delete [] f; // ERROR HERE
Well since we are missing some variable declerations, I'm just going to tell you how to debug this.
1. you probably should be using strncpy() rather than strcpy() that's most likely why your overwriting the memory location, or it could be when you add the terminating \0.
2. When you allocate memory using new while linked to the debug CRT, the memory will be padded with 4 bytes FD FD FD FD, this is so the debug CRT can detect if you have overwritten memory when you call delete [].
So breakpoint on the for(int i...open the debug->memory window, in the entry box, type *f now look what is in that location, should be something like: (depending on how large of array you allocated with the new)
CD CD CD CD CD CD FD FD FD FD
that would be a size of 6 plus the additional debug CRT nomansland fill FD, CD is the default debug fill for new memory. Now walk through your code, and make sure you don't stomp on any of the FD FD FD FD bytes, if you do, correct your code.
EDIT: btw paul is correct, that your variables don't make sense, and your making this overly complex when you don't need to. And using std::string is far safer and easier on my poor eyes...
Last edited by Mick; November 29th, 2003 at 08:20 AM.
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
|