making this function thread safe while maintaining recursivity - c++

How do I make this function thread safe while maintaining the recursive nature of the code?
int foo(char *p)
{
static int i = 0;
if (*p == '\0') return i;
i++;
return foo(p+1);
}

#include <iostream>
using namespace std;
int foo(char* p, int start)
{
if (*p == 0) return start;
return foo(p+1, start+1);
}
int main()
{
char test[] = "HI THERE";
cout << foo(test, 0);
return 0;
}

In C++11, you could use thread_local:
int foo(char *p)
{
thread_local int i = 0;
if (*p == '\0') return i;
i++;
return foo(p+1);
}
The function is, I hope, just an example as i=0 will be executed only once (in your example) and once per thread in my example.
Older compilers sometimes support static __thread as a pre-C++11-alternative.

int foo(char *p, int i = 0)
{
if(*p == '\0')
return i;
return foo(p+1, i+1);
}
Recursion is nice and all, but it can be less efficient than a loop if stack frames are created. It's the easiest way to cause a stack overflow. I would recommend getting rid of it. The following is simpler and likely faster:
int foo(char *p)
{
return strlen(p);
}
Or better yet, just call strlen directly and get rid of foo.
Note that this is pretty unsafe. What if a '\0' doesn't come? You'll just read on into who knows what...

Related

function parameters that are writeable only by the function itself - recursion counter

So I'm trying to write a recursive function that keeps track of how often it got called. Because of its recursive nature I won't be able to define an iterator inside of it (or maybe it's possible via a pointer?), since it would be redefined whenever the function gets called. So i figured I could use a param of the function itself:
int countRecursive(int cancelCondition, int counter = 0)
{
if(cancelCondition > 0)
{
return countRecursive(--cancelCondition, ++counter);
}
else
{
return counter;
}
}
Now the problem I'm facing is, that the counter would be writeable by the caller of the function, and I want to avoid that.
Then again, it wouldn't help to declare the counter as a const, right?
Is there a way to restrict the variable's manipulation to the function itself?
Or maybe my approach is deeply flawed in the first place?
The only way I can think of solving this, is to use a kind of "wrapper-function" that keeps track of how often the recursive function got called.
An example of what I want to avoid:
//inside main()
int foo {5};
int countToZero = countRecursive(foo, 10);
//countToZero would be 15 instead of 5
The user using my function should not be able to initially set the counter (in this case to 10).
You can take you function as is, and wrap it. One way I have in mind, which completely encapsulates the wrapping is by making your function a static member of a local class. To demonstrate:
int countRecursive(int cancelCondition)
{
struct hidden {
static int countRecursive(int cancelCondition, int counter = 0) {
if(cancelCondition > 0)
{
return countRecursive(--cancelCondition, ++counter);
}
else
{
return counter;
}
}
};
return hidden::countRecursive(cancelCondition);
}
Local classes are a nifty but rarely seen feature of C++. They possess some limitations, but fortunately can have static member functions. No code from outside can ever pass hidden::countRecursive an invalid counter. It's entirely under the control of the countRecursive.
If you can use something else than a free function, I would suggest to use some kind of functor to hold the count, but in case you cant, you may try to use something like this using friendship to do the trick:
#include <memory>
class Counter;
int countRecursive(int cancelCondition, std::unique_ptr<Counter> counter = nullptr);
class Counter {
int count = 0;
private:
friend int countRecursive(int, std::unique_ptr<Counter>);
Counter() = default; // the constructor can only be call within the function
// thus nobody can provide one
};
int countRecursive(int cancelCondition, std::unique_ptr<Counter> c)
{
if (c == nullptr)
c = std::unique_ptr<Counter>(new Counter());
if(cancelCondition > 0)
{
c->count++;
return countRecursive(--cancelCondition, std::move(c));
}
else
{
return c->count;
}
}
int main() {
return countRecursive(12);
}
You can encapsulate the counter:
struct counterRecParam {
counterRecParam(int c) : cancelCondition(c),counter(0) {}
private:
int cancelCondition;
int counter;
friend int countRecursive(counterRecParam);
};
Now the caller cannot modify the counter, and you only need to modify the function slightly:
int countRecursive(counterRecParam crp)
{
if(crp.cancelCondition > 0)
{
--crp.cancelCondition;
++crp.counter;
return countRecursive(crp);
}
else
{
return crp.counter;
}
}
And the implicit conversion lets you call it with an int
counterRecursive(5);
One way to do this is to use a functor. Here's a simple example:
#include <iostream>
class counter
{
public:
unsigned operator()(unsigned m, unsigned n)
{
// increment the count on every iteration
++count;
// rest of the function
if (m == 0)
{
return n + 1;
}
if (n == 0)
{
return operator()(m - 1, 1);
}
return operator()(m - 1, operator()(m, n - 1));
}
std::size_t get_count() const
{
return count;
}
private:
// call count
std::size_t count = 0;
};
int main()
{
auto f = counter();
auto res = f(4, 0);
std::cout << "Result: " << res << "\nNumber of calls: " << f.get_count() << std::endl;
return 0;
}
Output:
Result: 13
Number of calls: 107
Since the count is stored in the object itself, the user cannot overwrite it.
Have you tried using "static" counter variable. Static variables gets initialized just once, and are best candidates to be used as counter variables.

replace all negative value from array using recursion C

I want replace all negative value by zero(recursively). And I have use C and recursion. It's was my homework.
Desired output:
0 4 0 3
What I get:
0 4 -9 3
My code:
#include <stdio.h>
int zeros_value(int n, int tab[])
{
if (n==0) return 0;
if(tab[n-1] < 0){
tab[n-1]=0;
}
else{
return zero_value(n-1,tab);
}
}
int main(void)
{
int tab[4] = {0,4,-9,3};
int number = 0;
int i;
zero_value(4, tab);
for(i=0;i<4;i++)
printf("%d ", tab[i]);
return 0;
}
When you hit the first negative, the recursion doesn't continue anymore and the function returns. You don't really need to return any value from the function. You can rewrite it to make a void function.
#include <stdio.h>
void zero_value(int n, int tab[])
{
if (n==0) return;
if(tab[n-1] < 0) tab[n-1]=0;
zero_value(n-1,tab);
}
int main(void)
{
int tab[4] = {0,4,-9,3};
int number = 0;
int i;
zero_value(4, tab);
for(i=0;i<4;i++)
printf("%d ", tab[i]);
return 0;
}
I see the following problems with your code.
The function zero_values does not have a valid return statement when tab[n-1] is negative. You can see it more clearly if you change the function to:
int zeros_value(int n, int tab[])
{
if (n==0)
{
return 0;
}
if(tab[n-1] < 0)
{
tab[n-1]=0;
// No return here.
}
else
{
return zero_value(n-1,tab);
}
// No return here either.
}
Calling such functions leads undefined behavior.
The printf line in main is not right.
printf("%d%d%d%d", zeros_value(4,tab));
That line needs four arguments of type int after the format string to work correctly. Not providing enough arguments to printf is also cause for undefined behavior.
You can use solution provided in the answer by #usr to solve both problems.
If you have any valid reasons to return an int from zero_value, you need to change the implementation appropriately. It's not clear from your post what that return value is supposed to be.

C++ Calling function inside another function that uses references and parameters

I'm programming at Arduino IDE (that uses basically C++) and I'm in trouble to call a function inside another. The part of code is below:
unsigned long int deslocamento (char sentido, byte &cont, const int pino, unsigned long int posicaoA)
{
byte leitura;
unsigned int deltaposicao;
leitura = digitalRead(pino);
if ((leitura == HIGH) && (cont == 0))
{
cont = 1;
}
if ((leitura == LOW) && (cont == 1))
{
cont = 0;
deltaposicao++;
}
if (sentido == 'F')
{
posicaoA += deltaposicao;
}
else
{
posicaoA -= deltaposicao;
}
return posicaoA;
}
void zeramento (unsigned long int posicaoA)
{
byte pwm = 255;
char sentido = 'R';
byte fator;
fator = fatorcorrecaoP (pwm);
while (posicaoA != 0)
{
posicaoA = deslocamento (sentido, cont, pinencoder, posicaoA);
posicaoA -= fator;
comando (sentido, pwm);
}
}
On function "void zeramento" should I declare as inputs all the inputs (parameters) that function "unsigned long int deslocamento" uses too or have an easier and shorter (or maybe a more efficient in therms of memory optimization) way to do that? For example, should I declare as "void zeramento(posicaoA, sentido, &cont, pinecoder)"?
Thanks for all and sorry for any problem. I'm new here and still learning English.
It depends on what you are trying to achieve. For a shorter code, I'll just call your functions funcA and funcB and the parameters par1 and par2.
You can do either
void funcA(int par1, int par2)
{ ... use par1 and par2 ... }
void funcB(int par1, int par2)
{
...
funcA(par1, par2);
...
}
or
int global_par1;
int global_par2;
void funcA(int par1, int par2)
{ ... use par1 and par2 ... }
void funcB()
{
...
funcA(global_par1, global_par2);
...
}
In the first case you only use local variables, while in the second you use global ones.
The difference is that in the global case (please call them differently in funcA and funcB, since there could be readability issues) the variables are unique for the whole program (i.e. if you modify them in the main you will modify them even in the functions), while in the local case you are just working on a local copy of them.
In my opinion, since these variables identify the position (which is unique) I'd go with a fully global solution, i.e.
int global_par1;
int global_par2;
void funcA()
{ .. use global_par1 and global_par2.. }
void funcB()
{
...
funcA();
...
}

Runtime error SIGSEGV infix to postfix

This code works fine on my machine but when i upload it to codechef it gives me a runtime error SIGSEGV. Can anyone please point out the error in my code? This is the question i made it for http://www.codechef.com/problems/ONP/
#include<iostream>
#include<string>
using namespace std;
class stack
{
public:
void push(char a)
{
++top;
arr[top]=a;
}
void pop()
{
top--;
}
void initialize(int size)
{
top=-1;
max=size;
}
bool chckfull()
{
return (top==max-1);
}
bool chckempty()
{
return (top==-1);
}
char front()
{
return arr[top];
}
private:
int top;
int max;
char arr[404];
};
int chckalphanum(char y)
{
if((y>='a')&&(y<='z'))
return 1;
else if ((y>='A')&&(y<'Z'))
return 1;
else if((y>='0')&&(y<='9'))
return 1;
return 0;
}
int pre(char x)
{
if(chckalphanum(x))
return 0;
if(x=='(')
return -1;
else if(x=='^')
return 3;
else if((x=='/')||(x=='*'))
return 2;
else
return 1;
}
int main ()
{
std::ios::sync_with_stdio(false);
string s, s1=")";
char q[404];
int qmax=0,t;
stack prs;
scanf("%d", &t);
while(t--)
{
cin>>s;
prs.initialize(s.length());
prs.push('(');
s=s+s1;
for(int i=0; i<s.length(); i++)
{
if(s[i]=='(')
prs.push('(');
else if(chckalphanum(s[i]))
{
q[qmax]=s[i];
qmax++;
}
else if(s[i]==')')
{
while(prs.front()!='(')
{
q[qmax]=prs.front();
qmax++;
prs.pop();
}
prs.pop();
}
else
{
while(pre(prs.front())>=pre(s[i]))
{
q[qmax]=prs.front();
qmax++;
prs.pop();
}
prs.push(s[i]);
}
}
for(int i=0; i<qmax; i++)
cout<<q[i];
cout<<"\n";
qmax=0;
}
return 0;
}
I just commented out the below line from your solution and it got accepted in codechef.
std::ios::sync_with_stdio(false);
I am not sure if you are aware of what the above line does but I will try to explain to the best of my knowledge. Better answers will definitely follow in due course from the community.
"With stdio synchronization turned off, iostream standard stream objects may operate independently of the standard C streams (although they are not required to), and mixing operations may result in unexpectedly interleaved characters."
Quoting from cppreference.
"Concurrent access to the same stream object may cause data races."
Since you have turned off the synchronization between stdio (C style I/O) and iostream (C++ style I/O)
and you continued using scanf and cin simultaneously interleaved, I suspect you got a runtime error.
For more research, please go through : http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/
Hope it clarifies a bit, if not fully. Thanks!

How can I return an array?

Is there any way to return an array from a function? More specifically, I've created this function:
char bin[8];
for(int i = 7; i >= 0; i--)
{
int ascii='a';
if(2^i-ascii >= 0)
{
bin[i]='1';
ascii=2^i-ascii;
}
else
{
bin[i]='0';
}
}
and I need a way to return bin[].
You can't do that but you can:
return a dynamicaly allocated array - best owned by a smart pointer so that the caller does not have to care about deallocating memory for it - you could also return something like an std::vector this way.
populate an array/vector passed to you as an argument by pointer (suggested) or a non const reference.
Your array is a local variable allocated on the stack. You should use new [] to allocate it on the heap. Then you can just say: return bin;. Beware that you will have to explicitly free it with delete [] when you are done with it.
You are really asking the wrong question. If you want to do string processing in C++, use the std::string and/or std::vector classes, not arrays of char. Your code then becomes:
vector <char> func() {
vector <char> bin(8);
for( int i = 7; i >= 0; i-- ) {
int ascii='a';
if ( 2 ^ i - ascii >= 0 ) {
bin[i] = '1';
ascii = 2^i - ascii;
}
else {
bin[i] ='0';
}
}
return bin;
}
I think your best bet is to use a vector. It can function in many ways like an array and has several upsides (length stored with type, automatic memory management).
void Calculate( std::vector<char>& bin) {
for(int i = 7; i >= 0; i--)
{
int ascii='a';
if(2^i-ascii >= 0)
{
bin.push_back('1');
ascii=2^i-ascii;
}
else
{
bin.push_back('0');
}
}
}
If you want to return a copy of the array (might make sense for small arrays) and the array has fixed size, you can enclose it in a struct;
struct ArrayWrapper {
char _bin[8];
};
ArrayWrapper func()
{
ArrayWrapper x;
// Do your stuff here using x._bin instead of plain bin
return x;
}
Or just use a std::vector as has been already suggested.
Similar implemented to #ari's answer, i want to say there is already a boost solution, boost::array solving your problem:
boost::array<char, 8> f() {
boost::array<char, 8> bin;
for(int i = 7; i >= 0; i--) {
int ascii = 'a';
if(2 ^ i-ascii >= 0) {
bin[i] = '1';
ascii = 2 ^ i-ascii;
} else {
bin[i] = '0';
}
}
}
...
boost::array<char, 8> a(f());
[I'm not sure what you want to do with that algorithm though, but note that i think you want to do 1 << i (bit-wise shift) instead of 2 ^ i which is not exponentiation in C++.]
Boost array is a normal array, just wrapped in a struct, so you lose no performance what-so-ever. It will also be available in the next C++ version as std::array, and is very easy to do yourself if you don't need the begin()/size()/data()-sugar it adds (to be a container). Just go with the most basic one:
template<typename T, size_t S>
struct array {
T t[S];
T& operator[](ptrdiff_t i) { return t[i]; }
T const& operator[](ptrdiff_t i) const { return t[i]; }
};
But as usual, use the tools already written by other people, in this case boost::array. It's also got the advantage of being an aggregate (that's why it has no user declared constructor), so it allows initializing with a brace enclosed list:
boost::array<int, 4> a = {{ 1, 2, 3, 4 }};
you need to pass array bin as an argument in your function.
array always pass by address, therefore you dont need to return any value.
it will automatically show you all changes in your main program
void FunctionAbc(char bin[], int size);
void FuncationAbc(bin, size)
{
for(int i = 7; i >= 0; i--)
{
int ascii='a';
if(2^i-ascii >= 0)
{
bin[i]='1';
ascii=2^i-ascii;
}
else
{
bin[i]='0';
}
}
}
You'll want to pass by reference, as follows:
void modifyBin(char (&bin)[8])
{
/* your function goes here and modifies bin */
}
int main()
{
char bin[8];
modifyBin(bin);
/* bin has been updated */
return 0;
}
I think that everyone else answered this one... use a container instead of an array. Here's the std::string version:
std::string foo() {
int ascii = 'a';
std::string result("00000000");
for (int i=7; i>=0; --i) {
if (2^i-ascii >= 0) {
result[i] = '1';
ascii = 2^i-ascii;
}
}
return result;
}
I'm not really sure if 2^i-ascii is want you want or not. This will be parsed as (2 ^ (i - ascii)) which is a little strange.