Search code examples
cvisual-studio-2010heap-corruption

C Language - Windows has triggered a breakpoint


I am coding C language in Visual Studio C++.

In the first calling of this function there is no problem. It makes all of process but in the second calling VS give "Windows has triggered a breakpoint" error. However, I compile and run the code in Linux, there is no problem.

void printDataPagePersons(int pageNumber)
{   
    Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
    printf("page of city : %d\n",sizeof(*pageofCity));

    FILE* fp;
    fp=fopen("x.dat", "rb");
    fseek(fp, PAGE_SIZE*pageNumber,SEEK_SET);
    fread(pageofCity, PAGE_SIZE,1, fp);
    fclose(fp);
    //OTHER PRINTING PROCESSES...
}

Solution

  • Here you allocate a buffer:

    Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
    

    This is the size of that buffer:

    sizeof(Person)* RECORD_COUNT
    

    You then read data from a file into that buffer at this line of code:

    fread(pageofCity, PAGE_SIZE,1, fp);
    

    There you are telling the fread function to read a PAGE_SIZE of bytes into that buffer.

    I'm guessing the cause of your problems is this condition is also true:

    PAGE_SIZE > sizeof(Person)* RECORD_COUNT