I’ve stared at this so long that it’s all starting to run together.
So I’m getting a valgrind error:
==25468== Invalid write of size 4
==25468== at 0x52CF64D: _IO_vfscanf (vfscanf.c:1857)
==25468== by 0x52D730A: __isoc99_fscanf (isoc99_fscanf.c:35)
==25468== by 0x402DDB: loadMindRAW (gplib.c:172)
==25468== by 0x4047EE: loadAgent (gplib.c:739)
==25468== by 0x4048BD: loadAgentsFromFile (gplib.c:799)
==25468== by 0x4010C3: initRound (gpfight.c:220)
==25468== by 0x400EBE: main (gpfight.c:99)
==25468== Address 0x584388d is 253 bytes inside a block of size 256 alloc'd
==25468== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
And here’s the offender:
void loadMindRAW(FILE* f_file, unsigned char* mind)
{
int i;
for(i=0; i < MIND_SIZE; i++)
{
fscanf(f_file,"%d,",&(mind[i]));
}
}
The incoming unsigned character array was malloced just prior:
(MIND_SIZE is 256)
tmpAgent->mind = malloc(MIND_SIZE*sizeof(unsigned char));
loadMindRAW(f_file, tmpAgent->mind);
Am I pointing to my mind’s addresses correctly? &(mind[i]) seemed a little kludgy. Raw pointer manipulation, mind+i has the same behavior. Am I missing something simple here?
You are pointing to the address correctly, but you may not pass a pointer to
unsigned chartofscanfin a position expecting anintpointer. This is becausefscanfsees%dspecifier, it assumes that the corresponding position in the variadic argument list is a pointer to signedint; because of the way the varargs are implemented in C and C++,fscanfhas no other way of going about it.Here is how you should rewrite your loop: