I have and QT application with class Solver that solve some numeric problem (Bairstow method for finding roots of the polynomial by finding its distribution to trinomials) but while for smaller instances (5 parameters, in array tabA) it work fine, but when I tried this for larger instances (7 parameters) application crashed.
After I run the debugger I get the following message:
Heap block at 02989F58 modified at 02989F80 past request 20
I'm not exactly sure what is about (well I suppose I get stack overflow but I'm not sure where and how) but it points to line delete[] Q; delete[] W; here is how debugger pointed it:
And here is code of that class methods (the error occurs in main method int Bairstow(*parameters*), which work on class fields and returns number which indicate is solution was found, is not existing or can't be found in given number of iteration)
Here's the main method:
int solver::Bairstow(int stopien,double const *tabA, double *tabP,double *tabR, double eps, int N, double p_,double r_)
{
int i,q, Iter, indpziel=0;
i=stopien;
while(i>=0 && tabA[i]==0)
{
i--;
}
if (i<2) { return 1; }
double *A=new double[stopien +1]
for (i=0;i<=stopien;i++)
{
A[i]=tabA[i];
}
double *Q=new double[stopien-1 +1];
double *w=new double[stopien-3 +1];
double Reszta[2];
double dqdp[2], dqdr[2];
double p,r, a,b,c,d;
while (stopien>=2)
{
p=p_, r=r_;
Iter=O;
do
{
PodzielWiel(stopien,A,p,r,Q,Reszta);
if (fabs(Reszta[1])<eps && fabs(Reszta[0])<eps)
{
break;
}
PodzielWiel(Stopien-2,Q,p,r,W,dqdr);
for (i=Stopien-2;i>=0;i--)
{
Q[i+1]=Q[i];
}
Q[0]=0;
PodzielWiel(Stopien-1,Q,p,r,W,dqdp);
q=LiczMOdwrotna(dqdp[0],dqdr[0],dqdp[1],dqdr[1],a,b,c,d);
if (q==1)
{
delete[] Q;
delete[] W;
return 2;
}
p = p-(a*Reszta[0]+b*Reszta[1]);
r = r-(c*Reszta[0]+d*Reszta[1]);
} while (++Iter<N);
if (Iter==N) return 3;
tabP[IndDziel]=p;
tabR[IndDziel]=r;
IndDziel++;
Stopien-=2;
for (i=0;i<=Stopien;i++)
{
A[i]=Q[i];
}
}
delete[] Q; delete[] W; //that's the line debugger pointed to
return 0;
}
And two helper methods:
for polynomial division (by trinomial x^2-px-r)
int solver::PodzielWiel(int stopien,double const *tabA, double p,double r, double *Q,double *R)
{
if (Stopien<O) return 1;
int i;
for (i=0;i<=stopien-2;i++)
{
Q[i]=0;
}
while (stopien>=0 && tabA[stopien]==0)
{
stopien--;
}
if (stopien<2)
{
R[0]=tabA[0];
R[1]=tabA[1];
return 0;
}
double *A=new double[Stopien +1];
for (i=0;i<=stopien;i++)
{
A[i]=tabA[i];
}
Q[stopien-2]=A[stopien];
if (stopien>2)
{
for(i=stopien; i>1; i--)
{
Q[i-2]=A[i];
A[i-1]+=Q[i-2]*p;
A[i-2]+=Q[i-2]*r;
}
R[1]=A[1];
R[0]=A[0] ;
}
else
{
R[1]=A[1]+p*Q[0] ;
R[0]=A[0]+r*Q[0] ;
}
delete [] A;
return 0;
}
for inversing the 2x2 matrix(first 4 parameters are inputs and next 4 outputs as it):
int Solver::LiczMOdwrotna(double x,double y,double w,double z, double &a,double &b,double &c,double &d)
{
if (x*z==y*w)
{
return 1;
}
if (x*w!=0)
{
c=1/(y-z*x/w);
a=-z*c/w;
d=1/(z-y*w/x);
b=-y*d/x;
}
else if (y*z!=0)
{
a=1/(x-w*y/z);
c=-w*a/z;
b=1/(w-z*x/y);
d=-x*b/y;
}
else if (x==0 && z==0)
{
c=1/y;
d=0;
a=0;
b=1/w;
}
else if (y==0 && w==0)
{
a=1/x;
b=0;
c=0;
d=1/z;
}
return 0;
}
And sory for maybe poor formatting but I have to use OCR software as copying from QT creator was impossible... even after using show in explorer, save file as txt in new localization (to make it visible outside QT Creator) and then doing it again... I still couldn't copy anything...
I tried navily to add a vector to a vector of vectors inside a function:
the function gets a vector of vectors as a pointer (called r) and it adds to r another vector.
std::vector<double> b;
b.push_back(0);
b.push_back(0);
b.push_back(0);
b.push_back(v0*sin(theta));
b.push_back(0);
b.push_back(v0*cos(theta));
b.push_back(0);
cout<<"befor"<<endl;
(*r).push_back(b);
cout<<"after"<<endl;
but the function crashes when it tries to add b to r. I believe it's because b is an internal object, but why the programe crashes when it tries to add b and not after (when it ends the function) ? to check this I added a print line "before" and "after" (it prints "before" but not "after" and then it crashes).
Can anyone explane me what it the best way to add a vectors to a vector of vectors inside a function?
The fully function looks like this:
void solve_all(double v0,double theta,double omega,double phi,double
dt_in,double total_time,std::string solver, double eps_in,
std::vector<std::vector<double>>* r)
{
double delta,eta,miu,diff,time,eps_f,dt_f,eps_t;
std::vector<double> r_1(7),r_2(7),r_temp(7);
int i,good;
mone_f=0;
std::vector<double> b;
b.push_back(0);
b.push_back(0);
b.push_back(0);
b.push_back(v0*sin(theta));
b.push_back(0);
b.push_back(v0*cos(theta));
b.push_back(0);
cout<<"befor"<<endl;
(*r).push_back(b);
cout<<"after"<<endl;
i=1;
time=b[6];
dt_f=dt_in;
eps_f=eps_in;
delta=eps_f*dt_f/(total_time-time);
eta=delta/4;
miu=delta/2;
eps_t=1e-10;
while(total_time-time>eps_t)
{
cout<<time<<endl;
good=0;
while(good==0)
{
if(dt_f>total_time-time){dt_f=total_time-time;}
solve(&b,&r_1,dt_f,omega,phi,solver);
solve(&b,&r_temp,dt_f/2,omega,phi,solver);
solve(&r_temp,&r_2,dt_f/2,omega,phi,solver);
diff=0;
for(int j=0; j<=5; j++)
{
double subs;
subs=r_2[j]-r_1[j];
if(subs<0){subs=subs*(-1);}
if(subs>diff){diff=subs;}
}
if(diff>delta)
{
dt_f=dt_f/2;
}
else
{
if(miu<diff&&diff<delta)
{
good=1;
dt_f=0.9*dt_f;
}
else
{
if(eta<diff&&diff<miu)
{
good=1;
}
else
{
if(eta>diff)
{
good=1;
dt_f=1.1*dt_f;
}
}
}
}
i++;
b.clear();
if(solver.compare("RK2"))
{
for(int j=0; j<=6;i++)
{
b.push_back((4*r_2[j]-r_1[j])/3);
}
}
if(solver.compare("RK3"))
{
for(int j=0; j<=6;i++)
{
b.push_back((8*r_2[j]-r_1[j])/7);
}
}
(*r).push_back(b);
eps_f=eps_f-diff;
time=b[6];
delta=eps_f*dt_f/(total_time-time);
eta=delta/4;
miu=delta/2;
}
}
}
related functions:
double big_f(double v)
{
return (0.0039+0.0058/(1+exp((v-35)/5)));
}
double f1(double vx,double vy,double vz,double omega ,double phi)
{
double v;
mone_f++;
v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
return (-big_f(v)*v*vx+B*omega*(vz*sin(phi)-vy*cos(phi)));
}
double f2(double vx,double vy,double vz,double omega ,double phi)
{
double v;
mone_f++;
v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
return (-big_f(v)*v*vy+B*omega*(vx*cos(phi)));
}
double f3(double vx,double vy,double vz,double omega ,double phi)
{
double v;
mone_f++;
v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
return (-g-big_f(v)*v*vz-B*omega*(vx*sin(phi)));
}
void solve(std::vector<double>* r,std::vector<double>* r_new,double
l,double omega, double phi, std::string solver)
{
double vx,vy,vz,vx2,vy2,vz2,vx3,vy3,vz3;
if (solver.compare("RK2")==0)
{
vx=(*r)[3];
vy=(*r)[4];
vz=(*r)[5];
vx2=vx+0.5*l*f1(vx,vy,vz,omega,phi);
vy2=vy+0.5*l*f2(vx,vy,vz,omega,phi);
vz2=vz+0.5*l*f3(vx,vy,vz,omega,phi);
(*r_new).push_back((*r)[0]+l*vx2);
(*r_new).push_back((*r)[1]+l*vy2);
(*r_new).push_back((*r)[2]+l*vz2);
(*r_new).push_back((*r)[3]+l*f1(vx2,vy2,vz2,omega,phi));
(*r_new).push_back((*r)[4]+l*f2(vx2,vy2,vz2,omega,phi));
(*r_new).push_back((*r)[5]+l*f3(vx2,vy2,vz2,omega,phi));
(*r_new).push_back((*r)[6]+l);
}
if(solver.compare("RK3")==0)
{
vx=(*r)[3];
vy=(*r)[4];
vz=(*r)[5];
vx2=vx+0.5*l*f1(vx,vy,vz,omega,phi);
vy2=vy+0.5*l*f2(vx,vy,vz,omega,phi);
vz2=vz+0.5*l*f3(vx,vy,vz,omega,phi);
vx3=vx+2*l*f1(vx2,vy2,vz2,omega,phi)-l*f1(vx,vy,vz,omega,phi);
vy3=vy+2*l*f2(vx2,vy2,vz2,omega,phi)-l*f2(vx,vy,vz,omega,phi);
vz3=vz+2*l*f3(vx2,vy2,vz2,omega,phi)-l*f3(vx,vy,vz,omega,phi);
(*r_new).push_back((*r)[0]+l*(vx+4*vx2+vx3)/6);
(*r_new).push_back((*r)[1]+l*(vx+4*vx2+vx3)/6);
(*r_new).push_back((*r)[2]+l*(vx+4*vx2+vx3)/6);
(*r_new).push_back((*r)[3]+l(f1(vx,vy,vz,omega,phi)+4*f1(vx2,vy2,vz2,omega,phi)+f1(vx3,vy3,vz3,omega,phi))/6);
(*r_new).push_back((*r)[4]+l*(f2(vx,vy,vz,omega,phi)+4*f2(vx2,vy2,vz2,omega,phi)+f2(vx3,vy3,vz3,omega,phi))/6);
(*r_new).push_back((*r)[5]+l*(f3(vx,vy,vz,omega,phi)+4*f3(vx2,vy2,vz2,omega,phi)+f3(vx3,vy3,vz3,omega,phi))/6);
(*r_new).push_back((*r)[6]+l);
}
}
If you take a reference std::vector<std::vector<double>>& or pointer std::vector<std::vector<double>>* you can push_back as many and any std::vector<double> you want. It doesn't matter if you're pushing back stack variables, push_back creates a copy.
Your problem is probably that your r pointer is invalid. Try checking that it isn't a nullptr, or if it was pointing to a stack variable that fell out of scope - in this case it does matter. Alternatively, and preferably, change your function definition to take a reference and see if the compiler complains.
I've a program which inserts all prime numbers up to a specific number in an array.
The calculation is correct. My problems are the function parameters and the transfer of my dynamic array to the function. My function doesn't modify my array.
Please take a look at the code:
#include <iostream>
using namespace std;
int primeinlesen(int *i);
int primarrayspeicherung (int *primarray,int *bis);
int main()
{
int reload=1;
while(reload==1)
{
int bis=0,*primarray,valcounter;
primeinlesen(&bis);
valcounter=primarrayspeicherung(primarray,&bis);
for(int i=0;i<valcounter;i++)
{
cout<<i<<". Primzahl: "<<primarray[i]<<"\n";
}
delete [] primarray;
cout<<"Anzahl Primzahlen: "<<valcounter<<endl;
cout<<"Erneute Berechnung?(Ja(1) oder Nein(0))";
cin>>reload;
}
return 0;
}
int primeinlesen(int *i)
{
cout<<"Bis zu welchem Wert moechten SiePrimzahlen ausgegeben,haben(max.500)";
cin>>*i;
if(*i>500)
{
cout<<"Wert zu hoch...";
}
return 0;
}
int primarrayspeicherung (int *primarray,int *bis)
{
int x,y,counter,e,valcounter=0,xcounter=0,xvalcounter=0,xx,xy,xe;
for(x=2;x<*bis;x++)
{
counter=0;
for(y=2;y<x;y++)
{
e=x%y;
if(e==0)
{
counter++;
}
}
if(counter==0)
{
valcounter++;
}
}
//ZWEITER DURCHGANG
primarray=new int[valcounter];
for(xx=2;xx<*bis;xx++)
{
xcounter=0;
for(xy=2;xy<xx;xy++)
{
xe=xx%xy;
if(xe==0)
{
xcounter++;
}
}
if(xcounter==0)
{
primarray[xvalcounter]=xx;
xvalcounter++;
}
}
return valcounter;
}
Best regards
In this function:
int primarrayspeicherung (int *primarray,int *bis)
primarray is a local variable. Everything you're doing to it (e.g. allocating, assigning) only affects the local primarray, not the one you pass in. If you want to modify both, you need to pass in a reference:
int primarrayspeicherung (int*& primarray,int *bis)
I am trying to implement Newton Raphson in C++.
Approach: In my root() function my while(temp-e>0) exists even when temp==e. I know it is due to using doubles and comparing double might give precision errors sometimes. But still I want to know how can I make sure that the loop exists only when temp==0. Error occurring in the test case 64. My code is returning 4.000001, while it should return 4.000000.
Code:
#include<stdio.h>
double root(int n)
{
double x=n,a,b,e=0.000001,temp;
a=2*x;
b=n/(x*x);
temp=1;
while(temp-e>0)
{
x=(a+b)/3;
a=2*x;
b=n/(x*x);
temp=(n/(x*x)-x)/3;
temp=temp<0?-temp:temp;
}
return x;
}
int main()
{
printf("%lf\n",root(64));
return 0;
}
While this code is giving correct answer, where I am doing two more iterations from where temp starts failing:
#include<stdio.h>
double root(int n)
{
double x=n,a,b,e=0.000001,temp;
a=2*x;
b=n/(x*x);
int i=0;
temp=1;
while(i<=2)
{
x=(a+b)/3;
a=2*x;
b=n/(x*x);
temp=(n/(x*x)-x)/3;
temp=temp<0?-temp:temp;
if(temp<e)
i++;
}
return x;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%lf\n",root(n));
}
return 0;
}
I wish my first post wasn't so newbie. I've been working with openframeworks, so far so good, but as I'm new to programming I'm having a real headache returning the right value from an int function. I would like the int to increment up until the Boolean condition is met and then decrement to zero. The int is used to move through an array from beginning to end and then back. When I put the guts of the function into the method that I'm using the int in, everything works perfectly, but very messy and I wonder how computationally expensive it is to put there, it just seems that my syntactic abilities are lacking to do otherwise. Advice appreciated, and thanks in advance.
int testApp::updown(int j){
if(j==0){
arp =true;
}
else if (j==7){
arp = false;
}
if(arp == true){
j++;
}
else if(arp == false){
j--;
}
return (j);
}
and then its called like this in an audioRequest block of the library I'm working with:
for (int i = 0; i < bufferSize; i++){
if ((int)timer.phasor(sorSpeed)) {
z = updown(_j);
noteOut = notes [z];
cout<<arp;
cout<<z;
}
EDIT: For addition of some information. Removed the last condition of the second if statement, it was there because I was experiencing strange happenings where j would start walking off the end of the array.
Excerpt of testApp.h
int z, _j=0;
Boolean arp;
EDIT 2: I've revised this now, it works, apologies for asking something so rudimentary and with such terrible code to go with. I do appreciate the time that people have taken to comment here. Here are my revised .cpp and my .h files for your perusal. Thanks again.
#include "testApp.h"
#include <iostream>
using namespace std;
testApp::~testApp() {
}
void testApp::setup(){
sampleRate = 44100;
initialBufferSize = 1024;
//MidiIn.openPort();
//ofAddListener(MidiIn.newMessageEvent, this, &testApp::newMessage);
j = 0;
z= 0;
state = 1;
tuning = 440;
inputNote = 127;
octave = 4;
sorSpeed = 2;
freqOut = (tuning/32) * pow(2,(inputNote-69)/12);
finalOut = freqOut * octave;
notes[7] = finalOut+640;
notes[6] = finalOut+320;
notes[5] = finalOut+160;
notes[4] = finalOut+840;
notes[3] = finalOut+160;
notes[2] = finalOut+500;
notes[1] = finalOut+240;
notes[0] = finalOut;
ofSoundStreamSetup(2,0,this, sampleRate, initialBufferSize, 4);/* Call this last ! */
}
void testApp::update(){
}
void testApp::draw(){
}
int testApp::updown(int &_j){
int tmp;
if(_j==0){
arp = true;
}
else if(_j==7) {
arp = false;
}
if(arp == true){
_j++;
}
else if(arp == false){
_j--;
}
tmp = _j;
return (tmp);
}
void testApp::audioRequested (float * output, int bufferSize, int nChannels){
for (int i = 0; i < bufferSize; i++){
if ((int)timer.phasor(sorSpeed)) {
noteOut = notes [updown(z)];
}
mymix.stereo(mySine.sinewave(noteOut),outputs,0.5);
output[i*nChannels ] = outputs[0];
output[i*nChannels + 1] = outputs[1];
}
}
testApp.h
class testApp : public ofBaseApp{
public:
~testApp();/* destructor is very useful */
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void newMessage(ofxMidiEventArgs &args);
ofxMidiIn MidiIn;
void audioRequested (float * input, int bufferSize, int nChannels); /* output method */
void audioReceived (float * input, int bufferSize, int nChannels); /* input method */
Boolean arp;
int initialBufferSize; /* buffer size */
int sampleRate;
int updown(int &intVar);
/* stick you maximilian stuff below */
double filtered,sample,outputs[2];
maxiFilter filter1;
ofxMaxiMix mymix;
ofxMaxiOsc sine1;
ofxMaxiSample beats,beat;
ofxMaxiOsc mySine,myOtherSine,timer;
int currentCount,lastCount,i,j,z,octave,sorSpeed,state;
double notes[8];
double noteOut,freqOut,tuning,finalOut,inputNote;
};
It's pretty hard to piece this all together. I do think you need to go back to basics a bit, but all the same I think I can explain what is going on.
You initialise _j to 0 and then never modify the value of _j.
You therefore call updown passing 0 as the parameter every time.
updown returns a value of 1 when the input is 0.
Perhaps you meant to pass z to updown when you call it, but I cannot be sure.
Are you really declaring global variables in your header file? That's not good. Try to use local variables and/or parameters as much as possible. Global variables are pretty evil, especially declared in the header file like that!