C函数在另一个函数中没有返回正确的值

时间:2013-09-20 16:03:34

标签: c string utf-8 struct

我有这个问题。我有一个函数create_message填充结构

struct message {
struct header{
    char *protocol_version;         
    char *type;                     
    long int sequence_number;       
}header;                            
struct body
{
    int num_tag;                    
    char *tag_labels[LEN];          
    int num_attr_tag[LEN];          
    char *attr_labels[LEN][LEN];
    char *attr_values[LEN][LEN];    
    char *attr_types[LEN][LEN];         }body;                              
  }; 

create_message的代码是:

void create_message(struct message *msg, CharmsMsg *chmsg, char *protocol_version)
{
int i,j,n;
XTypes type = chmsg->type;
printf("Tipo %ld\n", chmsg->type);

msg->header.protocol_version = (char *) malloc(sizeof(char)*STRLEN);
msg->header.protocol_version = protocol_version;

msg->header.type = (char *) malloc(sizeof(char)*STRLEN);
msg->header.type = metadati[type].typemsg;

msg->header.sequence_number = chmsg->SeqNo;

msg->body.num_tag = metadati[type].num_tag;

n=0;
for(i=0;i<msg->body.num_tag;i++)        
{
    // assegno le etichette ai tag
    msg->body.tag_labels[i] = (char *) malloc(sizeof(char)*STRLEN);
    msg->body.tag_labels[i] = metadati[type].tag_labels[i];
    msg->body.num_attr_tag[i] = metadati[type].num_attr_tag[i];

    for(j=0;j<msg->body.num_attr_tag[i];j++)     
    {

        msg->body.attr_labels[i][j] = (char *) malloc(sizeof(char)*STRLEN);
        msg->body.attr_labels[i][j] = metadati[type].attr_labels[i][j];

        // assegno il valore dell'attributo j-esimo al tag i-esimo
        msg->body.attr_values[i][j] = (char *) malloc(sizeof(char)*STRLEN);
        msg->body.attr_values[i][j] = ((getDati[type])(chmsg->structCHARMS))[n++];
        printf("IN create_message: Contenuto buf[%d] %s\n",(i+j), msg->body.attr_values[i][j]);
        // assegno il tipo dell'attributo j-esimo al tag i-esimo
        msg->body.attr_types[i][j] = (char *) malloc(sizeof(char)*STRLEN);
        msg->body.attr_types[i][j] = metadati[type].attr_types[i][j];
    }
}
 }

Particurarly create_message调用getDati [type],它是一个指向函数的指针数组。此数组中的特定函数是getRegisterMe,如下所示

char **getRegisterMe(void *structCHARMS)
{
ClientData *client = (ClientData *)structCHARMS;
char *buf[3];
int i;

for(i=0; i< 3; i++)
    buf[i] = (char*)calloc(MAX_SEQ_LEN, sizeof(char));

//attributi del tag[0]:Cookie
sprintf(buf[0], "%d", client->cookie_value);
//attributi del tag[1]:Register
sprintf(buf[1], "%s", addrtostr(client->local_addr));
sprintf(buf[2], "%d", client->mode);

printf("IN getData buf[0]%s\n",buf[0]);
printf("IN getData buf[1]%s\n",buf[1]);
printf("IN getData buf[2]%s\n",buf[2]);

return (char **)buf;
}

当我启动程序时,我认为getRegisterMe没有正确填充struct struct message的元素。事实上,我观察到了这一点:

IN getData buf[0]0
IN getData buf[1]127.0.0.1:53285
IN getData buf[2]3
IN create_message: Contenuto buf[0] ?   ?V׾??FR?
IN getData buf[0]0
IN getData buf[1]127.0.0.1:53285
IN getData buf[2]3
IN create_message: Contenuto buf[1] H??H???H??t?

IN getData buf[0]0
IN getData buf[1]127.0.0.1:53285
IN getData buf[2]3
IN create_message: Contenuto buf[2] 

为什么getRegisterMe中打印的char * buf的值与msg-&gt; body.attr_values打印的值不匹配?任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

你的函数getRegisterMe返回一个指向局部变量buf的指针,当函数返回时它会消失。

答案 1 :(得分:1)

您的代码中有几个位置用于分配内存,并将其分配给指针。然后,在下一行中,您将指针指向不同的位置,因此您(a)泄漏了您分配的内存,并(b)将指针重定向到您不拥有的传入数据

msg->header.protocol_version = (char *) malloc(sizeof(char)*STRLEN);
msg->header.protocol_version = protocol_version;

您想要复制数据。

msg->header.protocol_version = (char *) malloc(sizeof(char)*STRLEN);
strncpy(msg->header.protocol_version, protocol_version, STRLEN);