artic sol
artic sol

Reputation: 493

creating a file name in c program causing a seg fault

The is a struct in my program called prv_instance_t which contains an experiment_id as defined here.

From this experiment_id variable I want to create another variable called serial_log which stores serial data from a UART. I want this handled in a thread. I create the filename for the serial data by appending "_serial_log.txt" to the experiment_id as below.

int function() { 

prv_serial_logging_thread_params_t serial_logging_thread_params;
targetP = (prv_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);

I then create a thread using pthread.

I'm getting a seg fault, could I be creating the filename in a better way, maybe I am causing a buffer overflow?

Upvotes: 0

Views: 37

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

serial_logging_thread_params is a local variable. It will go out of scope and disappear once the function returns. If that happens while the thread is still running, then the pointer to the structure will become invalid.

Allocate the structure dynamically using malloc.

Upvotes: 2

Related Questions