Program receive a seg fault while freeing the allocated memory

0

I have a structure:

struct Record {
        uint32_t  uunused; 
        uint8_t*  data;
        uint16_t  nRecords;
        uint32_t  val;
    };

Code goes like this: It crashes (seg fault) while freeing the memory. looks like heap is corrupted. but unable to find out the reason. Someone please help it's urgent.

Record *record;
        record = (Record *)calloc(1, sizeof(Record)+8)
        
        record->data = (uint8_t *)(record+1);
        record->nRecords = 8;
        record->val = 0xFFFFFFFF;
        uint8_t *var[8];
        for(i =0; i< 8; i++)
        {
            var[i] = &record->data[i]);
        }
        for(i =0; i< 8; i++)
        {
            *var[i] = *var[i] + 20;
        }
        
        free(record);
c++
pointers
memory
segmentation-fault
asked on Stack Overflow May 12, 2021 by Rajeev Kumar • edited May 12, 2021 by MikeCAT

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0