As I understand it, this code would lead to a segfault in Linux and probably unexpected behaviour in OSes where there is no concept of "protected memory". Am I right?
Code:
char *str; /* Don't initialize the pointer - it is initialized to some random memory location */
strcpy (str, "something is the problem"); /* Assign the pointer */
Now this code:
Code:
void func1 ()
{
char *str = new char[50];
func2 (str);
strcpy (str, "Something else");
}
void func2 (char* value)
{
delete[] value; /* delete is called on a pointer that's got as a parameter */
}
This would also lead to unexpected or dangerous consequences, right?
Or a classic memory leak scenario:
Code:
int* array = new int[100];
...
...
int* newarray = new int[10];
array = newarray; /* the old array is lost */
Can you think of any more situations where pointers would lead to memory leaks or other complex, hard to resolve problems?