gpt4 book ai didi

objective-c - 从缓冲区读取数据如何影响 C 中另一个变量的值

转载 作者:行者123 更新时间:2023-11-30 17:57:49 25 4
gpt4 key购买 nike

好吧,我看了这个几个小时。如果我提供的数据未提供完整的上下文,请告诉我,我将提供更多信息。

所以基本上我得到了 NSData * packetDescData 指向的一团数据,它看起来像这样:

00000000 00000000 00000007 00000007 00000000 00000016 0000001d 00000000 0000001a 00000037 00000000
0000002a 00000061 00000000 00000025 00000086 00000000 00000029 000000af 00000000 00000032 000000e1
00000000 00000027 00000108 00000000 00000038 00000140 00000000 00000031 00000171 00000000 0000001e
0000018f 00000000 00000035 000001c4 00000000 00000027 000001eb 00000000 0000002d 00000218 00000000
00000031 00000249 00000000 00000026 0000026f 00000000 00000033 000002a2 00000000 00000037 000002d9
00000000 00000035 0000030e 00000000 0000002c 0000033a 00000000 00000025 0000035f 00000000 00000020
0000037f 00000000 00000034 000003b3 00000000 0000005d

我运行一个循环,从某些偏移量读取数据并将它们分配给不同的变量:(以及日志语句):

for (int i=0; i < packetDescNumber; i++) {    
packetDescs[i].mStartOffset = [packetDescData rw_int32AtOffset:offset];
offset += sizeof(UInt32);
NSLog(@"packetDescriptionArray[%d].mStartOffset: %lld, fillbuffindex: %d, offset %lu", i,packetDescs[i].mStartOffset, fillBufferIndex, offset);


packetDescs[i].mVariableFramesInPacket = [packetDescData rw_int32AtOffset:offset];
offset += sizeof(UInt32);
NSLog(@"packetDescriptionArray[%d].mVariableFramesInPacket: %lu, fillbuffindex: %d, offset %lu", i,packetDescs[i].mVariableFramesInPacket, fillBufferIndex, offset);


packetDescs[i].mDataByteSize = [packetDescData rw_int32AtOffset:offset];
offset += sizeof(UInt32);
NSLog(@"packetDescriptionArray[%d].mDataByteSize: %lu, fillbuffindex: %d, offset %lu", i,packetDescs[i].mDataByteSize, fillBufferIndex, offset);

NSLog(@"-------------------------------------------------------\n\n\n\n");

}

rw_int32AtOffset 定义为:

- (int)rw_int32AtOffset:(size_t)offset
{
const int *intBytes = (const int *)[self bytes];
return ntohl(intBytes[offset / 4]);
}

输出是这样的(注意一些其他随机变量(fillbuffindex)..在同一线程上..突然将其值更改为与 mStartOffset相同值mStartOffset 在 packetDescriptionArray[6].mStartOffset 处变为 175:

packetDescriptionArray[4].mStartOffset: 97, fillbuffindex: 0, offset 52
MAIN: we are appending 1231 bytes to ring buffer
MAIN: appendToRingBuffer: FILLBUFFERINDEX: 0
packetDescriptionArray[4].mVariableFramesInPacket: 0, fillbuffindex: 0, offset 56
packetDescriptionArray[4].mDataByteSize: 37, fillbuffindex: 0, offset 60
-------------------------------------------------------



packetDescriptionArray[5].mStartOffset: 134, fillbuffindex: 0, offset 64
packetDescriptionArray[5].mVariableFramesInPacket: 0, fillbuffindex: 0, offset 68
packetDescriptionArray[5].mDataByteSize: 41, fillbuffindex: 0, offset 72
-------------------------------------------------------



packetDescriptionArray[6].mStartOffset: 175, fillbuffindex: 175, offset 76
packetDescriptionArray[6].mVariableFramesInPacket: 0, fillbuffindex: 175, offset 80
packetDescriptionArray[6].mDataByteSize: 50, fillbuffindex: 175, offset 84
-------------------------------------------------------



packetDescriptionArray[7].mStartOffset: 225, fillbuffindex: 175, offset 88
packetDescriptionArray[7].mVariableFramesInPacket: 0, fillbuffindex: 175, offset 92
packetDescriptionArray[7].mDataByteSize: 39, fillbuffindex: 175, offset 96
-------------------------------------------------------



MAIN: we are appending 1224 bytes to ring buffer
MAIN: appendToRingBuffer: FILLBUFFERINDEX: 175
packetDescriptionArray[8].mStartOffset: 264, fillbuffindex: 175, offset 100
packetDescriptionArray[8].mVariableFramesInPacket: 0, fillbuffindex: 175, offset 104
packetDescriptionArray[8].mDataByteSize: 56, fillbuffindex: 175, offset 108
-------------------------------------------------------

主要:我们将 1231 字节附加到环形缓冲区点只是我附加到单独线程上的环形缓冲区。环形缓冲区在线程之间共享,但它没有任何作用。与 fillbuffindex 一起做..有什么想法吗?

最佳答案

我最初在评论中回答:

I work under the assumption that if memory bounds are overrun (even during reading) then almost anything can happen. So reading your post I assume you are overrunning your memory bounds somewhere. Look for memory related errors using a memory debugger like valgrind: valgrind
--leak-check=yes a.out
and also a static analyzer like the clang static analyzer: scan-build gcc mycode.c

这是对任何问题的通用答案,例如:“我在使用动态内存分配的代码中遇到了非常奇怪的行为。”关键是内存溢出可能会导致未定义的行为,包括例如变量在没有显式分配的情况下发生变化。

除了手动调试之外,静态分析器(例如 clang 静态分析器)和内存调试器(例如 valgrind)非常适合查找此类错误。

关于objective-c - 从缓冲区读取数据如何影响 C 中另一个变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com