why pointer to pointer is needed to allocate memory in function

前端 未结 9 1791
难免孤独
难免孤独 2020-12-28 19:08

I have a segmentation fault in the code below, but after I changed it to pointer to pointer, it is fine. Could anybody give me any reason?

void memory(int *          


        
9条回答
  •  臣服心动
    2020-12-28 20:06

    i corrected the code.. read the comment and all will be clear..

    #include
    #include
    #include
    
    using namespace std;
    
    
    void memory(int* p, int size) {               //pointer to pointer` not needed
        try{
            p = new int[size] ;
        } catch(bad_alloc &e) {
           cout<

提交回复
热议问题