2

I have a problem with realloc function in C. I'm passing you a code bellow:

typedef struct _Pool Pool;
typedef struct _Item Item;

struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

struct _Item {
    char * file; 
    unsigned int lenOfFilePath;
    unsigned int x;
};

void Pool_insert(Pool ** pool, Item * item)
{       
    if ((*pool)->itemsCount + 1 == (*pool)->poolSize)
    {
        (*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;
        if (!(*pool)->items) return;
        (*pool)->poolSize += (*pool)->poolStep;
    }

    (*pool)->items[(*pool)->itemsCount++] = item;
}

I'm not sending the first allocation code but if I increase the number of items that I'm using there everything works fine. When I call realloc I'm getting such error:

malloc: *** error for object 0x19dba8: pointer being reallocated was not allocated

Could you please help me to solve my problem?


Here is the creation method for Pool:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

Like I told, if I want for example insert 1000 Items and allocate the memory by create function then everything works fine if I declare start Pool size for 100 elements and then I want to realloc Items I get the error.

Thank you very much for such quick answer.

4
  • 1
    Why passing pointer to pointer, when one level of indirection would have been enough. Commented Jul 2, 2009 at 18:04
  • By the way you'd better post the allocation code. It appears to be the culprit. Commented Jul 2, 2009 at 18:07
  • When testing allocation, start with allocation sizes of 1 or 2; you then get to test the reallocation quicker. For production, consider going to a larger initial allocation - but consider keeping it the same (small) size. I sometimes use N = (2 * N) + 2; for sizes, starting with N = 0; that gives 2, 6, 14, ... as the sizes. Makes sure the code is tested. Commented Jul 2, 2009 at 19:09
  • I'm not clear why you need the double pointer on the Items (any more than I understand why you are passing 'Pool **' values around). There are places for double pointers - but not all that many of them. (I have used a triple pointer a few times - but not willingly.) Commented Jul 2, 2009 at 19:11

4 Answers 4

1

But (*pool)->items was never allocated! You only allocated (*pool) - the whole slice of memory. You should have used two mallocs in this case - one for the control structure (Pool) and one for items* array.

Clarification: you cannot realloc part of allocated memory - only the whole malloc'ed chunk. Also note that you should test the result of realloc first, before replacing the original pointer in well-written code - otherwise if realloc fails you lose the previously allocated memory block.

Sign up to request clarification or add additional context in comments.

1 Comment

The point is you cannot realloc part of the block. What is malloced as a whole should be realloced or freed as a whole. I.e. you could have realloced (*pool)
0
struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

malloc: *** error for object 0x19dba8: pointer being reallocated was not allocated

Are you allocating memory for items with malloc ? That message seems to be telling that you are not. Realloc should be used for memory allocated with malloc - like functions.

(*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;

For further help, show us how you initialize items.

Comments

0

You probably forgot to initialize *items in the structure and are chasing a wild pointer.

Comments

0

Here is the creation method for Pool:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

Like I told, if I want for example insert 1000 Items and allocate the memory by create function then everything works fine if I declare start Pool size for 100 elements and then I want to realloc Items I get the error.

Thank you very much for such quick answer.

3 Comments

Stack Overflow is not a forum. Answers may be reordered for many reasons, such as votes and edits, and thus should not be used to reply to other answers. Please leave comments or edit the question instead.
Sorry for that I'll remember this next time.
OK - remember for next time, and delete this answer this time since the information is now in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.