P
Philipp.Weissenbacher
Hi all!
This is most certainly a total newbie question, but why doesn't the
following code cause a segfault?
void insertion_sort(int a[], int length)
{
int i;
for (i=0; i < length; i++)
{
int j, v = a;
for (j = i - 1; j >= 0; j--)
{
if (a[j] <= v) {
cout << a[j] << " <= " << v << endl;
break;
}
a[j + 1] = a[j];
}
a[j + 1] = v;
}
}
IMHO the segfault should occur at line 9 as on the first pass through
the first for-loop i = o ergo j is -1 causing a[j] to provoke a
segfault.
Thanks in advance and excuse me for asking such a dumb question
This is most certainly a total newbie question, but why doesn't the
following code cause a segfault?
void insertion_sort(int a[], int length)
{
int i;
for (i=0; i < length; i++)
{
int j, v = a;
for (j = i - 1; j >= 0; j--)
{
if (a[j] <= v) {
cout << a[j] << " <= " << v << endl;
break;
}
a[j + 1] = a[j];
}
a[j + 1] = v;
}
}
IMHO the segfault should occur at line 9 as on the first pass through
the first for-loop i = o ergo j is -1 causing a[j] to provoke a
segfault.
Thanks in advance and excuse me for asking such a dumb question