Allocating data from the heap.

I find EasyC has impemented most, if not all, of the Standard C library. I have so far avoided using heap allocation routines such as malloc or free, but am now developing a LinkedList module for some protions of my AI. In doing this, I need to know how large the user-allocatable heap is on the Cortex and if anyone has had trouble doing something like this so far.

Thanks, your help would be greatly appreciated :slight_smile:

I don’t know the answer, off hand, but you can figure it out…

Don’t Define any Variables Global. Then try to allocate a Very Large amount a Memory, and look for a Fail Message, then reduce the amount to Allocate and try again, until the Allocate succeeds, then display that Value…
Figure that the More Global Variables you have declared, the lower the Allocated Memory will be…

According to the Wiki Page for the Cortex, the RAM is 64Kb in size…

Hmm, yeah I saw that. I wonder though, because some of the mastercode takes up a lot of that, and then there is my own code that consumes some space in the ram as well. Then there is the main threads stack etc. I am guessing the heap is like 32kb, but I am note too sure.

Well, I could always setup my own heap working off of a very large array of bytes, but I would rather use a pre-existing heap if its there xd

It looks like 32K to me. I ran this little piece of code.

// MyTest.c : implementation file
#include "Main.h"
#include <stdlib.h>

void MyTest()
{
    unsigned char *p;
    int     i;

    for(i=1024;i<0x10000;i+=4096)
        {   
        if( (p = (unsigned char *)malloc( i )) != NULL )
            {
            PrintToScreen("Success %6d %08X\n",i, p );
            free(p);
            p = NULL;
            }
        else
            PrintToScreen("Error %d \n", i);

        Wait(100);
        }
}

and the output was like this

Success 1024 200015D0
Success 5120 200015D0
Success 9216 200015D0
Success 13312 200015D0
Success 17408 200015D0
Success 21504 200015D0
Success 25600 200015D0
H&S col

I assume H&S col means Heap and stack collision.

Running with smaller allocations shows heap is probably just under 32K.

I also dumped all the variables I could find in the runtime lib, there looks to be around 4K. Here’s the dump if you are interested.

    98: 00000000     1 OBJECT  GLOBAL DEFAULT	 3 _debug_info__
    99: 00000001     1 OBJECT  GLOBAL DEFAULT	 3 _is_globaldata__
   100: 00000000     8 OBJECT  GLOBAL DEFAULT	 2 szTeamInfo
    31: 00000004    80 OBJECT  GLOBAL DEFAULT  COM accInfo
    45: 00000000     1 OBJECT  GLOBAL DEFAULT	 3 numberEnabled
    46: 00000001     1 OBJECT  GLOBAL DEFAULT	 3 currentAccel
   119: 00000004    40 OBJECT  GLOBAL DEFAULT  COM pwm1
   122: 00000004    40 OBJECT  GLOBAL DEFAULT  COM pwm2
   130: 00000098     2 OBJECT  GLOBAL DEFAULT	 3 capture
   131: 0000009c     2 OBJECT  GLOBAL DEFAULT	 3 pwm1Index
   132: 000000a0     2 OBJECT  GLOBAL DEFAULT	 3 pwm2Index
   133: 000000a2     1 OBJECT  GLOBAL DEFAULT	 3 updateMaster
   134: 0000009e     1 OBJECT  GLOBAL DEFAULT	 3 pwmStatusFlags
    53: 00000004    64 OBJECT  GLOBAL DEFAULT  COM SPI1_Buffer_Rx
    62: 00000004    64 OBJECT  GLOBAL DEFAULT  COM SPI1_Buffer_Tx
    69: 00000004    64 OBJECT  GLOBAL DEFAULT  COM rx1
    70: 00000004    64 OBJECT  GLOBAL DEFAULT  COM rx2
    92: 00000004    12 OBJECT  GLOBAL DEFAULT  COM slaveJoystick1
    93: 00000004    12 OBJECT  GLOBAL DEFAULT  COM slaveJoystick2
    95: 00000008     4 OBJECT  GLOBAL DEFAULT	 3 slavePtr
    96: 00000004     4 OBJECT  GLOBAL DEFAULT	 3 masterPtr
    97: 0000000d     1 OBJECT  GLOBAL DEFAULT	 3 dataFromPC
    98: 0000000f     1 OBJECT  GLOBAL DEFAULT	 3 rx1Timeout
    99: 00000010     1 OBJECT  GLOBAL DEFAULT	 3 rx2Timeout
   100: 00000001     1 OBJECT  GLOBAL DEFAULT	 3 systemStateFlags
   101: 00000014   264 OBJECT  GLOBAL DEFAULT	 3 rbuf1
   102: 0000011c   264 OBJECT  GLOBAL DEFAULT	 3 tbuf1
   103: 00000004     4 OBJECT  GLOBAL DEFAULT	 2 tx_restart1
   104: 00000000     1 OBJECT  GLOBAL DEFAULT	 3 usartPort
   105: 0000000e     1 OBJECT  GLOBAL DEFAULT	 3 crystalActive
    52: 00000010   104 OBJECT  GLOBAL DEFAULT	 2 dio_gpios
    53: 00000000    13 OBJECT  GLOBAL DEFAULT	 2 dio_config
    54: 00000078    12 OBJECT  GLOBAL DEFAULT	 2 ain_ports
    55: 00000000    16 OBJECT  GLOBAL DEFAULT	 3 ain_adc
    60: 00000000    52 OBJECT  GLOBAL DEFAULT	 3 int_handler
    82: 00000000     2 OBJECT  GLOBAL DEFAULT	 3 ucLastSwData
    34: 00000004   104 OBJECT  GLOBAL DEFAULT  COM enc_data
   123: 00000004  1024 OBJECT  GLOBAL DEFAULT  COM eventRay
   124: 00000000     2 OBJECT  GLOBAL DEFAULT	 3 eventRayIdx
   125: 00000002     1 OBJECT  GLOBAL DEFAULT	 3 unkownIntError
    51: 00000004    60 OBJECT  GLOBAL DEFAULT	 3 gyroInfo
    74: 00000004    36 OBJECT  GLOBAL DEFAULT  COM udpFrameIn
   103: 00000000    12 OBJECT  GLOBAL DEFAULT	 2 usart_ports
   104: 00000008     3 OBJECT  GLOBAL DEFAULT	 3 buttonData
   105: 0000000c     3 OBJECT  GLOBAL DEFAULT	 3 latchButtonData
    81: 00000008   640 OBJECT  GLOBAL DEFAULT  COM imeInfo
    82: 00000004    80 OBJECT  GLOBAL DEFAULT  COM userI2C
    85: 00000004   521 OBJECT  GLOBAL DEFAULT  COM i2cBlock
    90: 00000004    50 OBJECT  GLOBAL DEFAULT  COM devTable
    91: 00000004    40 OBJECT  GLOBAL DEFAULT  COM lPresetCounterI2C
   126: 00000003     1 OBJECT  GLOBAL DEFAULT	 3 updateI2C
   127: 00000004     1 OBJECT  GLOBAL DEFAULT	 3 initI2C
   128: 00000006     1 OBJECT  GLOBAL DEFAULT	 3 isInitI2C
   129: 00000001     1 OBJECT  GLOBAL DEFAULT	 3 activeDeviceIndex
   130: 00000002     1 OBJECT  GLOBAL DEFAULT	 3 totalDevicesActive
   131: 00000038     1 OBJECT  GLOBAL DEFAULT	 2 devAddress
   132: 00000000    56 OBJECT  GLOBAL DEFAULT	 2 I2CErrors
   133: 00000000     1 OBJECT  GLOBAL DEFAULT	 3 currentIme
   167: 00000000    10 OBJECT  GLOBAL DEFAULT	 2 robotMotorsDefinition

Thanks for running that test for me :slight_smile: I hadn’t a cortex at hand to do anything of the sort.

Anything greater 5kb would have been sufficient, so I should be fine.

Thanks again.

You are very welcome, I’m happy to help.

Actually the Master Code runs on a totally different Microcontroller, the Phillips/NXT LPC2458FET180, which appears to have 512Kb of Flash and 64Kb + 16Kb of RAM.

Many Thanks to jpearman, for his Quick Little Test Program… I mean Test Programme… :wink:

I got:



<< SNIP.. SNIP... SNIP.... >>

Success  29696 200015B8
Success  29952 200015B8
Success  30208 200015B8
Success  30464 200015B8
Success  30720 200015B8
Success  30976 200015B8
Success  31232 200015B8
H&S col


Wow, actually thats pretty interesting. Thanks for the heads up :slight_smile: