【问题标题】:Should I free the pointer returned by setlocale?我应该释放 setlocale 返回的指针吗?
【发布时间】:2015-05-20 21:36:53
【问题描述】:
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 会抱怨内存泄漏。

==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 中,如果我不调用 'free',它就有内存泄漏。

【问题讨论】:

  • 你试过free()吗??
  • 是的,理想情况下你应该释放那个......
  • @Karthikeyan.R.S,我的困惑是 linux 和 os x 之间的行为不同。
  • @noinput 也许吧。您可以尝试使用free() 的建议。然后发布。

标签: c linux macos valgrind setlocale


【解决方案1】:

你应该free你得到的字符串。根据C11标准:

7.11.1.1 setlocale 函数

返回的字符串指针 setlocale 函数是这样的,后续调用 使用该字符串值及其相关类别将恢复程序的该部分 语言环境。 程序不能修改所指向的字符串,但可以 被后续调用覆盖 setlocale 功能

另外,Linux man pages 说:

这个字符串可以分配在 静态存储。

如果您尝试free 它会导致您的程序崩溃。

看起来 Linux 实现使用静态存储,但 OSX 使用malloc。不管幕后发生了什么,您不应该修改它,因为标准不允许您这样做 --- 它在 OSX 上是安全的这一事实是您应该忽略的实现怪癖。 Valgrind 本质上是在这里给你一个误报。

【讨论】:

    猜你喜欢
    • 2010-09-14
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多