0

I was working on part of my project which has to do with direct mapped caches. In the following I created a structure for my cache elements:

typedef struct node{
  int *tagBits; 
  int *setBits;
  int *blockOff; 
} cacheNode; 

I want to store the corresponding block offset bits, set bits and tag bits within each element.

For example, the binary of a memory address given is: 000000000000000000001001110010110011110101000100. I want the first two bits to be the block offset and the next two to be the set bits followed by the remaining 44 to be the tag. The result should be blockOff = 00, setBit = 01 tag = rest. The parameters: blockOff, setBits and tagBits represent how many bits belong to what in the binary (blockOff =2, set = 2 and tag = 44). Here is what I tried so far:

void directMap(cacheNode** cache,int blockOff, int setBits, int 
tagBits,int* binary)
{
int tagDec, blockDec, setDec, i= 0; 
cacheNode* element = (cacheNode*)malloc(sizeof(cacheNode)); 

element->blockOff = (int*)malloc(blockOff * sizeof(int)); 
element->setBits = (int*)malloc(setBits * sizeof(int)); 
element->tagBits = (int*)malloc(tagBits * sizeof(int)); 

for(i = 0; i< 48; i++){
  if(i = 0 && i < blockOff){
    element->blocOff[i] = binary[i]; 
  }
  else if(i = (blockOff) && i < (blockOff + setBits)){
    element->setBits[i] = binary[i]; 
  }
}

 printString(element->blockOff,blockOff); 
 printString(element->setBits,setBits); 
}  

My problem is that when I print out both integer arrays from my struct it gives me '00' for set and blockOff. The answer is correct for blockOff but not set, set should be '01'. Any HINTS as to why?

Carlos Romero
  • 181
  • 1
  • 13
  • 1
    Really not clear what you're trying to do. What does "binary" look like? Is it really an array of ints? LSB first or MSB first? Fixed size? Throw some printf()s in there to see what's going on, and use a debugger. – Lee Daniel Crocker Apr 13 '18 at 17:43
  • PS: I forgot to mention. My binary array is stored in reverse order of what I posted. – Carlos Romero Apr 13 '18 at 17:43
  • That answers one question out of a dozen. Still not at all clear. Add some printfs(), show the results, and show us what is expected. – Lee Daniel Crocker Apr 13 '18 at 17:47
  • This isn't making any sense to me. You want to have something "direct mapped" but allocate fresh memory and copy stuff over (btw don't cast the return value of `malloc`). – Daniel Jour Apr 13 '18 at 17:48
  • 1
    Also you talk about bits but assume `binary` to be an array of 48 `int` .. – Daniel Jour Apr 13 '18 at 17:49
  • `element` seems to be a local variable that is never given back to the caller. Memory leak? – jxh Apr 13 '18 at 18:00
  • [don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Apr 13 '18 at 18:21
  • Why are you using these parameters as the sizes of the arrays to allocate? Shouldn't the sizes be based on the number of bits in those fields? – Barmar Apr 13 '18 at 18:24
  • Sorry guys I've only been coding in c for around a semester. My real question is I have a int array called binary which is length 48 or 48 "bits". All I really want to do is to get the 'substring' of the array and store part of it into my struct. – Carlos Romero Apr 13 '18 at 20:53

1 Answers1

0
for(i = 0; i< 48; i++){
  if(i = 0 && i < blockOff){

  // Does not executes when above `if()` is true when `blockOff` is positive.
  else if(i = (blockOff) && i < (blockOff + setBits)){  

is certainly wrong in its use of i. if(i = 0 && i < blockOff) keeps re-assigning i. = is assignment. == is for comparing.

Change to

for(i = 0; i< 48; i++){
  if(i == 0 && i < blockOff){
  else if(i == (blockOff) && i < (blockOff + setBits)){

This also implies not all warnings are enabled. Save time, enable all compiler warnings.


Other problems may exist.


Tip: avoid malloc coding errors. Simplify and allocate to the de-referenced object.

// element->blockOff = (int*)malloc(blockOff * sizeof(int));
element->blockOff = malloc(sizeof *(element->blockOff) * blockOff);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Hey, thanks for taking the time to help. Can you please explain why it is a bad idea to cast malloc? – Carlos Romero Apr 13 '18 at 22:25
  • @CarlosRomero To be clear, this answer does not imply casting `malloc()` is _bad_ nor do I consider it bad - I consider it unnecessary - yet tolerable. It certainly is not needed per the C spec. See [this](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) for deeper insight. I consider use of `sizeof(int)` versus `sizeof *(element->blockOff)` something approaching _bad_. `sizeof(some_type)` is too often wrong and not readily detected, like here. – chux - Reinstate Monica Apr 13 '18 at 23:02
  • @CarlosRomero ... and as a general rule, casting should be avoided. It often implies some weak coding practice. Many exceptions exist do exist like the cast in `printf("%p\n", (void*)some_pointer);` to print a pointer value. – chux - Reinstate Monica Apr 13 '18 at 23:09