Solved

allocating memory for a data structure and accessing it

Posted on 2007-11-23
5
173 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:d2fox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20341252
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20341258
I think this line:
     if ((cache = malloc(numblocks * 2 * sizeof(int))) == NULL) {
should be:
     if ((cache = malloc(numblocks * sizeof(cache_line_t))) == NULL) {
 
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20341267
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 20341268
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20341273
Oh, and this :

      / *    initialize   * /

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

      /*    initialize   */
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
chcp 65001 File encoding 66 298
C language IDE – Compilers installation 14 78
sameEnds challenge 3 187
Is it possible to delete files one by one rather than the whole? 15 36
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question