如何从一个缓冲器读取数据影响用C另一个变量的值 [英] how can reading data from a buffer affect the value of another variable in C

查看:255
本文介绍了如何从一个缓冲器读取数据影响用C另一个变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在寻找在这几个小时。如果我提供的是数据不提供完整的上下文,请让我知道,我会提供一些​​更多的。

Ok I was looking at this for hours. If the data I'm providing is does not provide full context please let me know and I'll provide some more.

所以基本上我有blob数据通过的NSData * packetDescData指出,它看起来是这样的:

So basically I got a blob of data pointed to by NSData * packetDescData and it looks like this:

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

我通过一个循环,从一定偏移读取数据运行,并将它们分配到不同的变量:(用日志语句一起):

I run through a loop that reads data from certain offsets and assign them to different variables: (along with log statements):

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定义为:

rw_int32AtOffset is defined as:

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

输出为这个(注意一下其他一些随机变量(fillbuffindex).. 在同一个线程的..突然它的值更改为的相同的值作为mStartOffset时mStartOffset变的 175 的在packetDescriptionArray [6] .mStartOffset:

the output is this (notice how some other random variable (fillbuffindex).. on the same thread.. suddenly changes it's value to the same value as mStartOffset when mStartOffset becomes 175 at packetDescriptionArray[6].mStartOffset:

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 ..任何想法?

the MAIN: we are appending 1231 bytes to ring buffer point is just me appending to a ring buffer on a separate thread.. the ring buffer is shared amongst the threads, but it has nothing to do with fillbuffindex.. any ideas?

推荐答案

我原来在回答评论:

我的假设是,如果内存边界的溢出下工作(甚至
  读期间),那么几乎任何事情都有可能发生。所以,读您的文章
  我以为你是什么地方超越你的内存边界。寻找
  使用内存调试Valgrind的喜欢的内存相关的错误:的valgrind
  --leak检查= YES的a.out
,也是一个静态分析器像铛静态分析仪:扫描生成的gcc我的codeC

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

这是一个通用的答案像任何问题:我得到在使用动态内存分配的code很奇怪的行为。问题的关键是,超越内存可以导致不确定的行为,包括如在不改变变量被明确指定为。

This is a generic answer to any question like: "I'm getting really weird behavior in a code that uses dynamic memory allocation." The point is that overrunning memory can cause undefined behavior, including e.g. variables changing without being explicitly assigned to.

另外手动调试,像铛静态分析仪和内存调试Valgrind的像静态分析仪是伟大的发现这样的错误。

Besides manual debugging, static analyzers like the clang static analyzer and memory debuggers like valgrind are great for finding bugs like this.

这篇关于如何从一个缓冲器读取数据影响用C另一个变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆