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

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

今儿写了段代码

死循环了

 

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
<template> <view class="announcement-container"> <!-- 公告列表 --> <view class="rules-list"> <!-- 列表项 --> <view class="rule-item" v-for="(rule, index) in announcementList" :key="index" @click="goToDetail(rule)" > <view class="rule-info"> <text class="rule-title">{{ rule.noticetitle }}</text> <text class="rule-date">发布日期:{{ rule.gattime }}</text> </view> <view class="rule-arrow"> <text class="arrow">></text> </view> </view> <!-- 加载状态 --> <view class="loading-status" v-if="loading"> <uni-load-more status="loading" content-text="加载中..." /> </view> <!-- 没有更多数据 --> <view class="loading-status" v-else-if="!hasMore"> <uni-load-more status="noMore" content-text="没有更多数据" /> </view> <!-- 空状态 --> <view class="empty-state" v-if="announcementList.length === 0 && !loading"> <image src="/static/images/empty.png" mode="aspectFit"></image> <text>暂无公告数据</text> </view> </view> </view> </template> <script> import { noticeList } from '@/api/index' export default { data() { return { announcementList: [], pageNum: 1, pageSize: 10, total: 0, loading: false, hasMore: true } }, onLoad() { this.getNoticeList() }, onReachBottom() { // 触底加载更多 if (!this.loading && this.hasMore) { this.pageNum++ this.getNoticeList(true) } }, methods: { async getNoticeList(isLoadMore = false) { // 如果正在加载,直接返回 if (this.loading) return this.loading = true try { const res = await noticeList({ pageNum: this.pageNum, pageSize: this.pageSize }) if (res.code === 200) { const filteredList = res.rows.filter(item => item.nticetype === '1') this.total = res.total || 0 if (isLoadMore) { // 追加数据 this.announcementList = [...this.announcementList, ...filteredList] } else { // 重置数据 this.announcementList = filteredList } // 判断是否还有更多数据 this.hasMore = this.announcementList.length < this.total } else { uni.showToast({ title: res.msg || '获取公告列表失败', icon: 'none' }) } } catch (error) { uni.showToast({ title: '获取公告列表失败', icon: 'none' }) } finally { this.loading = false } }, goToDetail(rule) { uni.navigateTo({ url: `/pages/index/conponents/CompanyAnnouncement/components/index?item=${encodeURIComponent(JSON.stringify(rule))}` }) } } } </script> <style lang="scss" scoped> .announcement-container { padding: 20rpx; background-color: #f5f5f5; min-height: 100vh; } .rules-list { .rule-item { display: flex; justify-content: space-between; align-items: center; padding: 30rpx; background-color: #ffffff; border-radius: 12rpx; margin-bottom: 20rpx; box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05); transition: all 0.2s; &:active { transform: scale(0.99); } .rule-info { flex: 1; .rule-title { font-size: 32rpx; color: #333; font-weight: bold; margin-bottom: 10rpx; display: block; } .rule-date { font-size: 24rpx; color: #999; } } .rule-arrow { margin-left: 20rpx; .arrow { font-size: 32rpx; color: #999; } } } .loading-status { padding: 20rpx 0; text-align: center; } .empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 100rpx 0; image { width: 200rpx; height: 200rpx; margin-bottom: 20rpx; opacity: 0.5; } text { font-size: 28rpx; color: #999; } } } </style> 分页有问题 有重复的,帮我修改
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值