为什么我会通过这些 c++ 获得 EXC_BAD_ACCESS功能?

发布于 2025-01-02 11:45:00 字数 935 浏览 1 评论 0原文

我正在做作业,遇到了这些问题。 当我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2025-01-09 11:45:00

如果你谷歌EXC_BAD_ACCESS,你会发现这是因为你正在访问分配的内存块之外的内存。这可能有几个原因。

因此,让我们从失败点开始 - memcpy:您正在向空闲指针 (&free) 写入 free (free) 的内容code>),并且正在复制 sizeof(char *) 字节。假设 free 被声明为 char *free; 那么就可以了,所以它必须是您正在写入的 free 的内容。

从风格上来说,像这样使用memcpy——复制单个指针值——是令人困惑的。您最好使用以下内容:

free = *(char **)free;

这相当于您的:

memcpy(&free,free,sizeof(char*));

sizeof(char*) 的值因系统而异 - 32 位和 上的 4 64 位上为 8,因此分配的空间量必须至少有那么大。

好的,让我们看一下 ExpandPool 方法,看看 free 被设置为什么:

tmp2 = new char;

在这里,您使用 sizeof(char) 分配一块内存,即 1。这至少需要:

tmp2 = new char[sizeof(char *)];

注意:调用变量 free 将覆盖 free 函数,因此您需要通过编写 ::free 显式访问该函数

我首先绘制一个图表,说明您想要的池的内存布局以及它的外观/变化(a)当空时,(b)当分配空闲块时以及(c)当分配时当您需要扩展池时使用一个块。使用不同的变量(pooltmptmp2free)对图表进行注释。这将使您了解需要做什么以及代码应该是什么样子。

充分理解数据结构和算法(通过创建图表)将帮助您编写正确的代码。

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 copying sizeof(char *) bytes. Assuming free is declared as char *free; then that is ok, so it must be the content of free you are writing from.

Stylistically, using memcpy like this -- to copy a single pointer value -- is confusing. You are better off with something like:

free = *(char **)free;

which is equivalent to your:

memcpy(&free,free,sizeof(char*));

The value of sizeof(char*) varies between systems -- 4 on 32-bit and 8 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:

tmp2 = new char;

Here, you are allocating a block of memory with sizeof(char) which is 1. This needs to be at least:

tmp2 = new char[sizeof(char *)];

NOTE: Calling your variable free will override the free 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 and free). 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.

对你再特殊 2025-01-09 11:45:00

您的代码中有几个问题。对我来说最突出的是这一部分:

pool = tmp;
delete [] tmp;

对我来说,这使得 pool 指向已删除的内存。稍后在代码中使用pool会导致未定义的行为,这是语言无法解释的。代码中其他地方的失败是可以预料的。

There are several problems in your code. One that stands out to me is this part:

pool = tmp;
delete [] tmp;

To me, this makes pool point to deleted memory. Using pool 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文