我有一个名为from_binary_to_decimal
的C函数,它被另一个函数x_caching
调用。问题是from_binary_to_decimal
返回例如2.25(一个浮点数),但x_caching
(有一行写入来存储返回值)修改了第一个函数返回的以前的值。C函数返回值但调用者分配另一个值
我把(在其下它们被执行的顺序)代码的图像:
float** cache_provider(struct INPUT_DATA* d, char** arrszChromosomes)
{
static float** arrfCache = NULL;
int i;
if (d == NULL && arrszChromosomes == NULL) return arrfCache;
if (arrfCache == NULL)
{
arrfCache = (float**)malloc(d->m_iPopulationSize * sizeof(float*));
for (i = 0; i < d->m_iPopulationSize; ++i)
arrfCache[i] = (float*)malloc(2 * sizeof(float));
}
x_caching(d, arrszChromosomes, &arrfCache);
return arrfCache;
}
void x_caching(struct INPUT_DATA *d,
char **arrszChromosomes,
float **arrfCache)
{
int i;
float fTemp = 0.0f;
for (i = 0; i < d->m_iPopulationSize; ++i) {
arrfCache[i][0] = get_cache_key(arrszChromosomes[i]);
fTemp = from_binary_to_decimal(d, arrszChromosomes[i], 0);
arrfCache[i][1] = fTemp;
}
}
float from_binary_to_decimal(struct INPUT_DATA *d,
char *szChromosome,
int iCacheQuery)
{
float fRetVal = 0.0;
float fFinal = 0.0f;
float *fCacheVal = NULL;
int i = 0;
if (iCacheQuery
&& (fCacheVal = get_x_value_from_cache(szChromosome)) != NULL)
return *fCacheVal;
for (i = 0; i < strlen(szChromosome); ++i)
fRetVal +=
(szChromosome[i] == '1' ? 1 : 0) *
powf(2, d->m_iBitsPChromosome - (i + 1));
fFinal = d->m_arrcDomainInterval[0] + (fRetVal * d->m_fDelta);
return fFinal;
}
fTemp
本来存储多个像2.51代替它被存储值如8133608.
后这里作为文本的代码,没有人会打扰你的如下链接 – stdcall 2013-03-14 18:55:07
我更新了我的问题,如果没有被格式化得当那是因为我即将用完代表的。请帮助。谢谢。 – 2013-03-14 19:02:55
你在什么时候检查'fTemp'的值?你使用调试器吗? – moooeeeep 2013-03-14 19:05:20