c文件中指针的初始值是多少

发布于 2024-08-24 05:43:23 字数 765 浏览 6 评论 0原文

在下面的代码中,是否可能导致一些内存泄漏?

reference-ril.c
static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
{
    ....
    ATResponse *p_response = NULL;
    ....
    err = at_send_command(cmd, &p_response);   // it's a memory leakage or not ?
    ....
    at_response_free(p_response);
    ....
}

而在其他函数中:

static void requestOrSendPDPContextList(RIL_Token *t)
{
    ATResponse *p_response;
    ....
    err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);  
    // it's a leakage or not ?
    ....
    at_response_free(p_response);
    ....
}

实际上,在某些情况下,这些函数会在调用 at_response_free(p_response) 之前返回。 我想我们首先要设置 ATResponse *p_response 为 NULL,对吗? 将指针设置为 NULL 是一个好主意吗?

In following code, is it possible cause some memory leaks?

reference-ril.c
static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
{
    ....
    ATResponse *p_response = NULL;
    ....
    err = at_send_command(cmd, &p_response);   // it's a memory leakage or not ?
    ....
    at_response_free(p_response);
    ....
}

And in other function:

static void requestOrSendPDPContextList(RIL_Token *t)
{
    ATResponse *p_response;
    ....
    err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);  
    // it's a leakage or not ?
    ....
    at_response_free(p_response);
    ....
}

Actually, these function will returned before calling at_response_free(p_response) in some cases.
I suppose we shout set ATResponse *p_response to NULL first, right ?
Set pointer to NULL is a good idea or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-08-31 05:43:23

这取决于:

  • 如果 at_send_command_multilineat_send_command 查看各自最后一个参数 (&p_response) 指向的值,那么您应该设置他们到可预测的值。这可能意味着您将 p_response 设置为 NULL。如果函数为指针分配内存而不查看初始值,那么就可以了。为了回答您的特定问题,函数中声明的变量除非声明为静态,否则没有默认值。
  • 如果 at_send_command* 函数始终为最后一个参数分配内存,那么您必须释放内存。如果他们仅在成功的情况下分配,那么您必须仅在成功的情况下释放。一个简单的规则是,对于每个 malloc()calloc(),都应该有一个 free()。 (realloc() 稍微改变了这一点,但您现在不需要担心它)。

换句话说,您需要查看 at_send_command* 函数的文档,或者查看函数的定义才能完全回答您的问题。

It depends:

  • if at_send_command_multiline and at_send_command look at the value pointed-to by their respective last arguments (&p_response), then you should set them to predictable values. This may mean that you set p_response to NULL. If the functions allocate memory for the pointer without looking at the initial value, then you are okay. To answer your particular question, a variable declared in a function, unless declared static, has no default value.
  • if the at_send_command* functions always allocate memory for the last argument, then you must free the memory. If they allocate only in the case of success, then you must free only in the case of success. A simple rule is that for every malloc() or calloc(), there should be a free(). (realloc() changes this a bit, but you shouldn't need to worry about it right now).

In other words, you need to look at the documentation of at_send_command* functions, or look in the definition of the functions to answer your questions fully.

悲念泪 2024-08-31 05:43:23

将指针设置为 null 当然是一个好主意;但这两种情况都不是内存泄漏。

C 中指针的初始值是垃圾,任何未初始化的变量的初始值也是垃圾。 (这是因为效率,或者我被告知,并且需要牢记。)

Setting the pointer to null is certainly a good idea; but neither case is a memory leak.

The initial value of a pointer in C is garbage, as is the initial value of any variable that isn't initialized. (This is because of efficiency, or so I'm told, and needs to be kept in mind.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文