English 中文(简体)
为什么这个MSVC API是这样定义的?
原标题:Why this MSVC API is defined this way?
  • 时间:2011-05-29 03:59:26
  •  标签:
  • c
  • windows
errno_t _dupenv_s(
   char **buffer,
   size_t *sizeInBytes,
   const char *varname
);

我对此有几个问题:

  1. why a pointer to pointer(**) is required instead of a pointer(*)?
  2. why sizeInBytes is necessary,isn t that available by strlen(buffer)?
最佳回答

不,它要求一个字符指针的地址。该方法将分配必要的空间来保存变量的值,并将指针的值设置为NULL。请参阅下页的示例代码:

http://msdn.microsoft.com/en-us/library/ms175774(v=VS.80).aspx

问题回答

在msvc下,任何以<code>_s</code>为后缀的函数都是安全函数,这意味着它不对传递的数据的完整性进行假设。正是因为这个原因,字符串的长度是必需的,因为你不能假设它以null结尾,或者根本以null结尾(你可能只想要一半的字符串,但这是副作用)。

安全函数都返回错误代码,因此可以在没有错误的情况下进行检查,因此任何返回都需要通过指针发送。并将其视为strdup&dupnv返回一个char*,您将获得一个双向间接寻址,以便指向您传递的变量的指针获得分配的缓冲区的地址。

在此上下文中,类型char**是指向chars数组的指针的指针。(它也可以指指向单个char的指针的指针,但这不是它与_dupenv_s()一起使用的方式)。

_dupenv_s()函数通过要求操作系统保留足够大的内存块来分配char的数组。操作系统保留一块内存,并将新分配的char数组的地址提供给_dupenv_s()函数。_dupenv_s()函数将此数组的地址存储到char*变量中,因为它是指向chars数组的指针。

现在,函数必须将这个<code>char*</code>值传递给调用者,以便调用代码可以使用它。返回值已经用于返回错误代码,因此无法使用。但假设调用者有一个<code>char*</code>变量,准备接收分配缓冲区的地址。

如果_dupenv_s()函数知道调用方的char*变量的位置,则该函数可以继续使用正确的值填充调用方的char*。为此,调用者需要传递调用者的char*变量的地址。也就是说,您需要传递一个指向字符数组的指针的指针。这意味着必须传递char**

请注意,这也是sizeInBytessize_t*的原因。调用者有一个变量size_t,调用者可以将变量的地址作为size _t*传递给函数,以便函数可以用正确的值填充变量。

虽然strlen(buffer)==sizeInBytes可能是真的,但strlen()函数通过计算字符数来确定字符串的长度,直到它看到一个null终止符。strlen()完成所需的时间与字符数成线性关系,即它不是常数。为什么不跳过要求调用者这样做的麻烦,直接提供大小呢?


如果指针仍然让你感到困惑(有时也会让人感到困惑),此堆栈溢出答案可能会有所帮助。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签