为什么我会通过这些 c++ 获得 EXC_BAD_ACCESS功能?
我正在做作业,遇到了这些问题。 当我调用 allocate() 时,我收到 EXC_BAD_ACCESS;
void* Pool::allocate() {
if( free == NULL) {
this->expandPool();
}
void* tmp = free;
void* mem = malloc(elemSize);
memcpy(&free,free,sizeof(char*)); //exec bad access right here
memcpy(tmp,mem,sizeof(elemSize));
return tmp;
}
这是我的 ExpandPool 方法:
void Pool::expandPool() {
poolSize++;
// Is this the first time?
if(poolSize <= 1)
pool = new char*[poolSize];
else {
char** tmp = new char*[poolSize];
memcpy(tmp,pool,sizeof(pool));
delete [] pool;
pool = tmp;
delete [] tmp;
}
char* tmp = NULL;
char* tmp2;
for(int i = 0; i < blockSize; i++) {
tmp2 = new char;
memcpy(tmp2,&tmp,sizeof(char*));
tmp = tmp2;
}
pool[poolSize - 1] = tmp;
free = tmp;
}
I am doing a homework assignment and I am running into these issues.
I am getting EXC_BAD_ACCESS when I call allocate();
void* Pool::allocate() {
if( free == NULL) {
this->expandPool();
}
void* tmp = free;
void* mem = malloc(elemSize);
memcpy(&free,free,sizeof(char*)); //exec bad access right here
memcpy(tmp,mem,sizeof(elemSize));
return tmp;
}
Here is my expandPool method:
void Pool::expandPool() {
poolSize++;
// Is this the first time?
if(poolSize <= 1)
pool = new char*[poolSize];
else {
char** tmp = new char*[poolSize];
memcpy(tmp,pool,sizeof(pool));
delete [] pool;
pool = tmp;
delete [] tmp;
}
char* tmp = NULL;
char* tmp2;
for(int i = 0; i < blockSize; i++) {
tmp2 = new char;
memcpy(tmp2,&tmp,sizeof(char*));
tmp = tmp2;
}
pool[poolSize - 1] = tmp;
free = tmp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你谷歌
EXC_BAD_ACCESS
,你会发现这是因为你正在访问分配的内存块之外的内存。这可能有几个原因。因此,让我们从失败点开始 -
memcpy
:您正在向空闲指针 (&free
) 写入 free (free
) 的内容code>),并且正在复制sizeof(char *)
字节。假设 free 被声明为char *free;
那么就可以了,所以它必须是您正在写入的free
的内容。从风格上来说,像这样使用memcpy——复制单个指针值——是令人困惑的。您最好使用以下内容:
这相当于您的:
sizeof(char*)
的值因系统而异 - 32 位和上的
4
64 位上为 8,因此分配的空间量必须至少有那么大。好的,让我们看一下 ExpandPool 方法,看看 free 被设置为什么:
在这里,您使用
sizeof(char)
分配一块内存,即1
。这至少需要:注意:调用变量
free
将覆盖free
函数,因此您需要通过编写::free 显式访问该函数
。我首先绘制一个图表,说明您想要的池的内存布局以及它的外观/变化(a)当空时,(b)当分配空闲块时以及(c)当分配时当您需要扩展池时使用一个块。使用不同的变量(
pool
、tmp
、tmp2
和free
)对图表进行注释。这将使您了解需要做什么以及代码应该是什么样子。充分理解数据结构和算法(通过创建图表)将帮助您编写正确的代码。
If you google
EXC_BAD_ACCESS
, you will find that it is because you are accessing memory outside an allocated memory block. This can be for several reasons.So, lets start at the failing point -- the
memcpy
: you are writing to the free pointer (&free
) the content of free (free
), and are copyingsizeof(char *)
bytes. Assuming free is declared aschar *free;
then that is ok, so it must be the content offree
you are writing from.Stylistically, using
memcpy
like this -- to copy a single pointer value -- is confusing. You are better off with something like:which is equivalent to your:
The value of
sizeof(char*)
varies between systems --4
on 32-bit and8
on 64-bit -- so the amount of space allocated must be at least that big.Ok, so lets look at the expandPool method to see what free is set to:
Here, you are allocating a block of memory with
sizeof(char)
which is1
. This needs to be at least:NOTE: Calling your variable
free
will override thefree
function, so you will need to explicitly access that function by writing::free
.I'd start off by drawing a diagram of what you want the memory layout of the pool to be and how it will look/change (a) when empty, (b) when allocating a chunk that is free and (c) when allocating a chunk when you need to expand the pool. Annotate the diagram with the different variables (
pool
,tmp
,tmp2
andfree
). This will give you an idea of what you need to do and what the code should look like.Having a good understanding of the data structures and algorithms (through creating the diagrams) will help you get the code right.
您的代码中有几个问题。对我来说最突出的是这一部分:
对我来说,这使得
pool
指向已删除的内存。稍后在代码中使用pool
会导致未定义的行为,这是语言无法解释的。代码中其他地方的失败是可以预料的。There are several problems in your code. One that stands out to me is this part:
To me, this makes
pool
point to deleted memory. Usingpool
later in the code causes undefined behavior, which can not be explained by the language. Failure elsewhere in the code is just to be expected.