Thread: Initializing a pointer of a structure within a structure

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Initializing a pointer of a structure within a structure

    Hi I'm trying to intialize a struct for my cache simulator.

    The problem is one of the elements I need to initialize is another structure called cache_line which is a linked list and I wanna initialize the pointers to head and last.

    I thought it would do it like
    Code:
    cache wk_cache = {
    	my_cache.LRU_head = (Pcache_line*)malloc(sizeof(Pcache_line)*my_cache.n_sets); 
            my_cache.LRU_tail = my_cache.LRU_head;
    	};
    Does that look right to anyone? Or am I way off...I haven't used a struct within a struct in awhile

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's too hard to say without seeing your structures. Is 'n_sets' a pointer? Also, I don't think it's going to let you call a function as an initializer there.
    You'll need to split that up to something like:
    Code:
    cache wk_cache;
    wk_cache.LRU_head = malloc( my_cache.nsets * sizeof * my_cache.LRU_head );
    wk_cache.LRU_tail = my_cache.LRU_head;
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Here are my structs:

    Code:
    typedef struct cache_ {
      int size;			/* cache size */
      int associativity;		/* cache associativity */
      int n_sets;			/* number of cache sets */
      unsigned index_mask;		/* mask to find cache index */
      int index_mask_offset;	/* number of zero bits in mask */
      Pcache_line *LRU_head;	/* head of LRU list for each set */
      Pcache_line *LRU_tail;	/* tail of LRU list for each set */
      int *set_contents;		/* number of valid entries in set */
      int contents;			/* number of valid entries in cache */
    } cache, *Pcache;
    
    typedef struct cache_stat_ {
      int accesses;			/* number of memory references */
      int misses;			/* number of cache misses */
      int replacements;		/* number of misses that cause replacments */
      int demand_fetches;		/* number of fetches */
      int copies_back;		/* number of write backs */
    } cache_stat, *Pcache_stat;
    Obviously I omitted some stuff for ease of reading...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing structure in a class
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2010, 08:40 AM
  2. Initializing an array inside of a structure
    By Lima in forum C Programming
    Replies: 6
    Last Post: 06-08-2009, 08:53 PM
  3. initializing structure variables
    By cs32 in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 05:33 PM
  4. Initializing a reference within a structure
    By Russell in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2005, 07:22 AM
  5. initializing nested structure arrays
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 06-10-2004, 10:58 PM

Tags for this Thread