Cache Simulator in C

You’ve got two problems. Firstly, Scott Wales is correct about your hex2bin() function – you have a ‘x’ where you mean ‘4’. Secondly, you are not correctly counting a cache miss when you hit an invalid cache slot. You can simply handle “invalid” with exactly the same code path you use for a miss:

Categories c Tags

How to create an array of strings in C?

word_array[0] is a char, whereas word1 is a char *. Your character is not able to hold an address. An array of strings might look like it: If you rather want an array of pointers to your strings: And then: Maybe you should read this.

How do malloc() and free() work?

OK some answers about malloc were already posted. The more interesting part is how free works (and in this direction, malloc too can be understood better). In many malloc/free implementations, free does normally not return the memory to the operating system (or at least only in rare cases). The reason is that you will get gaps in … Read more

How to pause in C?

you can put before the return from the main function. That will wait for a character input before exiting the program. Alternatively you could run your program from a command line and the output would be visible.

Categories C Tags

How to properly malloc for array of struct in C

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with: In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc(). Note that’s it’s better style to use: … Read more

Sign extend a nine-bit number in C

Assuming a short is 16 bits: You can do it manually: (instr & 0x1FF) | ((instr & 0x100) ? 0xFE00 : 0). This tests the sign bit (the uppermost bit you are retaining, 0x100) and sets all the bits above it if the sign bit is set. You can extend this to 5 bits by adapting the … Read more

Categories c Tags