3

strcpy()strcat()のマニュアルを調べていました。関数呼び出しの「成功」を評価する方法がないようです。(つまり、戻り値が になることはありませんNULL)、それは正しいですか?

これらの関数の入力のルールに従えば、出力が有効になると想定されていますか? ここで何も見逃していないことを確認したかっただけです…</p>

4

3 に答える 3

5

これらの関数は、明確に定義された方法で失敗することはありません。それらは成功するか、事態がひどく悪化し (例: 0 文字が欠落している、出力バッファーが小さすぎる)、何かが起こる可能性があります。

于 2012-10-31T20:17:36.017 に答える
1

Because I don't think functions like that really have any way of knowing what "success" is. For instance, strcpy is really just memcpy. So as far as C is concerned, it is just taking data from one memory location and copying it to another. It doesn't know how the data is supposed to look, or be formatted in ways you expect. I guess the only real way you'd know if there is a success or not is if you end up getting a segfault or not.

于 2012-10-31T20:18:49.893 に答える
1

These functions are guaranteed to work, provided that you're not invoking undefined behaviour. In particular, the memory that you're writing to needs to be allocated. There is really no way for them to fail except crashing your program.

Generally, because it can be hard to tell how many bytes will be written, use of these functions is discouraged. Use strncpy and strncat if you can.

于 2012-10-31T20:18:59.847 に答える