Deleting Pointers

Hi do i need to delete the pointer.

1
2
3
4
5
6
7
8
9
10
11
void test(){
    char* pChar;
    pChar = new char[200];
    strcpy(pChar, "ffsafafafa");
}

int main(){
    test();
    // Is the pChar pointer is in the memory?
    return 1;
}
Last edited on
You'd add just before the closing brace on line 5, delete[] pChar;
Line 9: No, it's not.
After the test function, is the pChar pointer automatically deleted or do i need to delete it?
It is not automatically deleted. You have to delete it.

Generally speaking, every new in your code should have a corresponding delete somewhere (though that doesn't mean you have to have an equal number of news and deletes).
Topic archived. No new replies allowed.