This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Memory allocation

Other Parts Discussed in Thread: MSP430F5438A, MSP430FR5739

I'm trying to understand how memory is stored in the msp430.  I'm using an msp430f5438a and I'm trying to manipulate large amounts of data.


Here is a snippet of code that works.

#include "stdlib.h"

void fillArrays(float *accX, float *accY, float *accZ);

float AccX[200];

float AccY[200];

float AccZ[200];

void main(void) {

fillArrays(AccX, AccY, AccZ);

}

void fillArrays(float *accX, float *accY, float *accZ) {

AccX[0] = 0.21;

....

AccX[199] = 1;

and so on

}

However, I would like to save as much storage space as possible for other uses so I would like to allocate that space using malloc and free it with free.

#include "stdlib.h"

float *AccX;

void main() {

AccX = (float *) malloc(200*sizeof(float));

}

or

#include "stdlib.h"

void main() {

float *AccX;

AccX = (float *) malloc(200*sizeof(float));

}

However, AccX is always NULL in both cases indicating there isn't enough memory available.  In the program that works, to where are the arrays written?

What is the best way to go about doing this?

Thanks,

Kyle

  • You didn't say which compiler you are are using. But with CCS5 it is the linker which allocates memory.

    In the case of the program which works the linker allocates space for the global variables, reporting an error if there is insufficient space.

    When using malloc space is allocated from the heap, where the link -heap option specifies the size of the heap (from CCS5 available under the project properties CCS Build -> MSP430 Linker -> Basic Options). The default heap size is 80 bytes, which is insufficient for your example. For the malloc to work the heap size needs to be increased.

  • Chester Gillon said:
    The default heap size is 80 bytes

    Hmm, I didn't kow that the heap is limited.

    Normally (in old DOS as well as MSPGCC), the typical malloc implementation was using all free memory above the global variables from bottom-up (until it meets the stack which grows top-down).

    However, using malloc should only be used if really necessary. The problem isn't malloc itself (except that using malloc removes the synamiclaly allocated memory from the linker check, so you don't get a linker error if there isn't enough ram), it is free that makes problems.
    Because if you free memory not exactly the opposite order you allocated it (and on bad malloc/free implementation even then), and not all your allocated memory blocks are always exactly the same size, you'll end up with a segmented heap where small pieces of still allocated memory sit somewhere, segemntign the free memory in small segments.
    On a PC this often is nto a problem, since there is enough heap that the program exits long before heap segmentation causes it to crash. On old DOS with its rather limited memory, and even more on a microcontroller, where a typical applicaiton never exits, this will be a probem sooner or later.

    P.s.: it seems that this heap size setting, in opposition to the stack size setting, indeed affects the code. (the stack size is solely organizational and does not affect the generated binary). In MSPGCC none of them exists.

  • Thank you.  I was under the impression that the heap was all free memory available and didn't have a fixed size.  This fixed my issue.

    Kyle.

  • Hello,

    I am experiencing a trouble with using malloc/free functions from stdlib with CCS5 compiler and it seems to fit to topic. I want to port FNET (free TCP/IP stack for Freescale) throughput test to MSP430FR5739 + CC3000 SimpleLink board to roughly examine the limitations of CC3000, so I need to dynamically allocate buffer for reception/transmission buffer depends on running test. I have a trouble with free(), when I call free() function program stuck in infinite loop in disassembly (0x0004:  3FFF JMP (0x0004))

    Can you please give me a hint what I can possibly do wrong?

    my source code looks like this:

    static void fapp_bench_udp_rx (struct fapp_bench_rx_params* rx_params_p)
    {
    int received;
    int faultCounter = 0;
    int nonStand = 0;
    int is_first = 1;
    sockaddr servAddr;
    sockaddr clientAddr;
    socklen_t addr_len;
    struct fapp_bench_t fappBench;

    // Allocate memory for input buffer
    if((fappBench.buffer = (int *)malloc(FAPP_BENCH_BUFFER_SIZE)) == NULL)
    {
    sendString("Out of memory error.\n");
    return;
    }
     ....
     create socket, wait for client, run recieve test, cleanup
     ....
     free(fappBench.buffer);
    }

**Attention** This is a public forum