This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Realloc Function Dont Work Correct

Hi all,

I want to use Dynamic Memory Allocation Functions.
My platform is NXP LPC1768 (Cortex M3).
My Heap Size Defined as 4096 Bytes in lpc17xx_startup.s file.

First of all, I allocate an area with malloc and this operation is success.
After that, I want to resize this area with realloc but system allocate area from Global Area(in my opinion, allocated area address is in the Global Area Range).
So it changes a my Global variable.

My Simple Code :

PRIVATE List *list;
PRIVATE U32 totalHeapSize;

int main()
{
      reallocDyncArea(10, (void**)&list);       // Will Be Success
      reallocDyncArea(20, 10, (void**)&list);   // Allocate From Global Area
}


PUBLIC RESULT allocDyncArea(U16 size, void** allocatedArea)
{
        RESULT retVal = (SUCCESS);

        if ((totalHeapSize + size) < (TOTAL_HEAP_SIZE / 2))
        {
                *allocatedArea = malloc(size);
                if (*allocatedArea == NULL)
                {
                        retVal = (FAILURE);
                }
                else
                {
                        totalHeapSize += size;
                        retVal = (SUCCESS);
                }
        }

        return (retVal);
}
PUBLIC RESULT reallocDyncArea(U16 newSize, U32 oldSize, void** reAllocatedArea)
{
        RESULT retVal = (SUCCESS);

        if ((totalHeapSize + newSize - oldSize) < (TOTAL_HEAP_SIZE / 2))
        {
                *reAllocatedArea = realloc(*reAllocatedArea, newSize);
                if (*reAllocatedArea == NULL)
                {
                        retVal = (FAILURE);
                }
                else
                {
                        totalHeapSize += (newSize - oldSize);
                        retVal = (SUCCESS);
                }
        }

        return (retVal);
}


I cant find problem.
What is my problem.

Thanks.

Murat

Parents
  • After that, I want to resize this area with realloc but system allocate area from Global Area(in my opinion, allocated area address is in the Global Area Range).

    Your opinion is rather irrelevant in this. The region malloc() & friends work with is called the heap. It doesn't need any other name given by you.

    I cant find problem.

    That would be because in the code you're looking at, there isn't one.

    What is my problem.

    The problem lies in your expectation about what realloc() is supposed to do. realloc() is explicitly allowed to do its job by creating a new block and returning that instead of the old one.

Reply
  • After that, I want to resize this area with realloc but system allocate area from Global Area(in my opinion, allocated area address is in the Global Area Range).

    Your opinion is rather irrelevant in this. The region malloc() & friends work with is called the heap. It doesn't need any other name given by you.

    I cant find problem.

    That would be because in the code you're looking at, there isn't one.

    What is my problem.

    The problem lies in your expectation about what realloc() is supposed to do. realloc() is explicitly allowed to do its job by creating a new block and returning that instead of the old one.

Children
No data