I never thought of this but I'm a bit curious but what if I initialize a pointer inside an if-else statement like this?
if (true)
{
int *p=(int*)malloc (sizeof (int));
} // Will p be freed here?
// Can't free (p) here since it is not in this scope
I think the code tells pretty much everything about my question... I actually have a thought like this, it is necessary to free(p) at the end of if statement, but what if it's inside a loop?
for (int x=0; x<5; x++)
{
int *p=(int*)malloc (sizeof (int));
// Some code here
free (p); // Will p deallocated 5 times or just once?
}
Aucun commentaire:
Enregistrer un commentaire