実装がアドレスの任意の大きなリストを処理したいと仮定すると、次のようにすることができます:
struct hostent *gethostbyname(const char *name) {
static struct hostent *results = 0;
static size_t resultsize = 0;
size_t count = get_count_of_addresses(name)
if (count > resultsize) {
struct hostent *tmp = realloc(results, N * count + M);
if (tmp) {
results = tmp;
resultsize = count;
} else {
// handle error, I can't remember what the docs say
}
}
fill_in_hostent(results, name);
return results;
};
オプションで、ソケット ライブラリは、デバッグ ツールがメモリ リークを報告するのを避けるために、終了時に何かを解放する (ハンドラーresults
をインストールするなど) ことができます。atexit
構造体のサイジングとそれへの入力の間にアドレスの数が変化する可能性を無視しました.実際には、DNS結果を取得してからそれを処理するため、それは不可能です. DNS の結果に疑似コード表現が導入されるのを避けるために、2 つの別個の呼び出しとして残しました。