Thread: Another pointers / struct/ array related question

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    Another pointers / struct/ array related question

    First of all let me say thank you for this wonderful forum for all the help, I am learning a lot here.
    In my Gomoku game, I have most of the functions and static variables inside the main.c file. but I h ave decided to make a seperate .h and .c files for the Transposition table functions. My transpostion table is actually an array of pointers to structs. this struct type represents an entry in the table and holds all the relevant information (score,valueType,depth etc.)
    The transposition table itself is instantiated in the main file as a static member, and only passed to the insert() and find() operations in the TT.c file as a reference (pointer).
    I am initiating the table with all pointers as 0 (NULL) ...actually that the reason i chose to use pointers to struct, so i can make them all NULL .

    The problem is, in the alpha beta function, where i try to get something from the table, I get 0 results.. that's probably means that all the entries are still NULL even after i insert something to a certain index in the table. this is the insert() function, am i doing it right? can i define a struct inside a function and assign its pointer to my static table? from some reason it doesn't work.

    This is the relevant function, let me know if you need something more
    Code:
    void insert(unsigned long long key,int value,int valueType,int depth,Entry **table){
        int index = compress(key);
        Entry entry;
        entry.key = key;
        entry.value = value;
        entry.valueType = valueType;
        entry.depth = depth;
        table[index] = &entry;
    }

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Entry entry is a local object; after the function returns that variable/object no longer exists so storing a pointer to entry is invalid (because, well, what's it point to after entry stops existing?) You probably want to dynamically allocate entry using malloc.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    ok..thx. that's my reasoning also. but i thought that since I can RETURN a struct from a function (unlike array) i can maybe also assign its pointer inside an external/static variable of the file. but i guess i was wrong.
    What is the difference between assigning a struct pointer inside array index, and returning a struct from a function? why i can't do the first and can do the other?
    I would like to use malloc, but i afraid that it will be too slow... malloc and realloc a lot of times. the operation should be as fast as possible. Do i have other options except using malloc?

    Maybe i should just instantiate the whole table with structs and just use a "NULL" flag or something to indicate if that entry is active?
    Last edited by patishi; 09-11-2013 at 04:27 PM.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    I changed my insert function and now using malloc:
    Code:
    void insert(unsigned long long key,int value,int valueType,int depth,Entry **table){
        int index = compress(key);
        if(table[index] == NULL){}
            Entry *entryPtr = malloc(sizeof(Entry));
            entryPtr->key = key;
            entryPtr->value = value;
            entryPtr->valueType = valueType;
            entryPtr->depth = depth;
            table[index] = entryPtr;
        }
        else{
            table[index]->key = key;
            table[index]->value = value;
            table[index]->valueType = valueType;
            table[index]->depth = depth;
        }
    is this acceptable?
    And another question: as for the freeing memory part, when i do a loop and set all those pointers to NULL, it doesn't free the memory does it? I am asking because I want to empty the table after every move, and I don't know if i should free all the memory or just make all those pointers NULL again.
    Last edited by patishi; 09-11-2013 at 05:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers to a struct
    By turtlegrip in forum C Programming
    Replies: 8
    Last Post: 05-12-2013, 07:00 PM
  2. Array of pointers to a struct
    By Magical in forum C Programming
    Replies: 11
    Last Post: 03-20-2011, 06:03 PM
  3. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  4. array of pointers of a struct
    By paulur in forum C Programming
    Replies: 18
    Last Post: 04-14-2006, 07:17 AM
  5. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 12:54 PM