简体   繁体   中英

How do I initalize values? Array of structs?

I'm trying to initialize this values at the start of my program. I'm not getting the expected values so I might be doing something wrong with the pointers.

struct TLBentry{
  unsigned int physicalAddress;
  unsigned int virtualAddress;
};

struct pageTableEntry{
  char validFlag;
  char dirty;
  unsigned int physicalAddress;
};
  memSys->virtMem = malloc( sizeof( int ) * sizeVM * pageSize );
  memSys->tlb = malloc( sizeof(struct TLBentry) * sizeTLB );
  memSys->pgTable = malloc( sizeof(struct pageTableEntry) * sizeVM );

  for( int i=0; i < memSys->sizeTLB; i++ ){
    struct TLBentry* entry = &(memSys->tlb[i]);
    entry->virtualAddress = i;
    entry->physicalAddress = i;
  }
  for( int i=0; i < memSys->sizeVM; i++ ){
    memSys->pgTable[i].physicalAddress = i;
    memSys->pgTable[i].validFlag = 0;
  }
  for( int i=0; i < memSys->sizePM; i++ ){
    memSys->pgTable[i].physicalAddress = i;
    memSys->pgTable[i].validFlag = 1;
  }
  printf("initialize %d\n", memSys->tlb[1].virtualAddress );

The print statement should be "initialize 1" but it prints "initialize 0"

I don't know what's going on.

Do you know the difference between = and == operator in C? You're not attributing value i for your physicalAddress, you're doing nothing in this line.

entry->physicalAddress == i;
entry->virtualAddress == i;

In C, a == b is not assigning b to a, it's just checking if they are equal.

And in this line, why are you printing tlb[1]? Do you know that array starts at 0 in C?

printf("initialize %d\n", memSys->tlb[1].virtualAddress );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM