I have following function:
void deleteInventory( struct book** inventory )
{
struct book* ptr = NULL;
ptr = *inventory;
while( length != 0)
{
free(ptr);
length--;
++ptr;
}
free(inventory);
}
Structure:
#define MAX_LENGTH 64
#define ISBN_LENGTH 11
struct book
{
char isbn[ISBN_LENGTH];
char title[MAX_LENGTH];
char author[MAX_LENGTH];
int year;
float price;
};
Total size of the structure: 147 bytes (11 + 64 + 64 + 4 + 4) sizeof(struct book) returns: 148 bytes
My inventory variable is array of structures and contains 3 records
Now, results of debugging in the function (addresses without high 2 bytes):
inventory[0] = 0x1350
inventory[1] = 0x14e0
inventory[2] = 0x1670
Difference in memory: 400 bytes, OK
On first iteration everything is OK, first record deletes without a problems
But after
++ptr;
ptr will contain: 0x13e4
inventory[0] - ptr = 148 bytes - correct difference according to structure's size
but it does not refers to next record, because address of the next record in memory: 0x14e0 and I'm getting corruption of the heap.
Any suggestions: why?
deleteInventorythelengthvariable is uninitialized when you use it. - lurkerlengthis global variable :) - pydevd