使用 memcpy 连接字节块中的字节时出错
有时,以下代码可以工作,这可能意味着概念很好,但执行很差。由于崩溃取决于位掉落的位置,这意味着我正在一路走来。我有兴趣找到一种优雅的方法来用 buffer
中的 <=4096 字节填充 bufferdata
,但不可否认,这不是它。
编辑:我收到的错误是对 bufferdata 的非法访问
unsigned char buffer[4096] = {0};
char *bufferdata;
bufferdata = (char*)malloc(4096 * sizeof(*bufferdata));
if (! bufferdata)
return false;
while( ... )
{
// int nextBlock( voidp _buffer, unsigned _length );
read=nextBlock( buffer, 4096);
if( read > 0 )
{
memcpy(bufferdata+bufferdatawrite,buffer,read);
if(read == 4096) {
// let's go for another chunk
bufferdata = (char*)realloc(bufferdata, ( bufferdatawrite + ( 4096 * sizeof(*bufferdata)) ) );
if (! bufferdata) {
printf("failed to realloc\n");
return false;
}
}
}
else if( read<0 )
{
printf("error.\n");
break;
}
else {
printf("done.\n");
break;
}
}
free(bufferdata);
On occasion, the following code works, which probably means good concept, but poor execution. Since this crashes depending on where the bits fell, this means I am butchering a step along the way. I am interested in finding an elegant way to fill bufferdata
with <=4096 bytes from buffer
, but admittedly, this is not it.
EDIT: the error I receive is illegal access on bufferdata
unsigned char buffer[4096] = {0};
char *bufferdata;
bufferdata = (char*)malloc(4096 * sizeof(*bufferdata));
if (! bufferdata)
return false;
while( ... )
{
// int nextBlock( voidp _buffer, unsigned _length );
read=nextBlock( buffer, 4096);
if( read > 0 )
{
memcpy(bufferdata+bufferdatawrite,buffer,read);
if(read == 4096) {
// let's go for another chunk
bufferdata = (char*)realloc(bufferdata, ( bufferdatawrite + ( 4096 * sizeof(*bufferdata)) ) );
if (! bufferdata) {
printf("failed to realloc\n");
return false;
}
}
}
else if( read<0 )
{
printf("error.\n");
break;
}
else {
printf("done.\n");
break;
}
}
free(bufferdata);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很难判断错误在哪里,到处都缺少一些代码。
if(read == 4096) {
看起来像是罪魁祸首,如果 nextBlock 在一次迭代中返回 4000,在下一次迭代中返回 97,该怎么办?现在您需要存储 4097 字节,但您无需重新分配缓冲区来容纳它。您需要累积字节,并在通过 4096 边界时重新分配。
像这样的东西:
It's hard to tell where the error is, there's some code missing here and there.
if(read == 4096) {
looks like a culprit, what if nextBlock, returned 4000 on one iteration, and 97 on the next ? Now you need to store 4097 bytes but you don't reallocate the buffer to accomodate for it.You need to accumulate the bytes, and realloc whenever you pass a 4096 boundary.
something like:
一些评论:
请定义或 const 4096。如果您需要更改它,您将会被烧毁。 realloc 链接是获取缓冲区的极其低效的方法。有什么办法可以预取大小并一次获取所有内容吗?也许不是,但当我看到 realloc() 时,我总是感到畏缩。我还想知道 kZipBufferSize 是什么,以及它是否像其他计数一样以字节为单位。另外,bufferdatawrite到底是什么?我假设它是源数据,但我想查看它的声明以确保它不是内存对齐问题 - 这有点像这种感觉。或者由于大小调整不当而导致缓冲区溢出。
最后,您确定 nextBlock 不会以某种方式超出内存吗?这是代码中的另一个潜在弱点。
A few comments:
Please define or const 4096. You will get burned if you ever need to change this. realloc chaining is an extremely inefficient way to get a buffer. Any way you could prefetch the size and grab it all at once? perhaps not, but I always cringe when i see realloc(). I'd also like to know what kZipBufferSize is and if it's in bytes like the rest of your counts. Also, what exactly is bufferdatawrite? I'm assuming it's source data, but I'd like to see it's declaration to make sure it's not a memory alignment issue - which is kinda what this feels like. Or a buffer overrun due to bad sizing.
Finally, are you sure they nextBlock isn't overruning memory some how? This is another point of potential weakness in your code.