Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:05:23+00:00 2026-06-13T18:05:23+00:00

Full disclosure: This is my first time doing any significant programming in C, and

  • 0

Full disclosure: This is my first time doing any significant programming in C, and my first post on Stack Overflow.

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: Binding, Lambda, and SymbolEntry. I’m not using the Lambda struct yet, it’s just there for completeness. I also have a symbol table that holds symbol entries. printSymbolTable() does exactly what the name implies:

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 < numSymbols; i++)
        {
            printf("\tsymbolTable[%i]: %s = %s\n", i, symbolTable[i].binding->name, symbolTable[i].binding->value);
        }
    }
}

I’m currently trying to work out the logic for defining and looking up variables. The 2 relevant functions:

// 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 = &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();
}

main() calls testVars(). I get no warnings or errors when compiling, and the program executes successfully. However, this is the result:

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���

Not only am I getting junk values when outside of the defineVar() function, but the call to define bar shows incorrect non-junk values as well. I’m not sure what I’m doing wrong, but I assume it’s probably something with realloc(). 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:05:25+00:00Added an answer on June 13, 2026 at 6:05 pm

    Because it’s pointing to variables (or variable — at least props, haven’t read further) local to functions and the stack frame is discarded (and soon overwritten) after you return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, in order to provide full disclosure, I want to point out that this
Full disclosure: this is for an assignment, so please don't post actual code solutions!
First off, full disclosure: This is going towards a uni assignment, so I don't
full explanation: I'm working on a project to generate twitter links that post over
Full disclosure...this is my first SO question. Please be kind if I leave anything
Full disclosure : This is for a homework assignment. This is driving me nuts.
I want obtain full URL adres in php script, tell please, this code always
Full Disclosure: I am bad at javascript. I'm trying to write something that takes
[Full disclosure: Cross-post between here and ServerFault, because I believe the audiences (server admins
Full disclosure, this is part of a homework assignment (though a small snippet, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.