I’m investigating a memory leak and from what I see, the problem looks like this:
int main(){
char *cp = 0;
func(cp);
//code
delete[] cp;
}
void func(char *cp){
cp = new char[100];
}
At the //code comment, I expected cp to point to the allocated memory, but it still is a null pointer meaning I never delete the memory. What am I doing wroing?
In this function, char *cp is a “pointer being passed by copy” what means that they are pointing to the same memory address but they are not the same pointer. When you change the pointer inside, making it to point to somewhere else, the original pointer that has been passed will keep pointing to 0.