Thread: Determining size of a byte buffer

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Determining size of a byte buffer

    Hello guys, I am implementing my little protocol to send data to from microcontroller to PC via RS232. I would like to know if there is a way to determine size of a buffer passed to a function, besides sending buffer size as a function parameter

    Here is my function that does encoding
    Code:
    int8_t getMessage(const char* serializedBuffer, char* encodedBuffer, uint8_t encodedBufferLen, uint8_t messageType)
    {
        uint8_t serializedBufferLen;
        uint8_t minimumRequiredEncodedBufferLen;
    
        serializedBufferLen = strlen(serializedBuffer);
        minimumRequiredEncodedBufferLen = serializedBufferLen + MESSAGE_MINIMUM_HEADER_SIZE;
        if (encodedBufferLen <= minimumRequiredEncodedBufferLen)
        {
            return ENCODED_BUFFER_NOT_ENOUGH_SPACE;
        }
    
        // Add header
        encodedBuffer[MESSAGE_START_INDEX] = STX;
        if (messageType == MESSAGE_TYPE_TEMPERATURE)
        {
            encodedBuffer[MESSAGE_OPTION_FLAG_BYTE_POSITION] = MESSAGE_TYPE_TEMPERATURE;
        }
        else
        {
            encodedBuffer[MESSAGE_OPTION_FLAG_BYTE_POSITION] = MESSAGE_TYPE_CONTROL;
        }
    
        // Add data to encodedBuffer
        strcpy(&encodedBuffer[MESSAGE_DATA_START_INDEX], serializedBuffer);
        // Add NULL terminator and End Of Text bytes
        memset(&encodedBuffer[MESSAGE_DATA_LENGTH_INDEX], serializedBufferLen + '0', 1);
        memset(&encodedBuffer[serializedBufferLen + 3], '\0', 1);
        memset(&encodedBuffer[serializedBufferLen + 4], ETX, 1);
    
        return MESSAGE_ENCODER_OK;
    }
    Third parameter 'uint8_t encodedBufferLen' takes care of buffer that data will be stored in.
    But let's say that there is a scenario where user writes following:

    Code:
    int main(void)
    {
    #define ENCODED_BUFFER_LENGTH 10
        uint8_t encodedBuffer[ENCODED_BUFFER_LENGTH];
        uint8_t serializedBuffer[10];
        float floatNo = 125.61f;
        int8_t retVal;
    
        getSerializedMessage(serializedBuffer
        retVal = getMessage((const char*)serializedBuffer, encodedBuffer, ENCODED_BUFFER_LENGTH, MESSAGE_TYPE_TEMPERATURE);
        
        return 0:
    }
    For floatNo = 125.61f I need atleast buffer of length 11 atleast, and since user passed to the function getMessage, argument ENCODED_BUFFER_LENGTH == 10, my encoding will fail since I won't have enough space to store all data.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,113
    When dealing with arrays of any type and malloc'd memory, the usual method is to pass the address of the first element, and the number of elements of the array to the function.

    Another method but not one I recommend is to set the last element to some known value to mark the end of the array.

    There is no way that I am aware of to determine the size of any memory area, when just an address is passed to the function.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you've limited yourself to a maximum buffer size of 255 why not just use that size?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. determining the size of an array when passed by ref
    By mdehnel in forum C++ Programming
    Replies: 21
    Last Post: 09-01-2012, 06:59 AM
  2. Determining file size
    By waterborne in forum C Programming
    Replies: 5
    Last Post: 12-15-2009, 08:56 AM
  3. Single Byte Buffer Overflow
    By Azimuth in forum C Programming
    Replies: 5
    Last Post: 02-07-2009, 11:59 AM
  4. determining size of struct
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-09-2008, 07:10 AM
  5. determining size
    By gooddevil in forum C Programming
    Replies: 5
    Last Post: 05-22-2004, 11:10 AM

Tags for this Thread