Extra characters appearing when pthread_create is called with a struct as an argument to the thread


I observed that when I pass a struct as an argument to the thread, the string member of the struct is few characters longer inside the thread.

So basically, in main, s-dev is 12 characters long. And when s is passed as an argument to pthread_create, s->dev becomes 14 characters long. What could corrupt the string? Could anybody explain the reason? Thank you!

The code is below:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void *rtu_worker(void *ptr);

typedef struct mb_serial_dev_config
    char parity;
    char *dev;
    uint8_t databits;
    uint8_t stopbits;
    uint32_t baudrate;
} mb_serial_dev_config_t;

int main(int argc, char *argv[])
    int r_th = 0;
    pthread_t rtu_thread;
    mb_serial_dev_config_t *s = (mb_serial_dev_config_t *)malloc(sizeof(mb_serial_dev_config_t));

    //com1 config
    char *serial_device = "/dev/ttyUSB1";
    int32_t baud = 9600;
    char parity = 'N';
    int32_t databits = 8;
    int32_t stopbits = 1;

    s->dev = (char *)malloc(strlen(serial_device));
    s->parity = parity;
    s->baudrate = baud;
    s->databits = databits;
    s->stopbits = stopbits;

    printf("%s(%d)\n",s->dev, strlen(s->dev));
    r_th = pthread_create(&rtu_thread, NULL, rtu_worker, s);
    printf("Waiting on threads ...");
    pthread_join(rtu_thread, NULL);
    printf("exiting the main application ...\n");

void *rtu_worker(void *ptr)
    int count = 0;
    mb_serial_dev_config_t *d = (mb_serial_dev_config_t *)ptr;

    printf("%s(%d)\n",d->dev, strlen(d->dev));

    //print the device name character by character
    while(count < strlen(d->dev)){
        printf("(%d)  %c (%d)\n", count, (d->dev)[count], (d->dev)[count]);

>Solution :

You forgot to reserve memory for the terminating zero character '\0' of the string

s->dev = (char *)malloc(strlen(serial_device) + 1);

Pay attention to that the return type of the function strlen is size_t. So in a call of printf you need to use conversion specifier zu instead of d like

printf("%s(%zu)\n",s->dev, strlen(s->dev));

Also the function rtu_worker returns nothing though its return type is not void.

void *rtu_worker(void *ptr)

Leave a Reply Cancel reply