Originally Posted by
iMalc
NO.
Elysia was wrong to give you the okay on your suggestion that uses strlen. The size of the binary data to send is the size of the buffer you allocated which is:
Code:
sizeof(date) + sizeof(options) + sizeof(message));
No '+1', and No strlen!
First off strlen should again NOT be used for 'options', and if you were to use strlen for 'message' then you'd have to write an annoying little loop on the receiving end that read characters one at a time until you got a nul char. Not to mention it was just plain wrong as well.
Code:
int some_function(char *message) {
DATE tx_header;
uint8_t options[64]; // this isn't always totally full (normally only the first two elements) so hence the strlen below:
void* tx_buffer = malloc(sizeof(tx_header) + strlen(options) + strlen(message));
// fill the transmit buffer
memcpy(tx_buffer, &tx_header, sizeof(tx_header));
memcpy(tx_buffer + sizeof(tx_header), options, strlen(options));
memcpy(tx_buffer + sizeof(tx_header) + strlen(options), message, strlen(message));
printf("strlen(options): %d\n", strlen(options));
printf("strlen(message): %d\n", strlen(message));
printf("sizeof(tx_header) is : %d\n", sizeof(tx_header));
printf("sizeof(tx_buffer) is: %d, strlen(tx_buffer) is: %d\n", sizeof(tx_buffer), strlen(tx_buffer));
}
If I use sizeof for everything, it's going to return 64 for the options which is larger than what will be sent using the sendto() function. I need to support UP TO 64 options, but not always 64 options. Most of the time, there will be only two options. Options is a string (uint8_t = char if I'm not mistaken).
The same applies for the message that will be passed in. It can handle up to 300 bytes but will not always be this long. Sizeof(message) is going to return 4 every time as it is getting the size of the pointer.
Can you please explain some more why you think I should sizeof()?