
-----------------------------------
Srlancelot39
Mon Jun 02, 2014 10:52 am

Issue with: Recursion/function call/old variable data
-----------------------------------
I have this Boolean expression truth table solver that I'm making, and it is working relatively well so far (haven't started the bracket management code yet though *insert 'scared' Emoticon here*) except for the fact that the solution isn't being sent to its column correctly.

For example (as you will see if/when you run the code):
If you enter A+B+C+D as the expression, the recursion function will make a copy of it and solve it (all the way down to 0+0 and then 0 in the first case since the first row of the truth table has all inputs' values set to 0).  The function notices that the string (the copy of the expression) is only 1 character long (and therefore solved), and will return the copy of the expression as the value to be displayed.

if (strlen(copyofexpression) > 1)//recurse if there is more to solve
	{
		Recursion(copyofexpression);
		return copyofexpression;
	}
	else
	{
		return copyofexpression;
	}

The issue begins to arise here:
When the recursion function completes, it returns the copy of the expression.  Since the function was last called inside itself, the compiler is sent back to that call.  At this point, for some reason, the value of the variable holding the copy of the expression is changed from '0' to '0+0', and this is then passed to the printf in the function that draws the truth table...and for some reason, it doesn't even print '0+0', it prints a random character.

...wtf? lol


#include 
#include 
#include 
#include 
#include 

char * InputFind(char IF_expression

I am using Visual Studio Professional 2013.

Btw, anyone know how to change code colouring in the editor?  i.e., make it look how it appears in these forums?

Thanks!

-----------------------------------
Tony
Mon Jun 02, 2014 11:31 am

RE:Issue with: Recursion/function call/old variable data
-----------------------------------
copyofexpression on the stack, returning a pointer to the variable, but the variable is destroyed once the function exits. Still, the pointer points to some address in the memory, and that could be attempted to be read as whatever type you want. No guarantees as to what's going to be there though.


...it prints a random character...


You can either return the full value, or allocate the space on the heap (malloc), which you would have to clean up on your own.

-----------------------------------
Srlancelot39
Mon Jun 02, 2014 12:12 pm

RE:Issue with: Recursion/function call/old variable data
-----------------------------------
That is most likely the issue.  Could this be solved by changing the storage type?  I.e. static, global, etc?
I did that with a bunch of other variables and it really cleaned up/simplified my functions...

EDIT: Can you explain more about the pointer being passed to the variable?  I wasn't even aware that there was a pointer involved lol, and what variable is it being passed to (just to be clear)?

-----------------------------------
Tony
Mon Jun 02, 2014 1:32 pm

Re: RE:Issue with: Recursion/function call/old variable data
-----------------------------------

I wasn't even aware that there was a pointer involved

What's the return type of:

what variable is it being passed to (just to be clear)?
The returned value is being used at least in

Returning from the called function will pop the top frame off of the stack


-----------------------------------
Srlancelot39
Mon Jun 02, 2014 4:02 pm

RE:Issue with: Recursion/function call/old variable data
-----------------------------------
Haha, oops! I thought char *  would define a function that returns a string.

And okay, I was aware that it was being passed to/through the function calls, I just wasn't thinking of them as variables when you mentioned it and thus was curious :P


Thanks! I like the way you answer; helpful, yet not simply tossing out a solution.

I'm gonna go experiment/read on returning strings and using storage types...

EDIT: Just remembered that char *  is required for strings since strings are pointers to arrays of characters...so what I need to do is get the pointer working correctly, not eliminate it lol

-----------------------------------
Srlancelot39
Mon Jun 02, 2014 6:59 pm

RE:Issue with: Recursion/function call/old variable data
-----------------------------------
Fixed it!  As it turns out, there were other issues in addition to the ones you found, but I didn't find them until after fixing the issues you mentioned, so...

Thanks, Tony!

+1 Karma
