c文件中指针的初始值是多少
在下面的代码中,是否可能导致一些内存泄漏?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于:
at_send_command_multiline
和at_send_command
查看各自最后一个参数 (&p_response
) 指向的值,那么您应该设置他们到可预测的值。这可能意味着您将p_response
设置为NULL
。如果函数为指针分配内存而不查看初始值,那么就可以了。为了回答您的特定问题,函数中声明的变量除非声明为静态,否则没有默认值。at_send_command*
函数始终为最后一个参数分配内存,那么您必须释放内存。如果他们仅在成功的情况下分配,那么您必须仅在成功的情况下释放。一个简单的规则是,对于每个malloc()
或calloc()
,都应该有一个free()
。 (realloc()
稍微改变了这一点,但您现在不需要担心它)。换句话说,您需要查看 at_send_command* 函数的文档,或者查看函数的定义才能完全回答您的问题。
It depends:
at_send_command_multiline
andat_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 setp_response
toNULL
. 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 declaredstatic
, has no default value.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 everymalloc()
orcalloc()
, there should be afree()
. (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.将指针设置为 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.)