user1742032
user1742032

Reputation: 43

A double won't hold its value in c++

I'm writing some code in C++ for a JNI library and for some reason, one of the double variables i set absolutely will not hold the value I set for it. Where as a float, an int etc all hold, code below:

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;
}

However out of my logcat I am getting this value from the 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

I.e. for some reason even though the variable is declared in the header file and I assign it, it will just not stick! But the float does just fine.

Upvotes: 1

Views: 809

Answers (3)

Martin Schlott
Martin Schlott

Reputation: 4557

You have an error in your logout format string

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

%d is the specifierfor integer use %lf or %f (the l is ignored anyway) for doubles like:

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

so the value in the double is correct. Your debug output is buggy.

Upvotes: 3

Peter
Peter

Reputation: 36607

It would help if you provided a small but complete sample of code that exhibits your problem. As is, there are lots of operations, and any one of them could be the culprit. Nobody can narrow thigns down.

The most likely problem - in general terms - is that, somewhere in your program, some code is misusing a pointer (writing to a NULL pointer, dereferencing a dangling pointer, falling off the end of an array).

Changing the type of variable can potentially change the layout of data/variables in memory, and therefore change what is being affected by the invalid operation on a pointer. So the problem seeming to disappear by changing type (or size) of a variable is just happenstance - the cause has not been addressed.

The cause might be in code you have shown. It could also be in code you have not shown, such as one of the functions being called (for which you haven't shown code).

Upvotes: 0

bonnyz
bonnyz

Reputation: 13558

Try to print it using %llu :

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

Upvotes: 0

Related Questions