Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy is pointer address changing on return?
    text
    copied!<p>I'm working on homework, and I'm having a weird problem I'm hoping someone can help me out with. I have a function in the middle of my code:</p> <pre><code>Token scheme::addScheme(vector &lt;Token&gt; toAdd) { //Check if scheme is valid Token answer = isSchemeValid(toAdd); if (answer.retType() != "ok") { return answer; } identifierList * arrow = new identifierList(&amp;(toAdd.at(0)), NULL); idList = arrow; for (int i = 2; i &lt; toAdd.size()-1; i++) { (*arrow).id = &amp;(toAdd.at(i)); (*arrow).next= new identifierList(&amp;(toAdd.at(0)), NULL); arrow = (*arrow).next; } *id = toAdd.at(0); openParen = '('; closeParen = ')'; return Token("ok", "ok", 0); } </code></pre> <p>This code does everything I need it to do, except for the very end. It correctly sets all the variables to what I need them to be. Here's where it doesn't work: I run it step by step and I see that right after running the very last line return Token("ok", "ok", 0); the values of idList change from legitimate values to crazy ones (for example, the first id in the idList's line number changes from 2 to -17891602.</p> <p>this might make more sense if I post the header for what a identifierList and Token consist of: Token: #pragma once using namespace std; #include #include #include #include #include "datalogProgram.h"</p> <pre><code>enum state {COMMA, PERIOD, Q_MARK, LEFT_PAREN, RIGHT_PAREN, COLON, COLON_DASH, MULTIPLY, ADD, SCHEMES, FACTS, RULES, QUERIES, ID, STRING, COMMENT, WHITESPACE, UNDEFINED, ENDOFFILE, START}; class Token { friend class datalogProgram; public: int lineNumber; string type; string value; Token(string inType, string inValue, int inLineNum); void route(state inState, string inValue, int inLineNum); string retType(); string retValue(); //A list of a bunch of functions that don't matter here. }; </code></pre> <p>And an identifierList:</p> <pre><code>class identifierList { friend class scheme; public: Token * id; identifierList * next; identifierList(Token * inId, identifierList * inNext); }; </code></pre> <p>I don't get it- why are any variables changing when all I'm doing is returning from a function?</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