åçå¤æ°(new, delete)
newæ¼ç®åã§åçå¤æ°ãçæã§ãã¾ããåçå¤æ°ã使ãã¨ãããã°ã©ã ã§å¿ è¦ã«ãªã£ãæã«åçã«ã¡ã¢ãªã確ä¿ãããã¨ãã§ãã¾ãã(é常ã¯ã¹ã¿ãã¯é åã«ç¢ºä¿ãããã¡ã¢ãªãããã¼ãé åã«ç¢ºä¿ããã¨è§£éã)
- ãnew <å>;ãã§ã¡ã¢ãªã確ä¿ãããã®ãã¤ã³ã¿ãè¿ãã
- å¤æ°ã«ã¯ãã¤ã³ã¿ãä»ãã¦ã¢ã¯ã»ã¹ããã
- 確ä¿ããé åã¯ããdelete <ãã¤ã³ã¿>;ãã§éæ¾ããã
- é åã®å ´åã¯deleteã®å¾ã«[]ãã¤ããã
- å¿ããã¨ã©ããªãã®ã?ã¡ã¢ãªãªã¼ã¯?ããã°ã©ã ãçµäºãããéæ¾ããã?(âJavaããã°ã©ãçå¸æç観測ããã)
- ã¡ã¢ãªä¸è¶³ã§é åã確ä¿ã§ããªãå ´åã0ãè¿ãããã
- ãã¸ã!
#include <iostream> // ã¡ã¤ã³é¢æ° int main() { // åçå¤æ° int* i = new int; *i = 10; // åçå¤æ°ã«ããé å int* array = new int[*i]; for ( int j = 0; j < *i; j++ ) { array[j] = j * 100; } std::cout << *i << std::endl; std::cout << array[5] << std::endl; // å¤æ°ãåé¤ delete i; delete [] array; // deleteã®å¾ã«[]ãã¤ããã return 0; }
å®è¡çµæã§ãã
10 500