J
Jeff0803
Guest
I made a function which change characters in the specific position.
However, an error occur when delete allocated memory.
Can anybody fix this?
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
void ChangeCharInString(char* orgstring, int startposition, int endposition);
int main(int argc, char* argv[])
{
string s = "4263471904271655";
char* c_str = (char *)s.c_str();
ChangeCharInString(c_str, 12, 15);
cout << s << endl;
return 0;
}
void ChangeCharInString(char* orgstring, int startposition, int endposition)
{
int slen = strlen(orgstring);
char* old_str = NULL;
old_str = new char[slen];
memset(old_str, 0x00, slen);
strcpy(old_str, orgstring);
try
{
for(int i = startposition; i <= endposition; i++)
{
*(old_str + i) = 0x58;//'X'
}
}
catch(...)
{
}
strcpy(orgstring, old_str);
delete [] old_str; // <=== Error occur!
}
Continue reading...
However, an error occur when delete allocated memory.
Can anybody fix this?
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
void ChangeCharInString(char* orgstring, int startposition, int endposition);
int main(int argc, char* argv[])
{
string s = "4263471904271655";
char* c_str = (char *)s.c_str();
ChangeCharInString(c_str, 12, 15);
cout << s << endl;
return 0;
}
void ChangeCharInString(char* orgstring, int startposition, int endposition)
{
int slen = strlen(orgstring);
char* old_str = NULL;
old_str = new char[slen];
memset(old_str, 0x00, slen);
strcpy(old_str, orgstring);
try
{
for(int i = startposition; i <= endposition; i++)
{
*(old_str + i) = 0x58;//'X'
}
}
catch(...)
{
}
strcpy(orgstring, old_str);
delete [] old_str; // <=== Error occur!
}
Continue reading...