c++:return statement behaving weirdly - c++

Here is an outline of the code containing the relevant part of my code.
Inside the empprint function i call a bfs print function, which calls itself recursively till its done printing everything that needs to be printed, after which its supposed to return me back to empprint function. But the return statement in bfsprint doesn't take me back to empprint.
One possible reason I can think of is that bfsprint calls itself recursively so it will only return to the last bfsprint method that called it instead of empprint function but it doesnt seem to solve my problem. I m stuck up with a code whose execution doesnt terminate.
void node::empprint(node* myroot)
{
//do something
bfsprint(c);
cout<<"pt 5"; //this cout is not reached
return;
}
void node::bfsprint(Linklist<node*> noddy)
{
// lot of code to implement breadth-first search. No issue
if(c.getHead()==NULL) cout<<"1" //this does print 1 to output
if(c.getHead()==NULL) return; //I think this should send me back to empprint
// and print "pt 5" on output but program hangs.
// instead of this happening
bfsprint(c);
}
If anybody thinks this might be influenced by other code in the method , I will add it but I dont think its the case.

If your call stack looks like:
node::empprint
node::bfsprint
node::bfsprint
then returning from the final call will result in
node::empprint
node::bfsprint
So your still N calls deep away from getting back to node::empprint.
You could set a bool in the class to return back out, but thats a bit hacky..
void node::bfsprint(Linklist<node*> noddy)
{
if ( something ) { m_unwindstack = true; }
// setting the bool to force returning early/stop recursion once m_unwindstack is true to get back to empprint
if ( m_unwindstack ) { return; }
}
Edit: By the way if you're doing anything with Linklist you'll never seen the changes since your passing a copy of the data. You should pass a reference Linklist&.
Also Linklist seems like your own class? So if you don't use a reference then be sure its copyable otherwise bad things will happen.

Related

Objects vs. Static Variables for retaining function state

I have a function which processes data that comes as a sequence. Because of this, I need to know the value of certain variables from the last function call during the current function call.
My current approach to doing this is to use static variables. My function goes something like this:
bool processData(Object message){
static int lastVar1 = -1;
int curVar1 = message.var1;
if (curVar1 > lastVar1){
// Do something
}
lastVar1 = curVar1;
}
This is just a small sample of the code; in reality I have 10+ static variables tracking different things. My gut tells me using so many static variables probably isn't a good idea, though I have nothing to back that feeling up.
My question: Is there a better way to do this?
An alternative I've been looking into is using an object whose fields are lastVar1, lastVar2, etc. However, I'm not sure if keeping an object in memory would be more efficient than using static variables.
Your question has a taste of being purely about style and opinions, though there are aspects that are not a matter of opinion: multithreading and testing.
Consider this:
bool foo(int x) {
static last_val = -1;
bool result = (x == last_val);
last_val = x;
return result;
}
You can call this function concurrently from multiple threads but it wont do the expected. Moreover you can only test the function by asserting that it does the right thing:
foo(1);
assert( foo(1) ); // silenty assumes that the last call did the right thing
To setup the preconditions for the test (first line) you already have to assume that foo(1) does the right thing, which somehow defeats the purpose of testing that call in the second line.
If the methods need the current object and the previous object, simply pass both:
bool processData(const Object& message,const Object& previous_message){
if (message.var1 > previous_message.var1){
// Do something
return true;
}
return false;
}
Of course this just shifts the issue of keeping track of the previous message to the caller, though thats straight-forward and requires not messing around with statics:
Object message, old_message;
while ( get_more( message )) {
processData(message, old_message);
old_message = message;
}

How to fix a stack overflow error caused by recursive functions? C++

As titled, I figured a recursive function has caused my program stack overflowing. And the recursive logic is needed, how to fix it?
Code:
static bool orient_flip_face(HE *edge)
{
if(edge->flip == NULL)
return 1;
return orient_face(face);
}
static bool orient_face(HEF *face)
{
assert(face->oriented);
return orient_flip_face(face->edge);
}
static bool build_HE(he::Mesh_Data *mesh,
std::vector<HEV*> *hevs,
std::vector<HEF*> *hefs)
{
// process mesh data
// ...
return orient_face(first_face);
}
Basically orient_face and orient_flip_face calling each other back and forth until it's the last vertices. It's fine when I pass a simple mesh, but when I pass the stanford rabbit which has 63690 vertices, it's overflowed.Then I increased stack reversed size to 40MB and it fixed the stack overflow error. But is it a reasonable solution?
Thank you!
Your code is an example of tail recursion because the recursive calls are the last thing executed in each function. Tail recursion is easy to transform into a non-recursive loop. The following code is the equivalent to the code you have and doesn't use recursion at all.
static bool orient_face(HEF *face)
{
for (;;)
{
assert(face->oriented);
HE *edge = face->edge;
if (edge->flip == NULL)
return 1;
}
}
static bool build_HE(he::Mesh_Data *mesh,
std::vector<HEV*> *hevs,
std::vector<HEF*> *hefs)
{
// process mesh data
// ...
return orient_face(first_face);
}
As some programmer dude pointed out, the code is effectively a no-op (excepting the assert, but I'm assuming that's just a debugging detail, and not the point of the code). So unless there's something else going on you can delete it completely.
The details matter.
You are probably running into huge recursion or even infinite recursion loop. Try checking your base case condition and verify that the recursion stops within a finite limit.

C++ segfault at the end of a for loop

This code is segfaulting and I can't really figure out why. When I use gdb it segfaults at the end of the function (the curly brace). So that doesn't really give me a lot of information as to what's going on. Here's the code, I'll provide extra info if needed.
typedef std::list<Ground> l_Ground;
void Player::y_collisions(l_Ground grounds) {
for (l_Ground::const_iterator ent = grounds.begin(); ent != grounds.end(); ent++) {
if (getGlobalBounds().intersects(ent->getGlobalBounds())) {
in_air = false;
velocity -= gravity;
}
}
}
EDIT: Upon closer inspection, it's probably segfaulting at the end of that for loop. Which still doesn't really make sense because of the way the for loop is written. It shouldn't go beyond the end of the list.
EDIT2: This will work because of the answer below.
typedef std::list<Ground> l_Ground;
void Player::y_collisions(const l_Ground& grounds) {
for (l_Ground::const_iterator ent = grounds.begin(); ent != grounds.end(); ent++) {
if (getGlobalBounds().intersects(ent->getGlobalBounds())) {
in_air = false;
velocity -= gravity;
}
}
}
You were passing the grounds parameter by value. That means a copy of the list was made. Apparently your Ground class have a broken copy constructor, which makes the getGlobalBounds() method referring to some invalid pointer, which caused the crash.
You should almost never pass a big object by value unless you want to immediately copy it. Always train yourself to type const & all the time :).

Creating a vector in a method and setting it to a variable

OK so I an new to C++ and am fairly sure this should be a simple question if I can ask it right. Basically this is what I need to work:
printInfoFromVector(this->mycontroller.createVector)
This is the actual code I have:
vector<GasStation>& highPrices = this->myController.findHighestPrice();
this->findPrice(highPrices);
vector<GasStation>& findHighestPrice(){
The problem that I am having is that I can not get they types of highPrices and findHighestPrice() to match. Im fairly sure the problem is because I am passing by ref but I pretty sure that the right way to do it.
Can anyone tell me the correct way to write the assignment statement and the method head so that the types match?
If findHighestPrice is computing a new vector, then you should not return a reference, but an actual vector. Thus you would have:
vector<GasStation> findHighestPrice() { ... }
vector<GasStation> highPrices = this->myController.findHighestPrice();
For example, if you defined findHighestPrice as
vector<GasStation>& findHighestPrice() {
vector<GasStation> stations;
// ...
return stations;
}
then stations will be deallocated when the function returns and highPrices will be undefined.

Can I return in void function?

I have to return to the previous level of the recursion. is the syntax like below right?
void f()
{
// some code here
//
return;
}
Yes, you can return from a void function.
Interestingly, you can also return void from a void function. For example:
void foo()
{
return void();
}
As expected, this is the same as a plain return;. It may seem esoteric, but the reason is for template consistency:
template<class T>
T default_value()
{
return T();
}
Here, default_value returns a default-constructed object of type T, and because of the ability to return void, it works even when T = void.
Sure. You just shouldn't be returning an actual value.
Yes, you can use that code to return from the function. (I have to be very verbose here to make Stack Overflow not say that my answer is too short)
Yes, that will return from the function to the previous level of recursion. This is going to be very basic explanation, but when you call a function you are creating a new call stack. In a recursive function you are simply adding to that call stack. By returning from a function, whether you return a value or not, you are moving the stack pointer back to the previous function on the stack. It's sort of like a stack of plates. You keep putting plates on it, but than returning moves the top plate.
You could also verify this by using a debugger. Just put a few break points in your code and step through it. You can verify yourself that it works.
The simple answer to this is YES! C++ recognise void method as a function with no return. It basically tells the compiler that whatever happens, once you see the return; break and leave the method....
Yes, sometimes you may wish to return void() instead of just nothing.
Consider a void function that wants to call some pass-through void functions without a bunch of if-else.
return
InputEvent == E_Pressed ? Controller->Grip() :
InputEvent == E_Released ? Controller->Release() :
InputEvent == E_Touched ? Controller->Touch() : void();
You shouldn't have to have the return there, the program will return to the previous function by itself, go into debug mode and step through and you can see it yourself.
On the other hand i don't think having a return there will harm the program at all.
As everyone else said, yes you can. In this example, return is not necessary and questionably serves a purpose. I think what you are referring to is an early return in the middle of a function. You can do that too however it is bad programming practice because it leads to complicated control flow (not single-entry single-exit), along with statements like break. Instead, just skip over the remainder of the function using conditionals like if/else().