I'm trying to allocate an array 64 bytes in size and then loop over the array indexes to read a byte each from the inputfile. but when I don't malloc()
the array indexes, the loop stays in index0 (so each time it loops, it replaces the content in index0 with the next byte, instead of putting each byte in the next array index and keeping them all chronological).
When I use malloc()
it uses the array indexes properly, but it's an infinite loop and uses gigs of ram.
Here's my code:
struct BitIO {
FILE *FilePointer;
uint8_t *Buffer[64];
uint64_t BitsAvailable;
uint64_t BitsUnavailable;
} BitIO;
void Init_BitIO(const char *FileName, const char *Mode) {
BitIO.FilePointer = fopen(FileName, Mode);
malloc(sizeof(BitIO));
while (!feof(BitIO.FilePointer)) {
size_t BytesRead = 0;
for (int i = 0; i < 64; i++) {
BitIO.Buffer[i] = (uint8_t*)malloc(1);
BytesRead = fread(BitIO.Buffer[i], 1, 1, BitIO.FilePointer);
}
}
}
If you're "trying to allocate an array 64 bytes in size", you may consider
instead of
(the latter is an array of 64 pointers to byte)
After doing this, you will have no need in malloc as your structure with a 64 bytes array inside is allocated statically. The 'main' loop will look like
But, of course, I would advise a more efficient form: