0

I have a problem with loading data from file. This piece of code fills high scores with some random data (its just for debug)

int i = 0; 
for (i = 0; i < MAX_HIGH_SCORE; i++)
{
    high_score_name[i] = al_ustr_new("123456789012345678901");
    high_score[i] = 1000*i;
}

after that i save all of that to file.

ALLEGRO_FILE* high_score_file = al_fopen("hs.txt","wb");
for (i = 0; i < MAX_HIGH_SCORE; i++)
{
    int buffer = al_ustr_size(high_score_name[i]);
    al_fwrite32le(high_score_file, buffer);
    al_fwrite(high_score_file, al_cstr(high_score_name[i]), buffer);
    al_fwrite(high_score_file, &high_score[i], sizeof(high_score[i]));

}
al_fclose(high_score_file);

It work just fine. It have four bytes reserved for size then 21 characters of high_score_name[] followed by high_score[] int which takes also 4 bytes.

Problem starts when i try to load it up. I tried to do in many ways but it won`t compile because line

char* buffer= al_malloc(size); gives error :

a value of type "void *" cannot be used to initialize an entity of type "char *

for (i = 0; i < MAX_HIGH_SCORE; i++)
{
    int size = al_fread32le(high_score_file);
    char* buffer = al_malloc(size);
    al_fread(high_score_file, buffer, size);
    high_score_name[i] = al_ustr_new_from_buffer(buffer, size);

    int *buffer2 = &high_score[i];          
    al_fread(high_score_file, buffer2, sizeof(high_score[i]));

    al_free(buffer);
}

al_fclose(high_score_file);

Im stuck with it for two days and i would really apreciate any help.

Ankur
  • 3,584
  • 1
  • 24
  • 32
bfg9000
  • 5
  • 3
  • Try to add a cast before calling `al_malloc( )` i.e. `char *buffer = (char *) al_malloc(size);`. – shauryachats Jan 18 '15 at 16:40
  • 1
    it helped with the error but now i`m getting 0xC0000005: Access violation reading location 0xFEEEFEF6 when size is initialized. – bfg9000 Jan 18 '15 at 16:44
  • 1
    `0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory` http://stackoverflow.com/a/127404/487892 – drescherjm Jan 18 '15 at 16:48
  • on failure al_fread32le( ) returns -1. You need an if-statement before the al_malloc( ) call to make sure size > 0. – bpmason1 Jan 18 '15 at 16:52
  • i added `if(size>0)` statement after using `al_fread32le();` but im getting Access violation when program executes `int size = al_fread32le(high_score_file);` Value of size is `-858993460` and in a file its 15 00 00 00 (which is 21 in decimal) – bfg9000 Jan 18 '15 at 16:59
  • @bfg9000 Is that the same access violation that you posted above that said you were using a pointer to memory on the heap that was already deallocated? – drescherjm Jan 18 '15 at 17:03
  • @drescherjm `First-chance exception at 0x6829A31A (allegro-5.0.10-monolith-mt-debug.dll) in Bricks.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6.` thats the full error and im getting when initializing`int size` – bfg9000 Jan 18 '15 at 17:05
  • ok code works fine now. Turns out i wasnt opening the file again. – bfg9000 Jan 18 '15 at 17:10
  • You need to figure out why high_score_file was deleted. – drescherjm Jan 18 '15 at 17:10

2 Answers2

0

The output of malloc has type void* which you should try explicitly casting to char* .

char* buffer = static_cast<char *>(al_malloc(size))
bpmason1
  • 1,900
  • 1
  • 12
  • 9
0

If your function internally uses malloc to allocate memory, then malloc returns a void* and if your al_malloc returns void*...Use type cast to conert it to char*.

char* buffer = (char*)al_malloc(size);

This should work.

Function prototype of malloc:

  void *malloc(size_t size);
basav
  • 1,475
  • 12
  • 20