Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global pointer value lost. 2 c files

I'm making trie using 3 files: speller.c is the main file, dictionary.c contains functions & global root trie pointer, dictionary.h declares a dictionary's functions. Having problems with global root pointer value lost, after function load complete in dictionary.c.

speller.c

int main(int argc, char* argv[])
{   
    // load dictionary
    char* dictionary = TMPDICT;

    bool loaded = load(dictionary);

    // abort if dictionary not loaded
    if (!loaded)
    {
        printf("Could not load %s.\n", dictionary);
        return 1;
    }

    //next I need to call check function, but root value is missing here

}

dictionary.c

typedef struct TrieNode
    {
      struct TrieNode **children;
      bool is_word;
    } TrieNode;
struct TrieNode * root;


struct TrieNode *create_trienode(char c, struct TrieNode *parent);
bool load(const char* dictionary)
    {

        FILE * file = fopen(dictionary, "r"); 
        if (file == NULL) 
        {
            printf("trie: No such file(dictionary)\n"); 
            return 1;
        }

        struct TrieNode *root = create_trienode(' ', NULL);
        struct TrieNode *ptr = root; 
        int character;
        int converted;
        int buffer;

        //This handles if file does not end with a newline
        character = fgetc(file);
        buffer = fgetc(file);

        while(character != EOF)
        {
            character = tolower(character);
            if (character == 10) // if newline, start new word read
            {

            }
            else 
            if(isalpha(character))
            {
                converted = character - 'a'; 
                if(ptr->children[converted] == NULL)
                {
                    ptr->children[converted] = create_trienode(character, ptr); 
                }
            ptr = ptr->children[converted]; 
            }


        if (character == SLASHNUM) //if backslash
        {
            if(ptr->children[SLASH_TMP] == NULL)
            {
                ptr->children[SLASH_TMP] = create_trienode(character, ptr);
            }
            ptr = ptr->children[SLASH_TMP];
        }

        if(ptr != root && (!(character == SLASHNUM  || isalpha(character)) || buffer == EOF)) 
        {
            ptr->is_word = true;
            ptr = root;
            word_count++;
        }

          character = buffer;
          buffer = fgetc(file);
        }

       return true; 
    }


struct TrieNode *create_trienode(char c, struct TrieNode *parent)
{
    struct TrieNode *node = malloc(sizeof(struct TrieNode));
    node->children = malloc(ALPHABET_SIZE * sizeof(struct TrieNode*));
    node->is_word=false;

    for (int i = 0; i < ALPHABET_SIZE; i++)
    {
        node->children[i] = NULL;
    }

    return node; 
}

gdb

Breakpoint 1, load (dictionary=0x80489c4 "tmpDict") at dictionary.c:100
100     character = fgetc(file);
(gdb) info locals
file = 0x804b008
root = 0x804b170
ptr = 0x804b170
character = 1267019363
converted = 262929407
buffer = 1266804984
(gdb) c
Continuing.

Breakpoint 2, main (argc=1, argv=0xbffff0d4) at speller.c:40
$1 = (struct TrieNode *) 0x0
(gdb)

How can I make root visible in speller.c? Also in the task said that I can't change speller.c

like image 359
Tom Avatar asked Feb 20 '26 07:02

Tom


2 Answers

In general, you need to declare the root as extern in the dictionary.h file and include that header file in speller.c.

This way, root will be available to the translation unit spleller.c. Next, you can make use of root inside speller.c just like any other local variable (which is defined in that file itself).


[As rightly pointed by Mr. alk in below comment]

The issue here is the definition of the local root variable

 struct TrieNode *root = create_trienode(' ', NULL);

inside the load() function actually shadows the global root

struct TrieNode * root;`

and thus, the global root never get updated.

Related, from C11 standard, chapter §6.2.1 (emphasis mine)

[...] If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

like image 190
Sourav Ghosh Avatar answered Feb 22 '26 20:02

Sourav Ghosh


Your code defines root twice, with the definition local to load() shadowing the global definition.

This line

struct TrieNode *root = create_trienode(' ', NULL);

defines root locally and assigns the result of create_trienode() to this local definition of root.

From this local definition of root on, the globally defined root is shadowed by the local one, and not accessible any more.

To fix this change this:

    struct TrieNode *root = create_trienode(' ', NULL);
    struct TrieNode *ptr = root; 

to

    struct TrieNode *ptr= create_trienode(' ', NULL);
    root = ptr; 
like image 27
alk Avatar answered Feb 22 '26 19:02

alk