【发布时间】:2015-01-26 17:01:16
【问题描述】:
我有一个以下函数process 调用一个例程dataFileBuffer,它接受一个指向指针的指针并在取消引用的指针位置上执行memcpy。
int dataFileBuffer(uint8_t *index, char **tempBuf,int size)
{
if(index != stop_address)) /*stop_address is a fixed pointer to the end buffer*/
{
if(*tempBuf)
{
if(index + size < stop_address)
memcpy(*tempBuf,index,size);
else
{
size = stop_address-index-1;
memcpy(*tempBuf,index,size);
}
}
else
size = 0;
}
else
size = 0;
return size;
}
int process()
{
char *readBuf=NULL;
char *tBuf = (char *)malloc(MAX_LENGTH);
int readBytes = -1;
uint8_t *index = start_address;
uint8_t *complete = stop_address;
do
{
readBuf = tBuf+(sizeof(char)*40);
readBytes = 0;
readBytes = dataFileBuffer(index,&readBuf,MAX_LENGTH);
if(readBytes > 0)
{
index = index+readBytes;
}
}while(index <= complete);
return readBytes;
}
我的 process 函数间歇性地看到堆栈损坏,这让我认为我的复制实现有问题。
我只是想了解我们是否可以将指向指针的指针作为参数传递并安全地 memcpy 到被调用函数中的取消引用位置?
【问题讨论】:
-
你是在编写c还是c++?
-
不要在 c. 中投射
malloc。 -
当您在有问题的
malloc行放置断点时,指针的内容是否在正确的区域?您必须使用调试器来获取此信息。 -
你为什么选择
char**?你不修改调用者的指针,所以只需传递它char*。 -
sizeof(char)始终为 1,这是标准规定的。