我应该释放setlocale返回的指针吗? [英] Should I free the pointer returned by setlocale?

查看:105
本文介绍了我应该释放setlocale返回的指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(int argc, char *argv[])
{
    char *ret = setlocale(LC_ALL, NULL);
    // should I free 'ret' ???
    // free(ret);
    return 0;
}

我在Linux和OS X 10.10上都尝试过,在Linux上,我不能叫"free",但是在OS X上,如果我不叫"free",则valgrind会抱怨内存泄漏.

I've tried both on Linux and OS X 10.10, on Linux, I must not call 'free', but on OS X, if I do not call 'free', valgrind complains a memory leak.

==62032== Memcheck, a memory error detector
==62032== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==62032== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==62032== Command: ./a.out
==62032== 
--62032-- ./a.out:
--62032-- dSYM directory is missing; consider using --dsymutil=yes
==62032== 
==62032== HEAP SUMMARY:
==62032==     in use at exit: 129,789 bytes in 436 blocks
==62032==   total heap usage: 519 allocs, 83 frees, 147,421 bytes allocated
==62032== 
==62032== 231 bytes in 1 blocks are definitely lost in loss record 63 of 91
==62032==    at 0x10000859B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==62032==    by 0x1001E68C8: currentlocale (in /usr/lib/system/libsystem_c.dylib)
==62032==    by 0x100000F6B: main (in ./a.out)
==62032== 
==62032== LEAK SUMMARY:
==62032==    definitely lost: 231 bytes in 1 blocks
==62032==    indirectly lost: 0 bytes in 0 blocks
==62032==      possibly lost: 0 bytes in 0 blocks
==62032==    still reachable: 94,869 bytes in 10 blocks
==62032==         suppressed: 34,689 bytes in 425 blocks
==62032== Reachable blocks (those to which a pointer was found) are not shown.
==62032== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==62032== 
==62032== For counts of detected and suppressed errors, rerun with: -v
==62032== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 17)

所以, 在Linux中,如果我称之为免费",它将崩溃. 在OS X中,如果我不称之为免费",则会发生内存泄漏.

So, in Linux, if I call 'free', it will crash. in OS X, if I do not call 'free', it has a memory leak.

推荐答案

您应该 free所获得的字符串.根据C11标准:

You should not free the string that you get. According to the C11 standard:

7.11.1.1 setlocale函数

指向由字符串返回的字符串的指针 setlocale 功能使得后续调用 该字符串值及其关联的类别将恢复该程序的该部分 语言环境. 指向的字符串不得由程序修改,但可以 后续调用覆盖了 setlocale 功能

The pointer to string returned by the setlocale function is such that a subsequent call with that string value and its associated category will restore that part of the program’s locale. The string pointed to shall not be modified by the program, but may be overwritten by a subsequent call to the setlocale function

另外,Linux 手册页表示:

Additionally, the Linux man pages say:

此字符串可以分配在 静态存储.

This string may be allocated in static storage.

如果尝试free,将导致程序崩溃.

which would result in your program crashing if you tried to free it.

看起来Linux实现使用静态存储,但是OSX使用malloc.无论引擎盖发生了什么,您都不应对其进行修改,因为标准不允许您这样做--- OSX上的安全性是一个实现怪癖,您应该忽略. Valgrind本质上是在给您一个误报.

It looks like the Linux implementation uses static storage, but the OSX one uses malloc. Regardless of what's going on under the hood, you should not modify it because the standard disallows you from doing so --- the fact that it would be safe on OSX is an implementation quirk you should ignore. Valgrind is essentially giving you a false positive here.

这篇关于我应该释放setlocale返回的指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆