• Create Account

### #ActualiMalc

Posted 15 January 2013 - 12:57 AM

That sort of thing can't be detected at compile-time, in the general case.
Consider:
void foo(int x)
{
int *p;
if ((x % 2) == 0)
p = NULL;
else if ((x % 3) == 0)
p = new int(0);
else if ((x % 5) == 0)
p = new int[2];

if ((x % 7) == 0)
delete p;
else if ((x % 11) == 0)
delete[] p;
}
This will produce well-defined behaviour if I pass in say 4, 14, 21, 22, or 55, but will produce undefined behaviour if I pass in say 7, 33 or 35, and will leak memory if I pass in say 3.

### #2iMalc

Posted 15 January 2013 - 12:55 AM

That sort of thing can't be detected at compile-time, in the general case.
Consider:
void foo(int x)
{
int *p;
if ((x % 2) == 0)
p = NULL;
else if ((x % 3) == 0)
p = new int(0);
else if ((x % 5) == 0)
p = new int[2];

if ((x % 7) == 0)
delete p;
else if ((x % 11) == 0)
delete[] p;
}
This will produce well-defined behaviour if I pass in say 4, 14, 21, 22, or 55, but will produce undefined behaviour if I pass in say 7, 33 or 35, and will leak memory if I pass in say 10.

### #1iMalc

Posted 15 January 2013 - 12:54 AM

That sort of thing can't be detected at compile-time, in the general case.
Consider:
void foo(int x)
{
int *p;
if ((x % 2) == 0)
p = NULL;
else if ((x % 3) == 0)
p = new int(0);
else if ((x % 5) == 0)
p = new int[2];

if ((x % 7) == 0)
delete p;
else if ((x % 11) == 0)
delete[] p;
}
This will produce well-defined behaviour if I pass in say 4, 21, or 55, but will produce undefined behaviour if I pass in say 7, 33 or 35, and will leak memory if I pass in say 10.

PARTNERS