-1

I am writing a code to run in TI chip and my code gets stuck during the following code

UChar* message;
UChar* tempMessage;
message = malloc( bytesOfMessage * sizeof(UChar));
int i = 0;
while(true)
{
    int rxBytes = UART_read(handle, rxBuf, 1);

    //System_printf("%d \n",  rxBuf[0]);
    if(rxBuf[0] != 13)
    {
        message[i] = rxBuf[0];
        i++;
        int l = 1;

        if(i == bytesOfMessage)
        {
            System_printf("first Message: \n");
            for(l=0;l<bytesOfMessage;l++)
            {
                //System_printf("%c",message[l]);
            }
            System_printf("End of first Message\n");

            bytesOfMessage += 10;
            //message = realloc(message, bytesOfMessage * sizeof(UChar));


            tempMessage = (UChar*)realloc(message, bytesOfMessage * sizeof(UChar));
            message = tempMessage;
            tempMessage = NULL;


            System_printf("Message2 %d: \n", bytesOfMessage);
            for(l=0;l<bytesOfMessage;l++)
            {
                //System_printf("%c", message[l]);
            }
            System_printf("End of second Message\n");
        }
        UART_write(handle, rxBuf, rxBytes);
    }
    else
    {
        rxBuf[0] = '\r';
        rxBuf[1] = '\n';
        rxBuf[2] = 'n';
        rxBuf[3] = 'e';
        rxBuf[4] = 'w';
        UART_write(handle, rxBuf, 5);
    }
}

It really doesnt matter what UART_read does.

but the important thing is the realloc function.

the first time I get to that code it's doing OK. But the second time it just gets stuck. Is it a normal code that is supposed to work and the problem is with the chip or the operating system??

  • Ti chip using malloc & co. ....... – LPs Mar 10 '17 at 14:54
  • Anyway the reason behind `tempMessage` is to check `if(tempMessage != NULL)` before to overwrite `message`... – LPs Mar 10 '17 at 14:56
  • 1
    Word to the wise, don't call the result of `alloc` calls. Also, if you're not going to test the result of your `realloc` there's no point in a temporary variable. – KevinDTimm Mar 10 '17 at 15:08

1 Answers1

0

I think, you should post the whole code. before first loop you allocate message buffer with a size of "byteOfMessage" But in the first loop, you realloc message with a higher size (+10). It could work, but sometime it couldn't if the system isn't able to allocate 10 more byte. So you should try to test tempMessage after realloc. it could be NULL !

Code below should work:

    UChar* message;
UChar* tempMessage;
message = malloc(bytesOfMessage * sizeof(UChar));
int i = 0;
while (true)
{
    int rxBytes = UART_read(handle, rxBuf, 1);

    //System_printf("%d \n",  rxBuf[0]);
    if (rxBuf[0] != 13)
    {
        message[i] = rxBuf[0];
        i++;
        int l = 1;

        if (i == bytesOfMessage)
        {
            System_printf("first Message: \n");
            for (l = 0; l<bytesOfMessage; l++)
            {
                //System_printf("%c",message[l]);
            }
            System_printf("End of first Message\n");

            bytesOfMessage += 10;

            tempMessage = (UChar*)realloc(message, bytesOfMessage * sizeof(UChar));
            if (tempMessage == NULL)
            {   /* system can't realloc more bytes */
                tempMessage = malloc(bytesOfMessage * sizeof(UChar));
                memcpy(tempMessage, message, bytesOfMessage - 10);
                free(message);
            }
            message = tempMessage;
            tempMessage = NULL;


            System_printf("Message2 %d: \n", bytesOfMessage);
            for (l = 0; l<bytesOfMessage; l++)
            {
                //System_printf("%c", message[l]);
            }
            System_printf("End of second Message\n");
        }
        UART_write(handle, rxBuf, rxBytes);
    }
    else
    {
        rxBuf[0] = '\r';
        rxBuf[1] = '\n';
        rxBuf[2] = 'n';
        rxBuf[3] = 'e';
        rxBuf[4] = 'w';
        UART_write(handle, rxBuf, 5);
    }
}

Also you should clean your code. Good luck.

JL Mart
  • 1
  • 2