8 years, 6 months ago.

Why does this function crash my program?

This program function causes my program to instantly crash. A print imediately before the function call works, but the first print int the function is never reached. I believe it is a memory issue but i want to know why it crashes on the function call and not later. I am using a an Embedded Artist QSB with a LPC11u35 with 8kB ram.

int writeFrame(SPI my_spi, int Address)
{
    pc.printf("\nWrite Frame");
    char UARTDataBuffer [64];         //Creat a buffer for recived data
    char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash, bufferSize = 3840
    int bufferIndex = 0;            //points to the next available possition in memory
    while(bufferIndex<bufferSize){
		waitForData(UARTDataBuffer);	 //Wait for data on UART and store received data in UARTDataBuffer
		bufferIndex=appendBuffer(UARTDataBuffer, memoryBuffer, bufferIndex);	//copy UARTDataBuffer to the memoryBuffer. If I comment out this line the function runs fine.
		printf("\ntest");
    }
	
	//Write the Buffer to flash memory
    mem.blockErase(my_spi, Address);
    mem.blockErase(my_spi, Address+0x10000);
    Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
    printf("\nblest");
    return Address;
}

1 Answer

8 years, 6 months ago.

look at https://developer.mbed.org/handbook/Memory-Model
Your function allocates 3840 bytes (I hope your variable buffersize is really initialized !) to the stack ....and your total memory is only 8k.
How do you implement appendbuffer ?
If you need such a buffer, I think you need a board with more memory.

Accepted Answer

Thanks for you response. What confuses me is that it crashes immediately when i call the function. As I understand it, if there is not enough memory for the buffer it should crash when Iinitialise the buffer, or when i try to modify the buffer. Here is the appendBuffer code

int appendBuffer(char UARTDataBuffer[], char memoryBuffer[], int bufferIndex)
{
    int posIndex=0;
    for(int i = 0; i < 64; i++) {
        memoryBuffer[bufferIndex+i]=UARTDataBuffer[i];
        posIndex++;
    }
    return (bufferIndex+posIndex);
}
posted by Thomas Horler 05 May 2016

You write "As I understand it, if there is not enough memory for the buffer it should crash when I initialise the buffer" This is how I understand the bug
: 1)the instruction "char memoryBuffer [bufferSize];" allocates a address (a pointer) for the first position in this array, during the function call. 2) Afterwards, you fill the buffer and you destroy some values . This is the famous collision stack/heap

posted by Robert Spilleboudt 05 May 2016

It may not buy you enough space but you can save some memory with the following code change:

    pc.printf("\nWrite Frame");
    char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash, bufferSize = 3840
    int bufferIndex = 0;            //points to the next available possition in memory
    while(bufferIndex<bufferSize){
        waitForData(memoryBuffer+bufferIndex);     //Wait for data on UART and store received data in UARTDataBuffer
        bufferIndex+=64;
        printf("\ntest");
    }

This writes the received data directly into the correct location and so you save the 64 bytes of the uart data buffer.

posted by Andy A 05 May 2016