Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of this malloc error? Corrupted top size?

Tags:

c

I'm trying to initialize some values in couple of structs I created. (the goal of the program is to simulate virtual memory) For some reason when I try to initalize pgTable[i].validFlag = 1 I get this error:

malloc(): corrupted top size

but not if I initialize it to 0. I thought this had something to go with me going off the end of my array but I don't see how that's possible.

Can anyone tell me what I'm doing wrong?

  int* memmory = malloc( sizeof( int ) * sizeVM * pageSize );
  struct TLBentry* tlb = malloc( sizeof(struct TLBentry) * sizeTLB );
  struct pageTableEntry* pgTable = malloc( sizeof(struct pageTableEntry) * sizeVM );

  for( int i = 0; i < sizeTLB; i++){
    tlb[i].virtualAddress = i;
    tlb[i].physicalAddress = i;
  }

  for( int i = 0; i < sizePM; i++){
    pgTable[i].dirty = 0;
    pgTable[i].validFlag = 1;
    pgTable[i].physicalAddress = i;
  }

  memSys->virtMem = memmory;
  memSys->tlb = tlb;
  memSys->pgTable = pgTable;
like image 455
Kenneth Clarke Avatar asked Apr 22 '19 22:04

Kenneth Clarke


People also ask

What causes malloc error?

The most likely causes are writing outside the bounds of an allocated object, or writing to an object after it has been deleted. These errors can be difficult to track down with a debugger.

What is double free or corruption?

A double free or corruption likely means that free was called twice on the same block of memory, or that something was overwritten that shouldn't have been, e.g. an array overrun or something similar. This might have happened deep within Julia itself or in some C library that your code calls.


1 Answers

Your loop goes up to sizePM while you allocate sizeVM entries.

like image 91
SomeWittyUsername Avatar answered Sep 24 '22 08:09

SomeWittyUsername