Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:39:09+00:00 2026-05-29T07:39:09+00:00

I am doing a homework assignment and I am running into these issues. I

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:39:10+00:00Added an answer on May 29, 2026 at 7:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a basic homework assignment which looks like this: While input <> -1
I am doing a homework assignment that deals with classes and objects. In it,
I'm doing a homework assignment in which I need to print out to the
I am doing a little homework assignment in which we are making a very
I am doing a homework assignment that reads in a book. First, a line
I am doing a homework assignment that involves writing a program to draw shapes
Doing a homework assignment and I'm not sure I can wrap my mind around
I'm doing a homework assignment in C. I have to build a calculator that
while doing some homework in my very strange C++ book, which I've been told
I'm doing some homework and while I have some experience with SML, Haskell has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.