无法从被调用的函数返回更新的字符串并释放内存

时间:2015-08-10 16:22:51

标签: c pointers memory-management

我有一个带有两个字符串的简单程序,我应该在被调用函数中连接,return连接到调用函数。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


void call_func(char *str1,char *str2)
{

  char *msg_concat;
  asprintf(&msg_concat,"%s%s", str1, str2);
  printf("%s",msg_concat);
  str2 = msg_concat;

}



int main()
{
    char *str1="new" ;

    char *full_msg ="test";
    call_func(str1,full_msg);
    printf("value of full_msg=%s",full_msg);
    return 0;

}

我使用asprintf()进行连接,但是在main函数中没有返回值。因为我使用了指针,不是吗 预计会返回更改后的值

此外,在分配指针时,在我的理解中,复制引用而不是值。现在我需要释放 分配内存msg_concat否则会出现内存泄漏。如果函数使用指针并返回,我应该如何释放内存 引用调用函数??

在被调用函数的最后一行中,

free(msg_concat)无效 作为 -

void call_func(char *str1,char *str2)
{

  char *msg_concat;
  asprintf(&msg_concat,"%s%s", str1, str2);
  printf("%s",msg_concat);
  str2 = msg_concat;
   free(msg_concat);

}

实际上在我最近的项目中,我有调用函数

的场景
GSList *parsed_msg = LineParser(dev_ip,encoded_msg, "\n", file_path, unique_id);

,被调用的函数是

GSList *LineParser(char* dev_ip, char *msg_full, char *separator, char *file_path, int unique_id)
{
    GSList *parsed_msg = NULL;
    char connection_id[50];
    sprintf(connection_id,"%s|%d", dev_ip, unique_id);
    char *msg_concat;


    // inserting {file_path : last_not_complete_line} logic to TCP_CACHE
    //and removing the correspoing last_line
    g_mutex_lock (&mutex_hash_main);

    // char *last_line = (char *) (g_hash_table_lookup((GHashTable *) g_hash_table_lookup(TCP_CACHE, connection_id), file_path));

    GHashTable *t_filepath_msg_dict = NULL;         //for {file_path : last_not_complete_line}
    if (TCP_CACHE != NULL)
    {
        t_filepath_msg_dict = (GHashTable *)(g_hash_table_lookup(TCP_CACHE, connection_id));
        if (t_filepath_msg_dict != NULL)
        {
            char *last_line = (char *) (g_hash_table_lookup(t_filepath_msg_dict, file_path));

            if(last_line != NULL)   //if the hash has device ip, append the value to msg
            {
                zlog_debug(c,"concatenating: str1: %s and str2: %s\n", last_line, msg_full);
                asprintf(&msg_concat,"%s%s", last_line, msg_full);


                //msg_concat = concat(last_line,msg_full);
                g_hash_table_remove(t_filepath_msg_dict, file_path);

                msg_full = msg_concat;

            }   
        }

    }


    int msg_len = strlen(msg_full);
    char last_char = msg_full[msg_len - 1];
    zlog_debug(c, "len of message: %d", msg_len);
    zlog_debug(c, "last char is : %c", last_char);
    char *token=NULL;
    char *remaining_str=NULL;
    token = strtok_r(msg_full, "\n", &remaining_str);

    while(token != NULL)
    {
        if(token[0]==' ')
        {
            token = trimwhitespace_parser (token);
            if(strcmp(token,"")==0)
            {
                //insert this token to GSList
                parsed_msg = g_slist_prepend (parsed_msg, token);
                token = strtok_r(NULL, "\n", &remaining_str);
                continue;
            }
        }
        if(strcmp(remaining_str,"")==0)
        {
            if(strlen(token) > 10000)
            {
                zlog_warn(c, "Message too big(more than 10000 len). Stop looking for new line and process msg");
                g_hash_table_remove(t_filepath_msg_dict, file_path);
            }
            else
            {
                if(last_char=='\n')
                {
                    //new line is the last character. do nothing
                    zlog_debug(c, "last character is new line");
                }
                else
                {
                    zlog_debug(c, "last character is not new line");
                    //new line not received
                    if (t_filepath_msg_dict == NULL)        //insert new record
                    {
                        GHashTable *each_filepath_msg_dict = g_hash_table_new_full(g_str_hash, g_str_equal, key_str_destroy_cb_parser, value_str_destroy_cb_parser);
                        zlog_debug(c,"Inserting file_path: %s to connection_id: %s", file_path, connection_id);

                        g_hash_table_insert(each_filepath_msg_dict, strdup(file_path), strdup(token));
                        g_hash_table_insert(TCP_CACHE, strdup(connection_id), each_filepath_msg_dict);
                    }
                    else        //update existing record
                    {
                        zlog_debug(c,"Connection_id :%s is already found; appending/replacing file_path :%s", connection_id, file_path);
                        g_hash_table_insert(t_filepath_msg_dict, strdup(file_path), strdup(token));
                    }
                    g_mutex_unlock(&mutex_hash_main);
                    return parsed_msg;
                }
            }
        }
        //insert token to GSList
        parsed_msg = g_slist_prepend (parsed_msg, token);
        token = strtok_r(NULL, "\n", &remaining_str);
    }
    g_mutex_unlock(&mutex_hash_main);
    return parsed_msg;
}

我已经看到我有内存泄漏的问题,因为我必须释放msg_concat,因为在给定的答案中,据说改变的值不会返回到调用函数。哪里是释放asprintf msg_concat指针的适当位置???

2 个答案:

答案 0 :(得分:1)

由于C在代码

中使用pass-by-value进行函数参数传递
str2 = msg_concat;
关于将值返回main()

几乎没用。相反,您可能希望使用strcpy()将内容复制到str2指向的内存区域,这将反映在main()中。

此外,在复制内容后,您可以在free()结束前msg_concat call_func()

现在,即使这个也无法解决您的问题,因为您使用第二个参数作为字符串文字来调用call_func()。这在两个方面是非法的

  • 修改字符串文字将导致UB。
  • 目标字符串(在strcpy()中使用)没有足够的内存来容纳最终输出。

解决方案:解决此问题的可能方法是

full_msg的类型更改为固定长度数组,该数组具有足够的长度来保存连接输出,例如

char full_msg[32] = "test";

然后,通过像

这样的调用
call_func(str1,full_msg);

在内部,使用strcpy(str2, msg_concat);,您可以实现目标。

此外,您还可以使用free(msg_concat);

答案 1 :(得分:1)

指针按值传递,因此最后一个赋值对调用者没有影响。有两种方法可以做你想做的事:

  • 将指针传递给str2
  • 的指针
  • 将新字符串返回给调用者。

返回结果对函数的调用者有一个更明确的外观,但指针指针方法也是有效的。

您需要解决的另一个问题是释放传递给函数的指针。如果要释放值,则无法传递字符串文字:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char* call_func(char *str1,char *str2) {
    char *msg_concat = NULL;
    asprintf(&msg_concat,"%s%s", str1, str2);
    printf("%s",msg_concat);
    free(str2);
    return msg_concat;
}

int main() {
    char *str1="new" ;
    char *full_msg =strdup("test"); // Put the string in dynamic memory
    full_msg = call_func(str1, full_msg);
    printf("value of full_msg=%s", full_msg);
    free(full_msg);
    return 0;
}