I want to do a constructor object-oriented-like function in C, but the address of my structure and its members are not being changed after I malloc() them. I saw this answer Changing address contained by pointer using function and I got an idea of what my problem is, but still can't solve it. Below is my code that is doing the constructor:
udp.h
typedef struct{
int socket;
char header[3];
float *send_value;
char *message_buffer;
}message;
void build_message(message **msg, int socket, char *header,float *send_value);
main.c
message *message_Ta = NULL;
build_message(&message_Ta, socket_cliente, "sta", &Ta);
sprintf(message_Ta->message_buffer, "%s0.0", message_Ta->header);
udp.c
void build_message(message **msg, int socket, char header[], float *send_value){
*msg = malloc(sizeof(message)+sizeof(int)+sizeof(float *) + HEADER_SIZE + BUF_SIZE);
msg->socket = socket;
strcpy(msg->header, header);
msg->send_value = send_value;
msg->message_buffer = NULL;
msg->message_buffer = malloc(BUF_SIZE);
}
Btw, I am using threads in these code, would this code be thread-safe even without mutexes? thx in advance
Aucun commentaire:
Enregistrer un commentaire