well, I think I might have posted it a while back, but incase I didn't, I wrote this function some time ago:
szRec in this function is passed as a char array (string) without its index (generating the address of the first element). so I have something looking like this:Code:void EncryptRecord(char *szRec, unsigned long nLen, char *szKey) { unsigned long i; char *p; p = szKey; for(i = 0; i < nLen; i++) { if(!(*p)) p = szKey; *szRec ^= *p; *szRec += *p; szRec++; p++; } }
which works fine. now that I have written DecryptRecord:Code:char buf[256]; /* buf gets some data somewhere in here */ EncryptRecord(buf, dwBytesRead, szKey);
which isn't much different, except for the fact that I am passing it a slightly different value for szRec. I am passing it a true char pointer (that is one that is declared as char* with its memory allocated with malloc). so I have something like this:Code:void DecryptRecord(char *szRec, unsigned long nLen, char *szKey) { unsigned long i; char *p; p = szKey; for(i = 0; i < nLen; i++) { if(!(*p)) p = szKey; *szRec -= *szKey; *szRec ^= *szKey; szRec++; szKey++; } }
however when I execute the DecryptRecord, I get what looks like an error caused by me playing in memory that I do not own (one of those Windows errors stating that the application has to be closed). when I comment out the DecryptRecord statement, everything works fine, so it is obviously causing the error (and don't let the different variables dwFileSize and dwBytesRead fool you, I can assure you that they both hold the same value, or execution won't get to DecryptRecord).Code:char *p = malloc(dwFileSize); /* p gets some data somewhere in here */ DecryptRecord(p, dwBytesRead, szKey);
I'm fairly new with pointers, so I don't see what the real difference is here, however there must be one.
can anyone please explain to me why this is happening and how I can fix it?
any help is greatly appreciated, thank you in advance.![]()



LinkBack URL
About LinkBacks




).
. and yes I could pass szKey as a const char*, good idea. I dunno about p though, I don't think that should be a constant, since the way I am incrementing it by pointer. maybe though, its worth a try.