对不起,如果这不是MCV代码,但是这个错误让我如此生气,以至于我不知道这根本就是什么,我
我有这个功能:
#include <string>
#include <iostream>
#include "BeladyAlgorithm.hpp"
#define VERBOSE 0
float computeOptimalCHR(const std::vector<cv::Mat1f> setupQueries, const std::vector<cv::Mat1f> queries, size_t cacheSize, float threshold){
std::vector<cv::Mat1f> cache(cacheSize, cv::Mat1f());
//nextUse[i] = k it means that that the i-th cached element will be used at time k
std::vector<int> nextUse(cacheSize, -1);
std::vector<int> temp(cacheSize, 0);
int nQueries = queries.size(), cacheHits=0, cacheHitFound, freeElementFound, uselessElementFound, k, pos, max, faults = 0;
//cache initialization
for(size_t i = 0; i < std::min((int) setupQueries.size(), nQueries); ++i){
cache[i] = setupQueries[i];
}
std::cout<<"setupQueries="<<setupQueries.size()<<" nQueries="<<nQueries<<std::endl;
for(size_t i=0; i<cacheSize; i++)
std::cout<<"cache["<<i<<"] empty="<<cache[i].empty()<<std::endl;
//for each query
for(size_t i = 0; i < nQueries; ++i){
cacheHitFound = freeElementFound = 0;
std::cout<<"i="<<i<<std::endl;
//if i-th query is the j-th cached element, then cache hit
for(size_t j = 0; j < cacheSize; ++j){
if(!cache[j].empty() && cv::norm(cache[j], queries[i]) <= threshold){
cacheHitFound = freeElementFound = 1;
cacheHits++;
if(VERBOSE) std::cout<<"Cache hit with index "<<j<<std::endl;
break;
}
}
std::cout<<"cache hit done"<<std::endl;
std::cout<<"i="<<i<<std::endl;
//if cacheHit is 0, then queries[i] is not cached...
if(cacheHitFound == 0){
//searching for an empty element in the cache
for(size_t j = 0; j < cacheSize; ++j){
//if there is an empty element, assign it
if(cache[j].empty()){
if(VERBOSE) std::cout<<"Filling empty element"<<std::endl;
faults++;
cache[j] = queries[i];
freeElementFound = 1;
break;
}
}
}
std::cout<<"empty element done"<<std::endl;
std::cout<<"i="<<i<<std::endl;
//if freeElementFound, it means that the cache is full
if(freeElementFound == 0){
std::cout<<"i="<<i<<std::endl;
uselessElementFound =0;
std::cout<<"looking next use"<<std::endl;
for(size_t j = 0; j < cacheSize; ++j){
temp[j] = -1;
std::cout<<"j="<<j<<std::endl;
//look when queries[j] will be used next time
for(k = i + 1; k < nQueries; ++k){
std::cout<<"looking element "<<k;
if(!cache[j].empty() && cv::norm(cache[j], queries[k]) <= threshold){
std::cout<<" distance computed";
temp[j] = k;
std::cout<<" assignment done";
break;
}
std::cout<<std::endl;
}
}
std::cout<<"i="<<i<<std::endl;
std::cout<<"looking useless"<<std::endl;
//if a cached element will never be used again: replace it
for(size_t j = 0; j < cacheSize; ++j){
if(temp[j] == -1){
pos = j;
if(VERBOSE) std::cout<<"cache["<<j<<"] will never be used again"<<std::endl;
uselessElementFound = 1;
break;
}
}
std::cout<<"i="<<i<<std::endl;
std::cout<<"replacing"<<std::endl;
//if we did not find an useless element, then replace the one that will not be used for longer
if(uselessElementFound ==0){
max = temp[0];
pos = 0;
for(size_t j = 1; j < cacheSize; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
if(VERBOSE) std::cout<<"cache["<<pos<<"] is the most useless element"<<std::endl;
}
std::cout<<"cloning pos="<<pos<<std::endl;
std::cout<<"queries.size="<<queries.size()<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"queries["<<i<<"].rows="<<queries[i].rows<<std::endl;
std::cout<<"queries["<<i<<"].cols="<<queries[i].cols<<std::endl;
std::cout<<"queries.rows="<<queries[i].rows<<" cols="<<queries[i].cols<<std::endl;
std::cout<<"cache.rows="<<cache[pos].rows<<" cols="<<cache[pos].cols<<std::endl;
cache[pos] = queries[i];
faults++;
std::cout<<"replaced"<<std::endl;
}
if(VERBOSE) std::cout<<std::endl;
for(size_t j = 0; j < cacheSize; ++j){
if(cache[j].empty())
break;
if(VERBOSE) std::cout<<"cache["<<j<<"]= "<<cache[j]<<std::endl;
}
std::cout<<"cycle done"<<std::endl;
}
return cacheHits/(float)queries.size();
}
我在以下位置遇到了分段错误:
std::cout<<"queries["<<i<<"].rows="<<queries[i].rows<<std::endl;
通过测试i
的值,我得到一个无意义的值。
**但正如您从此代码中看到的那样,i
永远不会被分配,只会写入for(size_t i = 0; i < nQueries; ++i)
**
我遇到了一个段错误:
std::cout<<"queries["<<i<<"].rows="<<queries[i].rows<<std::endl;
nQueries
是1500
和i=284
。
同样,我很抱歉,但我不能认真地弄清楚该怎么做。