This is a question about the ++ operator. It is a question that doesn't solve a problem but I believe that it would be very informative to know the answer. So, while StackOverflow is a source of information, I would like to append the available knowledge here!
So, the problem:
int i = 0;
int getNextInt(){
return i++;
}
This is it. The question is how the ++ operator is implemented in the function call stack and where the increment actually happens when we call that function in our code.
I am really wondering a long time!
It happens after the function returns it's value. In this case, it returns 0 and then increments the variable, so the next time it will return 1
I have no idea what the compiler actually does with regard to the stack, but if it chose to, the compiler could easily rearrange it to this:
int getNextInt() {
int temp = i;
i++;
return temp;
}
i++ is the same as saying i = i+1
It is just a shorthand way of redefining the variable.
Related
Everybody knows it is good programming practice to keep the stack balanced. What I'm wondering, though, is whether I'm allowed to modify stack values in a C function called from a Lua script? Consider the following code:
int myfunc(lua_State *L)
{
int arg1 = luaL_checkinteger(L, 1);
int arg2 = luaL_checkinteger(L, 2);
// pop arg 2
lua_pop(L, 1);
// this is to be our return value
lua_newtable(L);
...do complicated stuff...
// restore second parameter but set it to nil for convenience's sake
lua_pushnil(L);
lua_insert(L, 2);
// return our table
return 1;
}
So the code above replaces the second parameter with nil. Is this allowed or do I have to restore the original value, i.e. would I have to do
lua_pushinteger(L, arg2);
instead of
lua_pushnil(L);
? Or doesn't this matter as long as myfunc returns with the stack balanced?
The stack values are the property of the C function being called. You can do whatever you wish with them. The only effect to the outside is the values returned by the function.
C functions called from Lua do not need to keep the stack balanced, that is, with the same contents or number of items it had on entry.
Imagine a gfor with a seq j...
If I need to use the value of the instance j as a index, who can I do that?
something like:
vector<double> a(n);
gfor(seq j, n){
//Do some calculation and save this on someValue
a[j] = someValue;
}
Someone can help me (again) ?
Thanks.
I've found a solution for this...
if someone had a better option, feel free to post...
First, create a seq with the same size of your gfor instances.
Then, convert that seq in a array.
Now, take the value of that line on array (it's equals the index)
seq sequencia(0, 200);
af::array sqc = sequencia;
//Inside the gfor loop
countLoop = (int) sqc(j).scalar<float>();
Your approach works, but breaks gfors parallelization as converting the index to a scalar forces it to be written from the gpu back to the host, slamming the breaks on the GPU.
You want to do it more like this :
af::array a(200);
gfor(seq j, 200){
//Do some calculation and save this on someValue
a[j] = af::array(someValue); // for someValue a primitive type, say float
}
// ... Now we're safe outside the parallel loop, let's grab the array results
float results[200];
a.host(results) // Copy array from GPU to host, populating a c-type array
if(CGRectIntersectsRect(m_Heart, m_sBirdSprite.boundingBox)){
m_sHeart[i].visible=FALSE;
m_fTime += 1;
}
This is my code, and the problem is that i want to add only one value in m_fTime variable, but it is continuously adding +1 in m_fTime varible because it is in process to be intersecting, now is there any way to add only 1 value in it when it intersects?
You can use BOOL porperty
if(CGRectIntersectsRect(m_Heart, m_sBirdSprite.boundingBox) && [m_sHeart[i] isVisible]){
m_sHeart[i].visible=FALSE;
m_fTime += 1;
}
thanks for your kind answer #Himanshu Joshi its correct in terms of logic, but bit incorrect in terms of syntax i did try the same, i am not pointing your syntax wrong, just giving the correct syntax for our new/beginner friends. and i am marking your question as right answer.
if(CGRectIntersectsRect(m_sHearts, m_sBirdSprite.boundingBox) && m_sHeart[i].visible==TRUE){
m_sHeart[i].visible=FALSE;
m_fTime += 1;
}
In expert programming a simple way to use Boolean flags.
Same here you can implement the Boolean flags in your problem,
if(CGRectIntersectsRect(m_Heart, m_sBirdSprite.boundingBox) && [m_sHeart[i] isVisible]){
m_sHeart[i].visible=true;
m_fTime += 1;
}
I'm currently building a prime number finder, and am having a memory problem:
This may be due to a corruption of the heap, which indicates a bug in PrimeNumbers.exe or any of the DLLs it has loaded.
PS. Please don't say to me if this isn't the way to find prime numbers, I want to figure it out myself!
Code:
// PrimeNumbers.cpp : main project file.
#include "stdafx.h"
#include <vector>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Until what number do you want to stop?");
signed const int numtstop = Convert::ToInt16(Console::ReadLine());
bool * isvalid = new bool[numtstop];
int allattempts = numtstop*numtstop; // Find all the possible combinations of numbers
for (int currentnumb = 0; currentnumb <= allattempts; currentnumb++) // For each number try to find a combination
{
for (int i = 0; i <= numtstop; i++)
{
for (int tnumb = 0; tnumb <= numtstop; tnumb++)
{
if (i*tnumb == currentnumb)
{
isvalid[currentnumb] = false;
Console::WriteLine("Error");
}
}
}
}
Console::WriteLine(L"\nAll prime number in the range of:" + Convert::ToString(numtstop));
for (int pnts = 0; pnts <= numtstop; pnts++)
{
if (isvalid[pnts] != false)
{
Console::WriteLine(pnts);
}
}
return 0;
}
I don't see the memory problem.
Please help.
You are allocating numtstop booleans, but you index that array using a variable that ranges from zero to numtstop*numtstop. This will be severely out of bounds for all numstop values greater than 1.
You should either allocate more booleans (numtstop*numtstop) or use a different variable to index into isvalid (for example, i, which ranges from 0 to numstop). I am sorry, I cannot be more precise than that because of your request not to comment on your algorithm of finding primes.
P.S. If you would like to read something on the topic of finding small primes, here is a link to a great book by Dijkstra. He teaches you how to construct a program for the first 1000 primes on pages 35..49.
Problem is that you use native C++ in managed C++/CLI code. And use new without delete of course.
`currentnumb` :
is bigger than the size of the array, which is just numtstop. You are probably going out of bound, this might be your issue.
You never delete[] your isvalid local, this is a memory leak.
I need to write a job where i could fetch the index of an array element of EDT Dimension
e.g. In my EDT Dimension i have array elements A B C when i click over them for properties I see the index for A as 1, B as 2 and C as 3. Now with a job ui want to fetch the index value. Kindly Assist.
I'm not sure if I did understand the real problem. Some code sample could help.
The Dimensions Table has some useful methods like arrayIdx2Code.
Maybe the following code helps:
static void Job1(Args _args)
{
Counter idx;
Dimension dimension;
DimensionCode dimensionCode;
str name;
;
for (idx = 1; idx <= dimof(dimension); idx++)
{
dimensionCode = Dimensions::arrayIdx2Code(idx);
name = enum2str(dimensionCode);
// if (name == 'B') ...
info(strfmt("%1: %2", idx, name));
}
}
I found a way but still looking if there is any other solution.
static void Job10(Args _args)
{
Dicttype dicttype;
counter i;
str test;
;
test = "Client";
dicttype = new dicttype(132);//132 here is the id of edt dimension
for (i=1;i<=dicttype.arraySize();i++)
{
if ( dicttype.label(i) == test)
{
break;
}
}
print i;
pause;
}
Array elements A B C from your example are nothing else but simple labels - they cannot be used as identifiers. First of all, for user convenience the labels can be modified anytime, then even if they aren't, the labels are different in different languages, and so on and so forth.
Overall your approach (querying DictType) would be correct but I cannot think of any scenario that would actually require such a code.
If you clarified your business requirements someone could come up with a better solution.