strdup():对警告感到困惑('隐式声明','使指针......没有强制转换',内存泄漏)

ice*_*man 3 c valgrind strdup

当我编译下面的一小段代码(我们在其中定义一个字符串,然后使用strdup进行复制)时,我得到3个警告:来自GCC的2个编译器警告和来自valgrind的1个运行时警告/错误.

我怀疑内存泄漏错误(由valgrind报告)也与我使用strdup有关,这就是我在下面包含相关输出的原因.

我究竟做错了什么?(我正在通过一本C书,这就是作者使用strdup的方式.)


代码:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
  char *string1 = "I love lamp";
  char *string2;

  string2 = strdup(string1);

  printf("Here's string 1: %s\n"
     "Here's string 2: %s\n",
     string1, string2);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

警告/输出:

dchaudh@dchaudhUbuntu:~/workspaceC/LearnCHW/Ex17_StructsPointers$ make test
cc -std=c99    test.c   -o test
test.c: In function ‘main’:
test.c:9:3: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
   string2 = strdup(string1);
   ^
test.c:9:11: warning: assignment makes pointer from integer without a cast [enabled by default]
   string2 = strdup(string1);
           ^
dchaudh@dchaudhUbuntu:~/workspaceC/LearnCHW/Ex17_StructsPointers$ valgrind --track-origins=yes --leak-check=full ./test
==3122== Memcheck, a memory error detector
==3122== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3122== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==3122== Command: ./test
==3122== 
Here's string 1: I love lamp
Here's string 2: I love lamp
==3122== 
==3122== HEAP SUMMARY:
==3122==     in use at exit: 12 bytes in 1 blocks
==3122==   total heap usage: 1 allocs, 0 frees, 12 bytes allocated
==3122== 
==3122== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3122==    at 0x4C2ABBD: malloc (vg_replace_malloc.c:296)
==3122==    by 0x4EBF2B9: strdup (strdup.c:42)
==3122==    by 0x4005A4: main (in /home/dchaudh/workspaceC/LearnCHW/Ex17_StructsPointers/test)
==3122== 
==3122== LEAK SUMMARY:
==3122==    definitely lost: 12 bytes in 1 blocks
==3122==    indirectly lost: 0 bytes in 0 blocks
==3122==      possibly lost: 0 bytes in 0 blocks
==3122==    still reachable: 0 bytes in 0 blocks
==3122==         suppressed: 0 bytes in 0 blocks
==3122== 
==3122== For counts of detected and suppressed errors, rerun with: -v
==3122== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)

AnT*_*AnT 19

C标准库没有这样的功能strdup.然而,这种流行的功能通常由标准库实现作为扩展提供.在GCC实现中<string.h>,声明了这个函数,你可以包含它.

但是,当您使用更严格的标准设置编译代码时-std=c99,编译器会隐藏标准库头中的非标准函数声明.这就是strdup您案件中的声明.您收到的警告是当您尝试调用未声明的函数时发出的典型警告.从形式上看,从C99的角度来看,这是一个错误,但是在这种情况下,您的编译器确定警告就足够了.如果-std=c99从编译器的命令行中删除该开关,则声明strdup将变为可见,并且代码将在没有该警告的情况下编译.

从技术上讲,-std=c99在命令行中指定使GCC定义__STRICT_ANSI__宏,这会导致所有非ANSI函数声明从标准头中"消失".

该函数仍然存在于库中,这就是您的代码正确链接的原因.请注意,它不一定正常运行,因为编译器假定strdup返回一个int,而实际上它返回一个指针.

valgrind报告只是内存泄漏的结果.当你不再需要它时strdup,分配你应该free自己的内存.

  • +1“虽然 [`strdup()`](http://linux.die.net/man/3/strdup) 在 `&lt;string.h&gt;` 中声明,但它*不是*标准 C 提供的库函数”。抱歉,我很难解析第一句话。 (3认同)
  • "真正的"参考:http://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html @WhozCraig (2认同)

alk*_*alk 5

strdup() 不是标准C。它是POSIX扩展

strdup()在使用该选件的情况下即使针对GCC严格遵守C99要求也要提供该功能,-std=c99#define至少需要以下一项:

_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 
  || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
  || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
Run Code Online (Sandbox Code Playgroud)

(摘自strdup()的Linux手册页

例如,通过编码(包括之前<string.h>):

#define _SVID_SOURCE
Run Code Online (Sandbox Code Playgroud)

要么:

#define _POSIX_C_SOURCE 200809L
Run Code Online (Sandbox Code Playgroud)

另外,您可以通过GCC的命令行将这些定义作为选项传递

-D_SVID_SOURCE
Run Code Online (Sandbox Code Playgroud)

要么

-D_POSIX_C_SOURCE=200809L
Run Code Online (Sandbox Code Playgroud)