警告:関数はローカル変数のアドレスを返します



Warning Function Returns Address Local Variable




警告:この関数はローカル変数を返します。



ローカル関数を自分で作成するときに、関数型に戻り値がある場合、戻り値がローカル変数の場合、警告がポップアップ表示されます。機能を実行するとメモリが解放されるためです。

3つの変数の説明:



@interface Person : NSObject { // Member variables: // Variables written in curly braces in class declarations, we call them member variables (attributes, instance variables) // Member variables can only be accessed through objects // Note: Member variables cannot leave the class, they are not member variables after leaving the class. Member variables cannot be initialized at the same time as they are defined // Storage: Heap (in the storage space of the heap corresponding to the current object) // The data stored in the heap will not be automatically released, it can only be released manually by the programmer int age } @end @implementation Person @end // Global variables: // Variables written outside functions and braces, we call them global variables // Scope: from the defined line, all the way to the end of the file // Global variables can be defined first, or they can be initialized at the same time // Storage: static area // The storage space will be allocated as soon as the program starts, and will not be released until the end of the program int a int b = 10 int main(int argc, const charchar * argv[]) { // Local variables: // Variables written in functions or code blocks, we call them local variables // Scope: from the defined line, until the brace or return is encountered // Local variables can be defined and initialized first, or they can be initialized simultaneously // Storage: stack // The data stored in the stack has a characteristic, the system will automatically release it for us int num = 10 { int value } return 0 }

解決策:(1)戻り値を静的として宣言します。

(2)malloc、覚えておいてください

(3)受信パラメータ