简体   繁体   English

离开 function 后结构变量值发生更改

[英]Struct variable values are altered after leaving the function

I am struggled to write a correct title for the post.我很难为这篇文章写一个正确的标题。 Forgive me if it is not 100% accurate.如果它不是 100% 准确,请原谅我。 Because, the initial issue was just freeing a malloc ed output without disturbing where it is assigned to.因为,最初的问题只是释放malloc和 output 而不会干扰它被分配到的位置。 I then decided to copy the source ( encrypt_Data ) into another variable before I free it.然后我决定在释放它之前将源( encrypt_Data )复制到另一个变量中。 And then another issue arose this time.然后这次又出现了一个问题。 It is where I am.这就是我所在的地方。 If I can find a proper solution at least for one of them it would be great.如果我至少能为其中一个找到合适的解决方案,那就太好了。

Issue#1问题#1

 typedef struct {
      const char*             sTopic;
      const char*             pData;
    } CLIENT_MESSAGE;
CLIENT_MESSAGE Publish;

char * pData = "Hello World!";
char * encrypt_Data = Encrypt_Data_Base64(pData);
Publish.pData = encrypt_Data;
free(encrypt_Data);

If I free the encrypt_Data , Publish.pData is also freed (as they are just a pointer and pointing to the same memory location).如果我释放encrypt_DataPublish.pData也会被释放(因为它们只是一个指针并指向同一个 memory 位置)。 Note that: The function Encrypt_Data_Base64 has several several nested function called underneath and it has malloc ed output. Note that: The function Encrypt_Data_Base64 has several several nested function called underneath and it has malloc ed output. This is why I try to free the memory sourced from there.这就是我尝试释放从那里采购的 memory 的原因。

And then I decided to make a copy of the encrypt_Data so I can then free it freely.然后我决定制作一份encrypt_Data的副本,这样我就可以自由地释放它了。

Issue#1 solving attempt问题#1 解决尝试

char * pData = "Hello World!";
char * encrypt_Data = Encrypt_Data_Base64(pData);
// ------- addition starts ------
int len = strlen(encrypt_Data);
char temp[len+1];
char * pTemp = temp;
memcpy(pTemp, encrypt_Data, len+1);
pTemp[len] = '\0';
// ------- addition ends------
Publish.pData = pTemp
free(encrypt_Data);

Struct variable value preserved well.结构变量值保存得很好。 So far so good.到目前为止,一切都很好。 And then I have to pass the struct to a library function (I don't have source code for it).然后我必须将结构传递给库 function(我没有它的源代码)。

Issue#2问题#2

CLIENT_Publish(&Publish); // This is how it supposed to be.
//Prototype: int CLIENT_Publish(CLIENT_MESSAGE* pPublish);

And this time, when I debug, as soon as my current function is left and just called that one before doing anything else, the struct value has been altered.而这一次,当我调试时,只要我当前的 function 离开并在做任何其他事情之前调用那个,结构值就被改变了。 I assumed this might be related to non-terminated string.我认为这可能与非终止字符串有关。 I therefore added NUL termination as you may see in the solving attempt above.因此,我添加了 NUL 终止,正如您在上面的解决尝试中看到的那样。 But it didn't help.但这没有帮助。

Array content before leaving the function (required block is between 0 and 12)离开 function 之前的数组内容(需要的块在 0 到 12 之间) 在此处输入图像描述

Array content when entering the other function (CLIENT_Publish)进入其他function(CLIENT_Publish)时的数组内容

在此处输入图像描述

Since I can't do much about the library part, I have to do something in the part I can control.由于我不能对库部分做太多事情,所以我必须在我可以控制的部分做一些事情。

EDIT: If I get my value without using this line char * encrypt_Data = Encrypt_Data_Base64(pData);编辑:如果我在不使用这一行的情况下获得我的价值char * encrypt_Data = Encrypt_Data_Base64(pData);

for example;例如;

AFunction_GetPtr(&pData);
Publish.pData = pData;
CLIENT_Publish(&Publish);

This way, it works nice and easy.这样,它工作得又好又容易。 But, I like to intercept the value coming from the AFunction_GetPtr and use it in Encrypt_Data_Base64 and then pass it to CLIENT_Publish .但是,我喜欢截取来自AFunction_GetPtr的值并在Encrypt_Data_Base64中使用它,然后将其传递给CLIENT_Publish

Any input highly appreciated.任何输入高度赞赏。

It is not the correct solution, but the simplest thing for you to do right now is:这不是正确的解决方案,但您现在要做的最简单的事情是:

char * pData = "Hello World!";
char * encrypt_Data = Encrypt_Data_Base64(pData);
Publish.pData = strdup(encrypt_Data);
free(encrypt_Data);

Now that you've made another copy of the data, you'll need to eventually free it.现在您已经制作了数据的另一个副本,您最终需要释放它。 So you might as well just do:所以你不妨这样做:

char * pData = "Hello World!";
char * encrypt_Data = Encrypt_Data_Base64(pData);
Publish.pData = encrypt_Data;
/* Do not free(encrypt_Data); */

Just remember to free Publish.pData when you no longer need it.请记住在不再需要时释放Publish.pData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM