Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
そのような関数はダングリング ポインターになりますか?
int *foo () { int a[2] = {2, 3}; int *p = NULL; p = a; return p; }
はい。自動保存期間を持つ配列へのポインタを返しています。関数が返すとき、ポインターは無効です。
自動ローカル変数へのポインターを返さないでください。 あなたの場合、変数aは一度返されると存在しないfooため、変数へのポインターは無効になります(未定義の動作を呼び出します)。
a
foo