Ok, I'm not sure what this problem is, but I think it may be a result of a memory allocation issue. I'm not even sure it's .dll related but since I am using a .dll I figured there's a good shot that it is, especially as I am a terrible c++ coder (I didn't actually do most of the coding on it because I know very little about c++)
The project is a planetary model and works as follows:
-I generate data for planets, either randomized or user-input.
-The data is sent to a constructor in the .dll for objects there that have the planet data (mass,position,speed,name).
-The data is updated in this code here:
void DLL run()//moves the simulation forward by one timestep.--currently 1 second
{
for(int x = 0;x<10;x++)
{
accelx[x]= 0.0;
accelz[x]= 0.0;
}
for(int x = 0;x+1<filled;x++)//The main loop-will cycle through for each object
{
float fx= 0;
float fz= 0;
for(int y = x+1;(y<filled);y++)
{
float px = (planets[x].position_x - planets[y].position_x);
float pz = (planets[x].position_z - planets[y].position_z);
float d = (px * px) + (pz * pz);
float distance = sqrt(d)/100.0;
float gfnet = (planets[x].mass * planets[y].mass * g) /(distance*distance);
fx = ((gfnet * (planets[y].position_x - planets[x].position_x))/(distance));
fz = ((gfnet * (planets[y].position_z - planets[x].position_z))/(distance));
accelx[x] = accelx[x] + (fx/planets[x].mass);
accelz[x] = accelz[x] + (fz/planets[x].mass);
accelx[y] = accelx[y] - (fx/planets[y].mass);
accelz[y] = accelz[y] - (fz/planets[y].mass);
}
}
for(int w = 0;w<filled;w++)
{
planets[w].speed_x=planets[w].speed_x+accelx[w]/100000000;
planets[w].speed_z=planets[w].speed_z+accelz[w]/100000000;
planets[w].position_x=(planets[w].position_x+planets[w].speed_x);
planets[w].position_z=(planets[w].position_z+planets[w].speed_z);
}
return;
-The data is sent back to the program with these Functions:
DWORD DLL position_x(int num) //Returns the x position of num in the array
{
return *((DWORD*)&planets[num].position_x);
}
DWORD DLL position_z(int num) //Returns the z position of num in the array
{
return *((DWORD*)&planets[num].position_z);
}
-The position data is used to update graphical object positions and is outputted to a file
The problem is that object positions are ending up all at bizzare values such as:
Object:1
xpos:1.15081e+009
zpos:1.16872e+009
They don't update, they jump to that value instantly after 1 run of the program, and they are what looks like completely random numberes. The weirdest part of this problem is that it keeps surfacing randomly, and I've managed to make it go away a few times, but I don't know how I did that. I know one time I rewrote some code in the DB program and it worked fine, but that isn't working anymore. I've done debugging at every point and I can't even find out where the problem is, because whenever I type in the call dll commands as it appears in the program it gives different (sometimes correct) results, whereas the program running gives completely wacky things.
Please can someone shed some light as to what may be going on before I cry.