Verwirrung über typedef struct in C++
Frage
Ich habe ein Programm mein professor schrieb, die simuliert die Weg das Gedächtnis schreibt der L2-cache.Es hat ein paar stellen, wo ich bin füllt in der leere.Das erste, was ich tun soll, klar, dass das valid bit der jeder cache-Eintrag.Er hat uns die folgenden:
//number of cache entries (2^11)
#define L2_NUM_CACHE_ENTRIES (1<<11)
/***************************************************
This struct defines the structure of a single cache
entry in the L2 cache. It has the following fields:
v_d_tag: 32-bit unsigned word containing the
valid (v) bit at bit 31 (leftmost bit),
the dirty bit (d) at bit 30, and the tag
in bits 0 through 15 (the 16 rightmost bits)
cache_line: an array of 8 words, constituting a single
cache line.
****************************************************/
Typedef struct {
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
} L2_CACHE_ENTRY;
//The L2 is just an array cache entries
L2_CACHE_ENTRY l2_cache[L2_NUM_CACHE_ENTRIES];
So, wie ich es verstehe, clearing die gültigen bit-genau bedeutet eine Einstellung von null.Die gültigen bit ist bit 31 des v_d_tag, also ich sollte mit einer Bitmaske - ich will etwas tun, entlang der Linien von, "v_d_tag = v_d_tag & 0x80000000;", denke ich?Aber was ich nicht verstehe ist, wie kann ich durch gehen und tun, dass für jeden cache-Eintrag.Ich sehe das array der cache-Einträge (l2_cache), aber ich sehe nicht, wie die v_d_tag damit in Zusammenhang steht.
Kann jemand es mir erklären?
Lösung
typedef struct redundant in C++, wie die #define
Ich sehe, Sie könnte static const int.
Um Sie zu deaktivieren Sie alle, die Sie würde wollen zu tun
for(int i = 0; i < L2_NUM_CACHE_ENTRIES; i++)
l2_cache[i].v_d_tag &= 0x80000000;
Andere Tipps
Die Struktur definiert ist, in einem C-Mode, da im C-es ist eine Allgemeine Redewendung, zu typedef deklarieren Sie eine Struktur, so dass es kann verwendet werden als Typ-ohne zu schreiben struct L2_CACHE_ENTRY
auf jeden Hinweis.Dieses idiom ist nicht mehr nötig, in C++, da die struct
tag der Arbeit als ein individueller Typ.
Kurz gesagt, in C++ können Sie Sie behandeln
typedef struct {
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
} L2_CACHE_ENTRY;
genau das gleiche wie
struct L2_CACHE_ENTRY{
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
};