Thread: Please help... Global structures

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    Please help... Global structures

    Hi - I am quite new to this and not sure if I can do the below. Below is one file in a multi-file program... basically the main program calls initialise() and then calls process_access().

    The arguments passed to process_access are fixed as is - but i need to be able to access the structures from all the fuctions without passing them. The members set correctly on initialisation but if i try to call them through the other functions they are all zero....

    This probably doesnt make much sense but if somebosy could help me out i would be really grateful... thanks.

    Code:
    typedef struct{
    
            int mem_blk;
            int cache_blk;
            int bank_num;
            int dirty;
            int valid;
    
    } Block_Struct;
    
    typedef struct {
    
            IndexFunc indexfunc_ptr;
            Block_Struct *blocks;
    
    } Bank_Struct;
    
    typedef struct {
    
            int size_blks;
            int blks_per_bank;
            int num_banks;
            Bank_Struct *banks;
    
    } Cache_Struct;
    
    Cache_Struct one_data;
    Cache_Struct one_inst;
    Cache_Struct two;
    
    void initialise_cache(CacheParams params, Cache_Struct cache);
    Block_Struct * find(Cache_Struct cache, int memory_address);
    
    
    
    void
    initialize(CacheParams l1_inst_params, CacheParams l1_data_params,
            CacheParams l2_params)
    {
    
            initialise_cache(l1_inst_params, one_inst);
            initialise_cache(l1_data_params, one_data);
            initialise_cache(l2_params, two);
    
    }
    
    
    
    void
    initialise_cache(CacheParams params, Cache_Struct cache){
    
            int i = 0;
    
            cache.num_banks = params.num_banks;
            cache.size_blks = params.block_size;
            cache.blks_per_bank = params.num_blocks_per_bank;
    
            cache.banks = malloc(sizeof(Bank_Struct)*(cache.num_banks));
    
            /* loops for each bank in the cache to initalise the blocks in the bank */
            for( i = 0; i < (cache.num_banks); i++ ){
    
                    int j;
    
                    cache.banks[i].indexfunc_ptr = params.index_func[i];
    
                    cache.banks[i].blocks =
                            malloc(sizeof(Block_Struct)*(cache.blks_per_bank));
    
                    for ( j = 0; j < cache.blks_per_bank; j++ ){
    
                            cache.banks[i].blocks[j].cache_blk = j;
                            cache.banks[i].blocks[j].bank_num = i;
                            cache.banks[i].blocks[j].dirty = 0;
                            cache.banks[i].blocks[j].valid = 0;
                    }
            }
    
    }
    
    Block_Struct *
    find(Cache_Struct cache, int memory_address){
    
            int i;
            int num_banks, blk_size;
            int blk_offset, mem_blk_number;
            int cache_blk;
    
            Block_Struct * blk_ptr;
    
            blk_size = cache.size_blks;
    
            blk_offset = memory_address % blk_size;
            mem_blk_number = memory_address / blk_size;
    
            for( i = 0; i < num_banks; i++ ){
    
                    cache_blk = cache.banks[i].indexfunc_ptr(mem_blk_number, cache.blks_per_bank);
    
                    blk_ptr = &(cache.banks[i].blocks[cache_blk]);
    
                    if ( (blk_ptr->mem_blk == mem_blk_number) && blk_ptr->valid )
                            return blk_ptr;
            }
    
            return NULL;
    
    }
    
    void
    process_access(AccessType access_type, int address, int access_size)
    {
    
            if( access_type == ACC_DATA_READ )
                   
                    find(one_data, address);
         
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It would make more sense to me if you posted a minimally compilable snippet that demonstrates the problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    My problem is...

    Sorry - basically this is my problem. Am i able to access the structures that 'structure' and 'stucture2' point to in other functions without passing to the function. I get a segmentation fault in line i=structure->info.....

    Code:
    typedef struct {
    
            int info1;
    
    } Second_Struct;
    
    typedef struct {
    
            int info;
            Second_Struct *second;
    
    } First_Struct;
    
    First_Struct *structure;
    First_Struct *structure2;
    
    int main(int argc, char **argv){
    
            initialise();
            access_structure();
            return 1;
    }
    
    void
    initialise(void)
    {
            initialise_structure(structure);
            initialise_structure(structure2);
    }
    
    void
    initialise_structure(First_Struct *i_structure){
    
            int i = 0;
            i_structure = malloc(sizeof(First_Struct));
            i_structure->info = 13;
            i_structure->second = malloc(sizeof(Second_Struct)*(6));
    
            for( i = 0; i < (6); i++ )
                    i_structure->second[i].info1 = i;
    
    }
    
    void
    access_structure(void)
    {
    
            int i;
        
            i = structure->info;
         
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Ah. If you want to modify a pointer passed into a function, you need to pass a pointer to it; i.e. a pointer to a pointer to your structure is necessary in order to malloc it in a called function and have the change persist in the calling code.

    [edit]Something like this:
    Code:
    #include <stdlib.h>
    
    typedef struct {
       int info1;
    } Second_Struct;
    
    typedef struct {
       int info;
       Second_Struct *second;
    } First_Struct;
    
    First_Struct *structure;
    First_Struct *structure2;
    
    void initialise_structure(First_Struct **i_structure)
    {
       int i = 0;
       *i_structure = malloc(sizeof(First_Struct));
       (*i_structure)->info = 13;
       (*i_structure)->second = malloc(sizeof(Second_Struct)*(6));
    
       for ( i = 0; i < (6); i++ )
       {
          (*i_structure)->second[i].info1 = i;
       }
    }
    
    void access_structure(void)
    {
       int i = structure->info;
    }
    
    void initialise(void)
    {
       initialise_structure(&structure);
       initialise_structure(&structure2);
    }
    
    int main(int argc, char **argv)
    {
       initialise();
       access_structure();
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    OK great, thanks.

    So if i pass a pointer to the pointer to the structure to initialise it - then I can access it in functions without passing it to those functions??

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It seems like an odd mix of globals with passing parameters, but sure.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  4. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM