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.

Unusual memory consumption when passing arrays to function

Other Parts Discussed in Thread: MSP430G2533, MSP430FR5969

I have a C project that contains a function that prints out string literals to an LCD.   The problem is that every time print function is called the ram utilization (according to the console) increases by the number of bytes in the string.   It seems that when literals are passed to the function they are stored in RAM.  When passing the function a pointer to a const char[ ] the same thing happens.  As it currently stands the program is burning through RAM.  Is there a way to stop the compiler from wasting RAM when passing strings?

const char t1[]="Random Text";

int main(void) {
    char i;

    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    LCD_Init();

    LCD_Print("Text 1");        //RAM use 88               7  chars
    LCD_Print("Text 2");        //RAM use 96            14 chars
    LCD_Print("Text 3");        //RAM use 104            21 chars

    i=t1[0];                    //RAM use 104        

    LCD_Print(t1);                //RAM use 116            33 chars
    while(1);
    return 0;
}

From the map file t1 is stored in flash at 0000c1fc.  However, passing it to the print function consumes 12 bytes of RAM, according to the console.  I am under the impression that passing an array in C is the same as passing a pointer to the first element of the array; so I do not see where the 12 bytes of RAM are being  used.

The Program is posted below

----------------------------------------------Main File-------------------------------------------------------

#include <msp430.h>
#include "EZ_LCD.h"


void Clock_Config();

const char t1[]="Random Text";

int main(void) {
    char i;

    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    void Clock_Config();
    LCD_Init();

    LCD_Print("Text 1");        //RAM use 88               7  chars
    LCD_Print("Text 2");        //RAM use 96            14 chars
    LCD_Print("Text 3");        //RAM use 104            21 chars

    i=t1[0];                    //RAM use 104        

    LCD_Print(t1);                //RAM use 116            33 chars
    while(1);
    return 0;
}


void Clock_Config(){  
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ; // DCO clock is set at 1 MHz
    BCSCTL2 =0x06;            // MCLK=DCO/0        SMLCK=DCO/8
    BCSCTL3 = XCAP_3;        //set crystal capacitance to 12.5pF
}

---------------------------------------------------EZ_LCD------------------------------------------------

#include <msp430.h>

 void LCD_W(int Nibble); //private  translates hex command into bit commands
 void LCD_WB(int Byte); //private  writes byte to data register
 void LCD_Print(char Text[]);

void LCD_Print(char Text[])
{
    int F_TMP=0;
    while (Text[F_TMP] != 0x00)  //0x00 is the null charecter used to signal end of string
    {
        LCD_WB(Text[F_TMP]);
        F_TMP++;
    }
}

void LCD_W(int Nibble)
{
    if ((Nibble & 0x8)==0)   {P2OUT &= ~ EZLCD_DB3;}//isolate DB3
    else {P2OUT |= EZLCD_DB3;}
    if ((Nibble & 0x4)==0)   {P2OUT &= ~ EZLCD_DB2;}//isolate DB3
    else {P2OUT |= EZLCD_DB2;}
    if ((Nibble & 0x2)==0)   {P2OUT &= ~ EZLCD_DB1;}//isolate DB3
    else {P2OUT |= EZLCD_DB1;}
    if ((Nibble & 0x1)==0)   {P1OUT &= ~ EZLCD_DB0;}//isolate DB3
    else {P1OUT|= EZLCD_DB0;}

    P1OUT &= ~ EZLCD_EN;
    P1OUT |= EZLCD_EN;
    P1OUT &= ~ EZLCD_EN;
}


void LCD_WB(int Byte)
{
    _delay_cycles(LCD_Delay_M);
    P1OUT |= EZLCD_RS;
    LCD_W((Byte & 0xF0) >> 4);     //masks bytes upper nibble
    _delay_cycles(LCD_Delay_M);
    LCD_W(Byte & 0x0F);    //masks bytes lower nibble
}

CCS 6.1

Windows 7 x64

MSP430G2533

  • Giovanni Montoya95039 said:
    The problem is that every time print function is called the ram utilization (according to the console) increases by the number of bytes in the string.

    I don't trust the memory usage reported on the CCS Console after loading a program. E.g. after using CCS 6.1.1 to load a program to a MSP430FR5969 the CCS Console reported:

    MSP430: Loading complete. There were 13428 (code) and 16 (data) bytes written to FLASH/FRAM. The expected RAM usage is 5217 (uninitialized data + stack) bytes.

    Whereas from the linker map file and CCS Memory Allocation view the RAM usage is 1239 bytes.

    As the MSP430FR5969 only has 2048 bytes of RAM, the CCS Console reporting "The expected RAM usage is 5217" is clearly suspect.

    What does the linker map file or CCS Memory Allocation view report as you add strings to the program?

  • Wow, that was it! The memory map indicated only 80 bytes of memory was used, regardless of how many times I called print. Lesson learned, never trust the console's resource estimates, instead use the .map file. Thank You for helping me.