views:

573

answers:

4

Hi Mates,

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.

A: 
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.

Tom
A: 

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

florin
A: 

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.

Jazzu
just....edit your question...
Tom
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.
ephemient
Sorry for that I'll remember this next time.
Jazzu
OK - remember for next time, and delete this answer this time since the information is now in the question.
Jonathan Leffler
A: 

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.

EFraim
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)
EFraim
Thank you thank you thank you !!!!
Jazzu