C access violation error, but not in debug mode


I have this code:

MountedJob* new_MountedJob(Job** job){
    MountedJob* new = malloc(sizeof(MountedJob*));
    printf("ok ");
    new->job = *job;
    printf("not ok");
    new->neededTools = new->job->toolSet;
    new->baseInstance = new->job->baseInstance;
    new->cj = new->baseInstance->C - hashset_size(new->neededTools);
    return new;

It is executed 10 times, each time for a Job passed as an argument. They come from a iterated list and are generated exactly the same way. At the 3rd iteration, new->job = *job; crashes with access violation error code (0xc0000005). The problem is that it works just fine in debug mode so I have no clue what the problem could be. Especially that it works for the 2 first iterations, that's unconsistent I really don't understand.

Thank you.

asked on Stack Overflow Oct 26, 2018 by Henri.D • edited Jul 5, 2020 by Martijn Pieters

2 Answers


You are not allocating the proper size:

MountedJob* new = malloc(sizeof(MountedJob));

Otherwise, you only allocated one pointer, instead of a whole structure.

answered on Stack Overflow Oct 26, 2018 by Matthieu Brucher

You are allocated memory of sizeof(MountedJob*) ie: a pointer size NOT the size of the structure, so then when accessing it, it is likely overwriting places it should not be.

It should be

MountedJob* new = malloc(sizeof(MountedJob));
answered on Stack Overflow Oct 26, 2018 by lostbard

User contributions licensed under CC BY-SA 3.0