Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers getting junk values outside of functions, but regular values inside them
    text
    copied!<p>Full disclosure: This is my first time doing any significant programming in C, and my first post on Stack Overflow.</p> <p>I'm working on code that will eventually be used with Bison to implement a small subset of the Scheme/Racket language. All of this code is in a single C file. I have three structs: <code>Binding</code>, <code>Lambda</code>, and <code>SymbolEntry</code>. I'm not using the <code>Lambda</code> struct yet, it's just there for completeness. I also have a symbol table that holds symbol entries. <code>printSymbolTable()</code> does exactly what the name implies:</p> <pre><code>typedef struct { char* name; char* value; } Binding; typedef struct { int numBindings; Binding** bindings; char* functionBody; } Lambda; typedef struct { Binding* binding; Lambda* function; } SymbolEntry; SymbolEntry* symbolTable = NULL; int numSymbols = 0; void printSymbolTable() { if (symbolTable) { int i = 0; for (i; i &lt; numSymbols; i++) { printf("\tsymbolTable[%i]: %s = %s\n", i, symbolTable[i].binding-&gt;name, symbolTable[i].binding-&gt;value); } } } </code></pre> <p>I'm currently trying to work out the logic for defining and looking up variables. The 2 relevant functions:</p> <pre><code>// Takes a name and an exprssion and stores the result in the symbol table void defineVar(char* name, char* expr) { printf("\nSetting %s = %s\n", name, expr); printf("Previous number of symbols: %i\n", numSymbols); Binding props; props.name = name; props.value = expr; SymbolEntry entry; entry.binding = &amp;props; entry.function = NULL; symbolTable = realloc(symbolTable, sizeof(SymbolEntry) * ++numSymbols); if (!symbolTable) { printf("Memory allocation failed. Exiting.\n"); exit(1); } symbolTable[numSymbols - 1] = entry; printf("New number of symbols: %i\n", numSymbols); printf("defineVar result:\n"); printSymbolTable(); } // Test storing and looking up at least 4 variables, including one that is undefined void testVars() { printf("Variable tests\n"); defineVar("foo", "0"); printf("After returning from defineVar:\n"); printSymbolTable(); defineVar("bar", "20"); printf("After returning from defineVar:\n"); printSymbolTable(); } </code></pre> <p><code>main()</code> calls <code>testVars()</code>. I get no warnings or errors when compiling, and the program executes successfully. However, this is the result:</p> <pre><code>Variable tests Setting foo = 0 Previous number of symbols: 0 New number of symbols: 1 defineVar result: symbolTable[0]: foo = 0 After returning from defineVar: symbolTable[0]: 1�I��^H��H���PTI��@ = �E Setting bar = 20 Previous number of symbols: 1 New number of symbols: 2 defineVar result: symbolTable[0]: bar = 20 symbolTable[1]: bar = 20 After returning from defineVar: symbolTable[0]: 1�I��^H��H���PTI��@ = �E symbolTable[1]: 1�I��^H��H���PTI��@ = �E��� </code></pre> <p>Not only am I getting junk values when outside of the <code>defineVar()</code> function, but the call to define <code>bar</code> shows incorrect non-junk values as well. I'm not sure what I'm doing wrong, but I assume it's probably something with <code>realloc()</code>. However, a similar strategy worked when parsing a string into individual tokens, so that's what I was trying to emulate. What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload