jungledede | xilebo a écrit :
Absolument pas. Windows te garantit que tu ne vas pas taper dans l'espace d'adressage d'un autre processus, mais dans ton propre processus, tu fais ce que tu veux. Tu peux faire un écrasement mémoire via une autre variable sans problème. Je te donne un exemple :
Code :
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- unsigned int a;
- unsigned int * b ;
- b = (int *)malloc ( 10 * sizeof(unsigned int));
- printf("adresse a : %X\n" , &a );
- printf("adresse b : %X\n" , &b );
- printf("pointeur alloué : %X\n" , b );
- *(&a+1) = 0xDEADBEEF;
- printf("pointeur alloué : %X\n" , b );
-
- free(b);
-
- return 0;
-
- }
|
result :
adresse a : 427F5CB4
adresse b : 427F5CB8
pointeur alloué : 12AC010 pointeur alloué : DEADBEEF
Segmentation fault (core dumped)
|
J'ai écrasé la valeur de ce que j'ai alloué dans b en passant par la variable a
Je dis pas que c'est ce que t'as fait, en général , les écrasements mémoire sont plus subtils, mais le fait est que si ta valeur pointée a changé, c'est pas bon ^^
Pire, un écrasement de pile, et tu peux même pas debugguer avec un point d'arrêt car la pile est perdue.
|
rufo a écrit :
Je pense à truc : tu ne vérifies pas que valeur retour de l'alloc de mémoire comme si tu partais du principe que ça serait forcément alloué. Il faut toujours vérifier que l'alloc a bien fonctionné 
|
ok, je comprends un peut plus. ça confirme le subtile. Surtout que j'ai pas encore compris la "recurrence" du crash. Des fois ça fonctionne, des fois non, mais quand ça marche pas c'est toujours sur un signal "bizarre" (du moins une forme trop éloigné d'une sinus. A par ne pas trouver les index qu'il faut je comprends pas encore le lien de cause à effet). Je verrais lundis avec les debug supplémentaire ce que je peu trouver.
Après je suis pas vraiment programmeur de formation, je me démerde, mais des fois je sens bien que j'implémente des trucs qui sont pas les bonnes pratiques. (Typiquement ça ne fait que 15 jours que je typecast les malloc )
Pour le code entier:
Code :
- int PhaseAngle(eSignals iSignal1, eSignals iSignal2 ,double dFrequency, unsigned short iNumbPairsPoles, double *pdAngle)
- {
- // passage en AC
- //TriggerDetection à 0V
- // 6 period processed
- const int PERIOD2PROCESS = iNumbPairsPoles*1+2;
- double FREQSIGNAL = dFrequency;
- int FREQSAMPLE = DescripteurCourbe[iSignal1].frequenceApparenteEchantillonnage;
- int nbPoint;
- int nbSamplesToCheck = RoundRealToNearestInteger((FREQSAMPLE/FREQSIGNAL)*PERIOD2PROCESS);
- int width = nbSamplesToCheck / (PERIOD2PROCESS*3);
- int startSample = 0;
- double dMedian1,dMedian2;
- const int COEFSUPERSAMPLE = 200;
- const int SUPER_SAMPLES_MAX = nbSamplesToCheck*COEFSUPERSAMPLE;
- double *X = NULL;
- double *dSecondDerivative1 = NULL,*dSecondDerivative2 = NULL;
- int iSuperSample;
- double *dYSuperSampled1 = NULL, *dYSuperSampled2 = NULL;
- int *iIndices1 = NULL;
- int *iIndices2 = NULL;
- const int LINFITSAMPLES = 150*(0.5*COEFSUPERSAMPLE); //20 before
- double *dDataX1 = NULL,*dDataX2 = NULL,*dOut = NULL;
- int iNewStartSample1 = 0, iNewStartSample2 = 0;
- double dA1 = 0, dA2 = 0, dB1 = 0, dB2 = 0, dError1 = 0, dError2 = 0;
- double dX1 = 0, dX2 = 0;
- double *dPhaseShiftSamples = NULL;
- double dPhaseShiftSamplesMean = 0.;
- double *dPhaseShiftSamplesTrigger = NULL;
- double dPeriodSample = 0.;
- double *dPeriodSampleArray = NULL;
- int indice = 0;
- int iNbIndices1 = 0, iNbIndices2 = 0;
- int *iPolarity = NULL;
- int iFlagError = false;
- int SignalValid = false;
- unsigned char maxTryPhase = 1;
- int try = 0;
- iPolarity = (int*)calloc(PERIOD2PROCESS+2,sizeof(int));
- iIndices1 = (int*)calloc(PERIOD2PROCESS+2,sizeof(int));
- iIndices2 = (int*)calloc(PERIOD2PROCESS+2,sizeof(int));
- dPhaseShiftSamples = (double*)calloc(PERIOD2PROCESS+2,sizeof(double));
- dPhaseShiftSamplesTrigger = (double*)calloc(PERIOD2PROCESS+2,sizeof(double));
- dPeriodSampleArray = (double*)calloc(PERIOD2PROCESS+2,sizeof(double));
- dDataX1 = (double*)calloc(LINFITSAMPLES+1,sizeof(double));//+2
- dDataX2 = (double*)calloc(LINFITSAMPLES+1,sizeof(double));
- dOut = (double*)calloc(LINFITSAMPLES+20,sizeof(double));
- //Recreate the real signal in super sampled signal (x1000).
- //Alow better accuracy
- X = (double*)calloc(nbSamplesToCheck,sizeof(double));
- dSecondDerivative1 = (double*)calloc(nbSamplesToCheck,sizeof(double));
- dSecondDerivative2 = (double*)calloc(nbSamplesToCheck,sizeof(double));
- dYSuperSampled1 = (double*)calloc(SUPER_SAMPLES_MAX,sizeof(double));
- dYSuperSampled2 = (double*)calloc(SUPER_SAMPLES_MAX,sizeof(double));
- double validity = 0;
- double stdev = 0;
- double mean = 0;
- do
- {
- printf("debut boucle\n" );
- CHECK_TIMEOUT;
- //sometime the acquition is not finish for all signal, so we cue for the signal with the less of point in the curve.
- if(DescripteurCourbe[iSignal1].nombrePoints <= DescripteurCourbe[iSignal2].nombrePoints)
- nbPoint = DescripteurCourbe[iSignal1].nombrePoints;
- else
- nbPoint = DescripteurCourbe[iSignal2].nombrePoints;
- startSample = nbPoint - nbSamplesToCheck - 2;
- printf("startSample %d\n",startSample);
- //SuperSample the signals
- Ramp(nbSamplesToCheck,startSample,startSample+nbSamplesToCheck,X);
- Spline(X,DescripteurCourbe[iSignal1].identificateurPointsY.points+startSample, nbSamplesToCheck, 0.0, 0.0, dSecondDerivative1);
- Spline(X,DescripteurCourbe[iSignal2].identificateurPointsY.points+startSample, nbSamplesToCheck, 0.0, 0.0, dSecondDerivative2);
- for (iSuperSample = 0; iSuperSample < SUPER_SAMPLES_MAX; iSuperSample++)
- {
- SpInterp(X,DescripteurCourbe[iSignal1].identificateurPointsY.points+startSample, dSecondDerivative1, nbSamplesToCheck, startSample+iSuperSample*(1.0/COEFSUPERSAMPLE), &dYSuperSampled1[iSuperSample]);
- SpInterp(X,DescripteurCourbe[iSignal2].identificateurPointsY.points+startSample, dSecondDerivative2, nbSamplesToCheck, startSample+iSuperSample*(1.0/COEFSUPERSAMPLE), &dYSuperSampled2[iSuperSample]);
- }
- //Get the H1 harmonic for phases since the waveform at crossing 0 is not reliable anymore with rare earth magnet
- if(iSignal1 < PSU1)
- {
- double dFreqResolution;
- double dStartFreq;
- double phaseFound;
- double amplFound;
- double freqFound;
- SearchType Search;
- double *dExportSpectre = (double*)malloc(SUPER_SAMPLES_MAX*sizeof(double));
- double period = 1.0/(DescripteurCourbe[iSignal1].frequenceApparenteEchantillonnage * COEFSUPERSAMPLE);
- Search.centerFrequency = dFrequency;
- Search.frequencyWidth = 2;
- SingleToneSignal (dYSuperSampled1, SUPER_SAMPLES_MAX, period
- , &Search, TONE_EXPORT_DETECTED, &freqFound
- , &lFound, &phaseFound, dYSuperSampled1
- , &period, dExportSpectre, &dStartFreq, &dFreqResolution);
- free(dExportSpectre);
- dExportSpectre = NULL;
- }
- if(iSignal2 < PSU1)
- {
- double dFreqResolution;
- double dStartFreq;
- double phaseFound;
- double amplFound;
- double freqFound;
- SearchType Search;
- double *dExportSpectre = (double*)malloc(SUPER_SAMPLES_MAX*sizeof(double));
- double period = 1.0/(DescripteurCourbe[iSignal2].frequenceApparenteEchantillonnage * COEFSUPERSAMPLE);
- Search.centerFrequency = dFrequency;
- Search.frequencyWidth = 2;
- SingleToneSignal (dYSuperSampled2, SUPER_SAMPLES_MAX, period
- , &Search, TONE_EXPORT_DETECTED, &freqFound
- , &lFound, &phaseFound, dYSuperSampled2
- , &period, dExportSpectre, &dStartFreq, &dFreqResolution);
- free(dExportSpectre);
- dExportSpectre = NULL;
- }
- //Get the median value (avoid HighPass filter to AC or soustract the ac value)
- Median(DescripteurCourbe[iSignal1].identificateurPointsY.points+startSample,nbSamplesToCheck,&dMedian1);
- Median(DescripteurCourbe[iSignal2].identificateurPointsY.points+startSample,nbSamplesToCheck,&dMedian2);
- if(dMedian2 ==0)
- {
- //dor debug
- Sleep(1);
- }
- // Find the location of the next value over the median value (equivalent to 0V crossign value with AC signal)
- ThresholdPeakDetector (dYSuperSampled1,
- SUPER_SAMPLES_MAX,
- dMedian1, width,
- iIndices1,&iNbIndices1);
- ThresholdPeakDetector (dYSuperSampled2,
- SUPER_SAMPLES_MAX,
- dMedian2, width,
- iIndices2,&iNbIndices2);
- if (try >= maxTryPhase) //for debug
- {
- YGraphPopup("",dYSuperSampled2,SUPER_SAMPLES_MAX,VAL_DOUBLE);
- }
- //Get the sample phase shift
- for(indice = 1;indice < iNumbPairsPoles+1;indice ++)
- {
- if(iIndices1[indice] != 0 && iIndices2[indice] != 0)
- {
- //We have samples position, but not acurate enought.
- //We get 80 samples around the index sample to process a lineFit.
- //With the ax+b line, we can compute the position for the triger position with a result in double.
- // (we know y = ax+b. y = median value, need to find the x).
- //Create an array for the x axle
- Ramp(LINFITSAMPLES,iIndices1[indice]-(LINFITSAMPLES/2),iIndices1[indice]+(LINFITSAMPLES/2),dDataX1);
- Ramp(LINFITSAMPLES,iIndices2[indice]-(LINFITSAMPLES/2),iIndices2[indice]+(LINFITSAMPLES/2),dDataX2);
- //Get the new start sample
- iNewStartSample1 = iIndices1[indice]-(LINFITSAMPLES/2);
- if(iNewStartSample1 <0)
- break;
- iNewStartSample2 = iIndices2[indice]-(LINFITSAMPLES/2);
- if(iNewStartSample2 <0)
- break;
- //Get the linear regression
- LinFit(dDataX1,dYSuperSampled1+iNewStartSample1,LINFITSAMPLES,dOut,&dA1,&dB1,&dError1);
- LinFit(dDataX2,dYSuperSampled2+iNewStartSample2,LINFITSAMPLES,dOut,&dA2,&dB2,&dError2);
- //compute the float number sample (the X at the median value)
- dX1 = (dMedian1 -dB1) / dA1;
- dX2 = (dMedian2 -dB2) / dA2;
- if(iSignal1 == U1)
- {
- //dor debug
- Sleep(1);
- }
- //computed value
- dPhaseShiftSamples[indice] = dX1 - dX2;
- if (dPhaseShiftSamples[indice] < 0)
- iPolarity[indice] = 1;
- else if (dPhaseShiftSamples[indice] > 0)
- iPolarity[indice] = 2;
- else
- iPolarity[indice] = 0;
- if(indice > 1)
- {
- if(iPolarity[indice] != iPolarity[indice-1])
- iFlagError = true;
- }
- dPeriodSampleArray[indice] = iIndices1[indice+1] - iIndices1[indice];
- printf("phase %3.2f, period %3.2f\n",dPhaseShiftSamples[indice],dPeriodSampleArray[indice]);
- }
- else
- break;
- }
- //check that all index are almost the same.
- StdDev(dPhaseShiftSamples+1,indice-1,&mean,&stdev);
- validity = stdev/mean;
- validity = fabs(validity);
- printf("std %f, mean %f, valid %f\n",stdev,mean,validity);
- //(!iFlagError) &&
- if( (validity < 0.01))// if we don't have error, means the index are all in the same order and check they have almost the same valeu.
- SignalValid = TRUE;
- try++;
- printf("fin boucle\n" );
- }
- while (!SignalValid);
- Median(dPhaseShiftSamples+1,indice-1,&dPhaseShiftSamplesMean);
- Mean(dPeriodSampleArray+1,indice-1,&dPeriodSample);
- if(dPhaseShiftSamples)
- {
- memset(dPhaseShiftSamples,0, (PERIOD2PROCESS+2)*sizeof(double));
- free(dPhaseShiftSamples);
- dPhaseShiftSamples = NULL;
- }
- if(dPeriodSampleArray)
- {
- memset(dPeriodSampleArray,0, PERIOD2PROCESS+2);
- free(dPeriodSampleArray);
- dPeriodSampleArray = NULL;
- }
- if(X)
- {
- memset(X,0, nbSamplesToCheck);
- free(X);
- X = NULL;
- }
- if(dSecondDerivative1)
- {
- memset(dSecondDerivative1,0,nbSamplesToCheck);
- free(dSecondDerivative1);
- dSecondDerivative1 = NULL;
- }
- if(dSecondDerivative2)
- {
- memset(dSecondDerivative2,0, nbSamplesToCheck);
- free(dSecondDerivative2);
- dSecondDerivative2 = NULL;
- }
- if(dYSuperSampled1)
- {
- memset(dYSuperSampled1,0,SUPER_SAMPLES_MAX);
- free(dYSuperSampled1);
- dYSuperSampled1 = NULL;
- }
- if(dYSuperSampled2)
- {
- memset(dYSuperSampled2,0, SUPER_SAMPLES_MAX);
- free(dYSuperSampled2);
- dYSuperSampled2 = NULL;
- }
- if(iIndices1)
- {
- memset(iIndices1,0, PERIOD2PROCESS+2);
- free(iIndices1);
- iIndices1 = NULL;
- }
- if(iIndices2)
- {
- memset(iIndices2,0, PERIOD2PROCESS+2);
- free(iIndices2);
- iIndices2 = NULL;
- }
- if(dDataX1)
- {
- free(dDataX1);
- dDataX1 = NULL;
- }
- if(dDataX2)
- {
- free(dDataX2);
- dDataX2 = NULL;
- }
- if(dOut)
- {
- free(dOut);
- dOut = NULL;
- }
- if(iPolarity)
- {
- free(iPolarity);
- iPolarity = NULL;
- }
- //Convert into angle value
- // get periods sample duration
- // scale in angle
- // dPeriodSample = iIndices1[2] - iIndices1[1];
- FREQSAMPLE = FREQSAMPLE*COEFSUPERSAMPLE;
- *pdAngle = 360.0*dPhaseShiftSamplesMean/dPeriodSample;
- printf("angle %3.2f\n",*pdAngle);
- return 0;
- }
|
---------------
Monde de merde | Restez curieux
|