for (; curPosition >= 0; curPosition --)死循环

本文探讨了一种特殊的死循环情况,即使用 unsigned int 类型的变量进行递减循环时可能出现的问题,并提供了一种通过正向循环来避免该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今儿写了段代码

死循环了

 

for (; curPosition >= 0; curPosition --)

 

什么情况下会发生呢

 

unsigned int curPosition;

 

第一次碰到这种问题,备忘下:D

 

临时解决方法是:

加个变量,改成正顺 for循环

 

 

写出这段函数的调用方法:int16_t SdFile::read(void* buf, uint16_t nbyte) { uint8_t* dst = reinterpret_cast<uint8_t*>(buf); // error if not open or write only if (!isOpen() || !(flags_ & F_READ)) return -1; // max bytes left in file if (nbyte > (fileSize_ - curPosition_)) nbyte = fileSize_ - curPosition_; // amount left to read uint16_t toRead = nbyte; while (toRead > 0) { uint32_t block; // raw device block number uint16_t offset = curPosition_ & 0X1FF; // offset in block if (type_ == FAT_FILE_TYPE_ROOT16) { block = vol_->rootDirStart() + (curPosition_ >> 9); } else { uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_); if (offset == 0 && blockOfCluster == 0) { // start of new cluster if (curPosition_ == 0) { // use first cluster in file curCluster_ = firstCluster_; } else { // get next cluster from FAT if (!vol_->fatGet(curCluster_, &curCluster_)) return -1; } } block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; } uint16_t n = toRead; // amount to be read from current block if (n > (512 - offset)) n = 512 - offset; // no buffering needed if n == 512 or user requests no buffering if ((unbufferedRead() || n == 512) && block != SdVolume::cacheBlockNumber_) { if (!vol_->readData(block, offset, n, dst)) return -1; dst += n; } else { // read block to cache and copy data to caller if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) return -1; uint8_t* src = SdVolume::cacheBuffer_.data + offset; uint8_t* end = src + n; while (src != end) *dst++ = *src++; } curPosition_ += n; toRead -= n; } return nbyte; }
06-12
for K in range(0,N - Nm - Nd+1):#[0,90) if(K == bK):#bK就是均值窗口的起点 WD1 = databck[:Nm] #均值窗 # -----------------检测模块-------------------------------------- U0 = np.mean(WD1) #均值窗口的均值 Mmax = np.max(WD1) #均值窗口的最大值 Mmin = np.min(WD1) #均值窗口的最小值 B = C * (np.abs((Mmax - U0) / 2) + np.abs((U0 - Mmin) / 2)) #噪声水平系数 beishu = (np.abs((Mmax - U0) / 2) + np.abs((U0 - Mmin) / 2)) #自适应倍数,根据周波数据计算得来 Kd = bK+ Nm #更新检测窗口起点 Kx = Kd #更新检测窗口起点 D_p = 0 #正向已检测点个数 D_n = 0 #负向已检测点个数 Pflag = 0 #有突变点的标志 Nflag = 0 #有突变点的标志 #*******************************************************稳态续检模块***************************************** if self.continuedetect_p == 1: #继续检测稳态 WD3 = databck[Kx:Kx+Nd] V = np.var(WD3) # 计算方差 if (V <= (Hc)): dataopen_sta.append(self.CurPosition - N + Kx) dataopen_sta.append(databck[Kx]) self.continuedetect_p = 0 if self.continuedetect_n == 1: #继续检测稳态 WD3 = databck[Kx:Kx+Nd] V = np.var(WD3) # 计算方差 if (V <= (Hc)): dataopen_sta.append(self.CurPosition - N + Kx) dataopen_sta.append(databck[Kx]) self.continuedetect_n = 0 #********************************************************************************************************* # WD3 = databck[Kx:Kx + Nd] # entropy = np.std(WD3) # lamda3 = np.abs(((np.exp(entropy) + np.exp(-entropy + 0.1)) / (np.exp(entropy) - np.exp(-entropy + 0.1))) * np.sqrt(entropy) + 0.1) # 变异系数 # # """归一化数据,使所有元素之和为1""" # data3 = self.normalize(WD3) # entropy2 = self.shannon_entropy(data3) # entropy3 = self.sigmoid(entropy2) # # print('beishu,lamada3,香浓,香浓sigmoid,point',beishu,lamda3, entropy2,entropy3,self.CurPosition - N + Kx) for Kd in range(Kx,Kx+Nd,1): #kd [5,10),kd[95,100) Z_p = max(0.0, float((Z_p + databck[Kd] - (U0 + B)))) # 累计和 正向 Z_n = max(0.0, float((Z_n - databck[Kd] + (U0 - B)))) # 累计和 负向 D_p = D_p + 1 #已检测点个数增加 D_n = D_n + 1 #已检测点个数增加 #***********************************************阈值调整模块,没有自适应目前,只是分了三个档******************************** if(IsLowH == 1): if(U0 < H and U0 > 20):#低于70的用平均功率 if(self.globalmax < Mmax): self.globalmax = Mmax*1.5 H_start = self.globalmax H_end = self.globalmax # H_start = U0 # H_end = U0 elif U0 < 20: H_start = 20 H_end = 20 else: #大于70 H_start = H # 阈值 后续自定义!!!!!! H_end = H # 阈值 后续自定义!!!!!! else: pass #使用默认值H ,70w是阈值 你是事件检测专家, 请根据论文完成双边cusum算法完成阈值选取和自适应问题 您输入的问题字符过长,请简短描述您的问题。
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值