MSDN一句英文看不大明白

来源:百度知道 编辑:UC知道 时间:2024/05/17 08:44:53
Because _strdup calls malloc to allocate storage space for the copy of strSource, it is good practice always to release this memory by calling the free routine on the pointer returned by the call to _strdup.

_strdup是个函数,strSource是参数
calls malloc to分配存储空间为strSource,这是个良好的习惯经常释放这块内存by……(后面就不知道怎么翻了,理不清楚。)
我不是机器翻译的……我自己按照字面意思写的。后面没看懂就没整理……我英语不好,拍我吧。
// crt_strdup.c

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

int main( void )
{
char buffer[] = "This is the buffer text";
char *newstring;
printf( "Original: %s\n", buffer );
newstring = _strdup( buffer );
printf( "Copy: %s\n", newstring );
free( newstring );
}Output
Original: This is the buffer text
Copy: This is the buffer text
例子挺简单的,我是看懂了。

请不要使用机翻,谢谢……

翻译是这样的:
因为_strdup是调用malloc这个函数去为strSource参数申请内存创建一份副本的,所以你需要调用free函数来释放这个由_strdup返回的指针

说白了就是你要自己手动来释放_strdup返回的指针(这个指针指向一个内存区,这个内存区是通过malloc来申请的,所以只能通过free来释放)

=================================================

T_T 你们别这样……

因为 _strdum函数调用malloc函数来为strSource分配存储空间, 所以, 你应该总是通过调用free来释放_strdum返回的指针指向内存, 这是一个好习惯.

楼上误会, 我没有用机器翻译.
char *str =_strdum(...);

free str;// 英文的意思就是叫你别忘了这句话.

_strdup 调用malloc为拷贝的字符串资源分配内存,并需要通过free来释放_strdup返回的指针占用的内存.