• C

allocating memory for a data structure and accessing it

I get an access violation from the below code.
Can't figure it out and would appreciate some tips.

Thanks!

typedef struct associativity
   {
   int vb; /* valid bit */
   int tag;
   } assoc_t;
 
typedef struct cache_line
   {
   assoc_t *assoc;
   int     *replacement;
   } cache_line_t;
 
cache_line_t *cache=NULL;
 
  numblocks = 64;
  cache_blocksize = 1;
  if ((cache = malloc(numblocks * 2 * sizeof(int))) == NULL) {
    (void)fprintf(stderr, "ERROR: Malloc failed");
    (void)exit(EXIT_FAILURE);    /* or return EXIT_FAILURE; */
  }
   /* for each cache line, allocate the assoc arrays */
    
    if ((assoc = malloc(numblocks * cache_blocksize * 2 * sizeof(int))) == NULL) {
    (void)fprintf(stderr, "ERROR: Malloc failed");
    (void)exit(EXIT_FAILURE);    /* or return EXIT_FAILURE; */
 
  }
 
    / *    initialize   * /
    
    j=0;
    i=0;
    for (j=0;j<numblocks;j++){
    
        for (i=0;i<cache_blocksize;i++){
            cache[j].assoc[i].vb = 0;
            cache[j].assoc[i].tag = 0;
    }
   }
}

Open in new window

d2foxAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
Or your modified code would be :

typedef struct associativity {
  int vb; /* valid bit */
  int tag;
} assoc_t;
 
typedef struct cache_line {
  assoc_t *assoc;
  int     *replacement;
} cache_line_t;
 
cache_line_t *cache=NULL;
 
  numblocks = 64;
  cache_blocksize = 1;
 
  if ((cache = malloc(numblocks * sizeof(cache_line_t))) == NULL) {
    (void)fprintf(stderr, "ERROR: Malloc failed");
    (void)exit(EXIT_FAILURE);    /* or return EXIT_FAILURE; */
  }
 
  /* for each cache line, allocate the assoc arrays */
  for (j = 0; j < numblocks; ++j) {
    if ((cache[j].assoc = malloc(numblocks * cache_blocksize * sizeof(assoc_t))) == NULL) {
      (void)fprintf(stderr, "ERROR: Malloc failed");
      (void)exit(EXIT_FAILURE);    /* or return EXIT_FAILURE; */
    }
  }
 
  / *    initialize   * /
  for (j = 0; j < numblocks; ++j) {
    for (i = 0; i < cache_blocksize; ++i) {
      cache[j].assoc[i].vb = 0;
      cache[j].assoc[i].tag = 0;
    }
  }
}

Open in new window

0
 
Infinity08Commented:
First of all, you'd better change this :

          if ((cache = malloc(numblocks * 2 * sizeof(int))) == NULL) {

into :

          if ((cache = malloc(numblocks * sizeof(cache_line_t))) == NULL) {

and the same for :

          if ((assoc = malloc(numblocks * cache_blocksize * 2 * sizeof(int))) == NULL) {

which becomes :

          if ((assoc = malloc(numblocks * cache_blocksize * sizeof(assoc_t))) == NULL) {



You also probably want to put the second malloc in a loop that loops over the previously allocated cache_line_t's, and allocates their respective alloc members. Because otherwise these lines will cause the access violation you saw :

            cache[j].assoc[i].vb = 0;
            cache[j].assoc[i].tag = 0;



0
 
Jaime OlivaresSoftware ArchitectCommented:
I think this line:
     if ((cache = malloc(numblocks * 2 * sizeof(int))) == NULL) {
should be:
     if ((cache = malloc(numblocks * sizeof(cache_line_t))) == NULL) {
 
0
 
Jaime OlivaresSoftware ArchitectCommented:
You are assuming that the struct have the same size of 2 integers, but this not always is true.
Anyway this kind of calculation is unnecessary.
0
 
Infinity08Commented:
Oh, and this :

      / *    initialize   * /

is not a valid comment. The /* and */ can't contain spaces !! So it has to be :

      /*    initialize   */
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.