双赢不会在c ++中保持其价值

时间:2015-04-17 10:29:13

标签: android c++ java-native-interface

我在C ++中为JNI库编写了一些代码,由于某种原因,我设置的双变量之一绝对不会保留我为它设置的值。浮点数,整数等等都包含在下面的代码中:

void DualPlayer::process(SLAndroidSimpleBufferQueueItf caller) {
pthread_mutex_lock(&mutex);
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"into process");
float *stereoBuffer = outputBuffer[currentBuffer];

bool masterIsA = (crossValue <= 0.5f);
float masterBpm = 125.0;
bpmMaster = 125;
double msElapsedSinceLastBeatA;
if(!hasAbeenPlayedYet){
    masterBpm = 125.0;
    __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," A has not been played : %f",masterBpm);
    __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," A has not been played bpmMaster: %d",bpmMaster);

} else {
    __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," b has not been played: %f",masterBpm);
     masterBpm = masterIsA ? playerA->currentBpm : playerB->currentBpm;
     msElapsedSinceLastBeatA = playerA->msElapsedSinceLastBeat;
}
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"34");
 // When playerB needs it, playerA has already stepped this value, so save it now.
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"56");
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"%i",buffersize);
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"%f",volA);


__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"double: %d",bpmMaster);
bool silence;
if(!hasAbeenPlayedYet){
     silence = !playerA->process(stereoBuffer, false, buffersize, volA,bpmMaster, -1.0);

} else {
 silence = !playerA->process(stereoBuffer, false, buffersize, volA,bpmMaster, playerB->msElapsedSinceLastBeat);

}
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"78");
if(hasAbeenPlayedYet){
    if (playerB->process(stereoBuffer, !silence, buffersize, volB, bpmMaster, msElapsedSinceLastBeatA)) silence = false;
}
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"90");
pthread_mutex_unlock(&mutex);
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"before short to int");
if (silence) memset(stereoBuffer, 0, buffersize * 4); else SuperpoweredStereoMixer::floatToShortInt(stereoBuffer, (short int *)stereoBuffer, buffersize);
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"after short to int");
(*caller)->Enqueue(caller, stereoBuffer, buffersize * 4);
//__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,"stereobuffe put into queue");
if (currentBuffer < NUM_BUFFERS - 1) currentBuffer++; else currentBuffer = 0;
}

然而,在我的logcat中,我从double获得了这个值:

04-17 11:28:09.020  10179-10197/com.players.jason.dualplayers V/DualPlayer﹕ A has not been played : 125.000000
04-17 11:28:09.021  10179-10197/com.players.jason.dualplayers V/DualPlayer﹕ A has not been played bpmMaster: 1074686709

即。由于某种原因,即使变量在头文件中声明并且我分配它,它也不会坚持!但漂浮的确很好。

3 个答案:

答案 0 :(得分:3)

您的退出格式字符串

中有错误
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," A has not been played bpmMaster: %d",bpmMaster);

%d是整数使用%lf或%f的指定符(无论如何都会忽略l),例如:

__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," A has not been played bpmMaster: %lf",bpmMaster);

所以double中的值是正确的。你的调试输出有问题。

答案 1 :(得分:0)

尝试使用%llu

进行打印
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG," A has not been played bpmMaster: %llu",bpmMaster);

答案 2 :(得分:0)

如果您提供了一个小而完整的代码示例来展示您的问题,那将会很有帮助。按原样,有很多操作,其中任何一个都可能是罪魁祸首。没有人可以缩小规模。

最常见的问题 - 一般而言 - 在程序的某个地方,某些代码滥用指针(写入NULL指针,取消引用悬空指针,从数组末尾掉落)。

更改变量类型可能会更改内存中数据/变量的布局,从而更改指针上无效操作所影响的内容。因此,通过改变变量的类型(或大小)似乎消失的问题只是偶然事件 - 原因尚未得到解决。

原因可能在您展示的代码中。它也可能是您未显示的代码,例如被调用的函数之一(您没有显示代码)。