gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:50 25 4
gpt4 key购买 nike

我有一个包含两个字符串的简单程序,我应该将它们连接到被调用函数中并返回到调用函数。

#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() 进行连接,但主函数中未返回该值。正如我使用的指针,是不是期望返回改变后的值??

此外,在分配指针时,根据我的理解,复制的是引用而不是值。现在我需要释放分配内存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 指针的适当位置在哪里???

最佳答案

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

  • 将指针传递给 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;
}

关于c - 无法从被调用函数返回更新后的字符串并释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com