|
-
June 23rd, 2005, 07:27 AM
#1
pointers to chars + sprintf
I have got a real problem getting this function to work and do not really understand why.
Code:
int CopyFiles(const std::string &strFilePath1,
const std::string &strTargetPath,
std::vector<std::string> &vecFilesCopied)
{
char *buffer = new char[strFilePath1.length()];
int StringPos, Test, Counter;
Test = _snprintf(buffer, strFilePath1.length(), strFilePath1.c_str());
// blabla du some fancy stuff not causing any problems
char *cTargetPath = new char[((strFilePath1.length()-StringPos) + strTargetPath.length())];
Test = _snprintf(cTargetPath, strTargetPath.length(), strTargetPath.c_str());
delete [] cTargetPath;
delete [] buffer;
return 0;
}
Now what happens is, that no matter what value strFilePath1.length() returns, strlen(buffer) always is 25.
The other problem I'm suffering from is, that I get a debug error when trying to delete both pointers.
Can anybody help me with this problem? Thank you very much in advance
-
June 23rd, 2005, 07:50 AM
#2
Re: pointers to chars + sprintf
Not sure about your delete[] statements but where are you testing for the string length of buffer? I don't see it anywhere in your code.
If you're testing it after your new char[] operation, the string length of buffer could be just about anything. All you've done is allocate a block of memory that is strFilePath1.length() bytes long. strlen() reads a block of memory until it finds a NULL character, though. So even if strFilePath1.length() was 1, it could be 25 characters until strlen() discovers a NULL character.
This should return the proper string length:
Code:
char *buffer = new char[strFilePath1.length()];
buffer[strFilePath1.length() - 1] = 0;
int iLength = strlen(buffer);
but it would be better to add an extra byte to hold NULL when you create your buffer on the heap:
Code:
char *buffer = new char[strFilePath1.length() + 1];
buffer[strFilePath1.length()] = 0;
int iLength = strlen(buffer);
Note that when you do a new char[], each byte in that block of memory is initialized with some value. If that value happens to be NULL, then your strlen() function would return 0, which is probably what it should be thought of as anyway, since you don't really have a string stored in here yet -- just some unknown byte values termintated with a NULL character.
Last edited by Bond; June 23rd, 2005 at 08:01 AM.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
June 23rd, 2005, 10:24 AM
#3
Re: pointers to chars + sprintf
Perfect, thanks. I think I became a victim to the fence post error once again. I used your second suggestion and all my problems were blasted away.
I'm sorry I didn't clearly indicate that I of course am not testing for size anywhere in my code posted , however during my search for the solution of my problem I checked the length of the string. I removed that test from the code to make it more easy to understand.
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
|