Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need help with a problem regarding exponential smoothing in C++
The smoothing follows this equation :
newValue = inputSignal*smoothFactor + (1-smoothFactor)*oldValue
My function is supposed to only have one input parameter, that is the inputSignal parameter (the smoothFactor can be declared within the function and is not subject of the problem) and one output parameter, that is the newValue.
Now the issue I am having is that the FIRST calculation is missing an oldValue, since the oldValue is the preceding newValue in the first function call.
So the oldValue has to equal the first inputSignal in the first function call.
That means my function needs to behave different in its first call than every following call. I can solve this if I declare a global i=0 parameter and count up i++ after the first call. This however is NOT a function independent of outside circumstances, which it should be.
I was able to solve the problem with the i=0 and i++ global variables, but fail to find a solution without this.
You can use a local static variable in the function (cf this question). Local static variables are initialized exactely once on the first invocation of the method. So you can use this:
double smooth(double inputSignal) {
static double oldValue = inputSignal; // Executed only once on first invocation
double newValue = inputSignal*smoothFactor + (1-smoothFactor)*oldValue;
oldValue = newValue; // Store it for next invocation
return newValue;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I saw a conclusion that a C++ variadic function using C-style ellipsis can be vulnerable when an attacker designs some kind of input into the function to run arbitrary code. But how can it be done? I mean, how to design an attack vector to exploit the vulnerability?
If you mean a format string exploit to run arbitary code it works as such:
printf allows you to give a format string not matching the argument counts. In such a case variables are pulled off the stack. A function call (like printf) is done by putting all the function's arguments onto the stack, roughly like this:
(low addresses)
^^^^^^ stack growth direction ^^^^^^
---------printf-stack------------
printf function arguments
stack base pointer of caller
return address for when printf() ends
---------printf-stack------------ <-- stack base pointer register (cpu)
---------caller-stack------------
stack variables (int a = 5, char cstr[500] = "hello world!", etc)
caller function arguments
stack base pointer of caller-caller
return address for when caller() ends
---------caller-stack------------
(high addresses)
Reading out more arguments than were given will print the stack base pointer of the calling function, then the return address and eventually variables of the caller function.
To allow arbitrary code execution you need to overwrite the return address of any function. Unfortunately printf allows you to write a variable with %n (which, really is just a bad idea.). This will write the number of characters printed so far to the int pointer provided at the argument's position.
This doesn't seem useful at first, but the user could alter some stack variable to point to the return address on the stack. Then he could print an arbitrary number of characters and use &n to write that number of characters to that stack location (containing the return pointer for the function). The user can place his shellcode whereever and execute by returning into it.
A detailed video by LiveOverflow explaining all this can be found here.
I imagine similar behavior (writing to a pointer given as an argument) can be exploited in other variadic functions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to read the documentation of the DOLFIN c++ library for finite element modelling located on this link:
https://fenicsproject.org/olddocs/dolfin/1.3.0/python/programmers-reference/index.html
but the documentation is hard to read, so for someone without c++ knowledge how you will read the following specification of parameters for the c++ method eval_cell() of the Expression class (https://fenicsproject.org/docs/dolfin/2017.2.0/python/programmers-reference/cpp/function/Expression.html):
Parameters:
double > & values (Array<) – (Array<double>) The values at the point.
Array< double > & x (const) – (Array<double>) The coordinates of the point.
ufc::cell & cell (const) – (ufc::cell) The cell which contains the given point.
After taking a look at the page t.niese linked in the comments I think this is a automatically generated documentation, with a really bad generator (like really really bad).
So, if we fix the butchered first line, realign some braces here and there and fix the position of const it might become clearer:
Parameters
const Array<double>& values1 – The values at the point.
const Array<double> &x – The coordinates of the point.
const ufc::cell &cell – The cell which contains the given point.
Meaning
You are dealing with a function that takes three parameters, the first and second are of type Array<double>, which seems to be generic container. The third parameter is of type ufc::cell, whatever this is. All three parameters are passed by reference (see the & before each variable name) and not by value. But they are not just passed as reference but actually as const reference (see the const), meaning that the function can't modify the objects you give to it.
I can't however say much about the comments for each parameter.
1 I assume the first parameter is also const, because it got the brackets, where the const is noted in the other two parameters, but this is just guessing.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've got a recursive function that is supposed to check if a given word is a palindrome, which works by comparing the first and last value, then strips them off and passes them recursively into checkPalindrome, but whenever I try to assign word[size-1] to '\0', I get a Bus Error.
Here is my code:
bool checkPalindrome(char word[]){
int size = std::strlen(word);
//Parts removed...
word[size-1]='\0'; //This is the line causing the issue
return checkPalindrome(word+1);
}
}
Here is an example of a call to this function:
checkPalindrome("racecar");
Thank you!
There are two ways I can think of to solve this problem:
The obvious one would be to use a std::string instead of a string literal and just .pop_back() (C++11) to remove the last character.
The second would be to pass the length of the string to the function as a parameter, instead of computing it. Then, just decrease the length by 1 so now you have a new "fake end point" to the string. Since you're using this number to check the last character, you don't really need to actually modify the string, just change where the "last" character is.
But since you're using C++, I don't see why not use std::string and avoid overcomplicating the problem.
So as stated in the comments you can't modify a string literal.
So before passing it to the function, make a copy of it.
std::size_t len = std::strlen(word);
char* copy = new char[len + 1];
std::strncpy ( copy, word, len + 1 );
isPalindromeR(copy);
delete[] copy;
other solution would be to not use recursion, thus not needing to modify the string.
anyway outside of assignments use a std::string
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I had try to compile this code but it shows this error "term does not evaluate to a function taking 0 arguments"
I'm completely new to programming so please help me out here.
For reference, the problem function seems to be:
void traps_rand()
{
while (player!=treasure)
srand((unsigned)time(0));
xt1=(rand %6()+1);
xt2=(rand %6()+1);
xt3=(rand %6()+1);
yt1=(rand %8()+1);
yt2=(rand %8()+1);
yt3=(rand %8()+1);
...
...
...
I'm pretty sure this is what you want if you are trying to generate a random number between 1-6:
xt1 = rand() % 6 + 1;
The statement above executes the function rand (as noted by the parentheses), then does modulo 6 on the result before adding 1.
Your original statement:
xt1=(rand %6()+1);
is attempting to invoke the function "6" and use that as the modulus with the address of rand. Then add 1. It hits as error because there is no function named 6. You can't name functions starting with numbers anyway.
Change rand %6()+1 to rand() %6 + 1. It looks like you are using a variable named rand and calling a function named 6(), but what you really want is to call rand() and mod it by 6 (+1).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm getting the error 'term does not evaluate to a function taking 1 arguments' on the following line in my code:
steerToSpiralRefPoint(m_CentrePos);
But I'm not sure why this is... Is it that the function will only take 1 argument as a parameter, but since the 'm_centrePos' variable holds more than 1 attribute, the function is effectively receiving several parameters?
I have defined 'steerToSpiralRefPoint' with the line:
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData aHeloData);
'm_CentrePos' has been assigned the value 'cCentrePos' at the start of this file ('cCentrePos' is a variable of type 'CCoordinate', which has a number of attributes- latitude, longitude, altitude, etc).
'CDirectOverflyItem's also has a number of attributes- ground speed, wind speed, wind angle, etc.
Can anyone point out to me why I'm getting this error, and how I should correct it?
This expression
steerToSpiralRefPoint(m_CentrePos);
is a postfix expression of a function call. However as it follows from your post steerToSpiralRefPoint is not a function (or function pointer) but a pointer to an object. If you want to assign a value to the pointer then you have to write
steerToSpiralRefPoint = m_CentrePos;
Or if there is an operator function for this type then the code should look as
( *steerToSpiralRefPoint )( m_CentrePos );
And this construction
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData );
is also invalid. You may not use qualifiers before variables in expressions. They may be used only in declarations.
It seems that the issue was that I was trying to pass the wrong data type into the parameter- it was expecting a 'CHeloData', but I was trying to give it a 'CCoordinate'.