# Newbie question: Why does this work?

Discussion in 'C Programming' started by Philipp.Weissenbacher@gmail.com, May 16, 2008.

1. ### Guest

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

, May 16, 2008

2. ### santoshGuest

wrote:

> 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

The second for loop will be entered only when it's condition is true,
i.e., only when j is >= 0. This will not happen after the first
iteration of the outer loop. During the second iteration of the outer
loop, j will be initialised to 0 so the code under the inner for loop
will be executed.

santosh, May 16, 2008

3. ### Walter RobersonGuest

In article <>,
<> wrote:

>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;

The above line will not compile, as you have not defined a variable
named 'cout', nor a variable named 'endl'. Furthermore, left-shifting
a pointer by a value, or a value by a pointer, is not a defined
operation in C. Perhaps in copying out the program for the posting,
you accidently pasted in a line from another window in which you
had something dealing with some other programming language such as
Rogaine ?

> 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.

When i = 0, and you start the for (j = i - 1; j >= 0; j--) loop,
then j -will- be initialized to -1, but the loop condition j >= 0
will be evaluated before any trips are taken through the loop, and will
be found to be false, so the loop will not be executed. The j >= 0
protects the loop from executing when j is not at least 0.
--
"Man's life is but a jest,
A dream, a shadow, bubble, air, a vapor at the best."
-- George Walter Thornbury

Walter Roberson, May 16, 2008
4. ### Charlton WilburGuest

>>>>> "P" == Philipp Weissenbacher@gmail com
>>>>> <> writes:

P> Hi all! This is most certainly a total newbie question, but why
P> doesn't the following code cause a segfault?

Because segfaults are a bonus, not a requirement.

Charlton

--
Charlton Wilbur

Charlton Wilbur, May 16, 2008
5. ### Default UserGuest

wrote:

> 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.

Even if you had accessed outside the boundaries, that's undefined
behavior. There is no defined behavior for undefined behavior. That
includes segfaults or any other behavior.

Brian

Default User, May 16, 2008
6. ### Guest

On 16 Mai, 21:01, Charlton Wilbur <> wrote:
> >>>>> "P" == Philipp Weissenbacher@gmail com
> >>>>> <> writes:

>
> P> Hi all! This is most certainly a total newbie question, but why
> P> doesn't the following code cause a segfault?
>
> Because segfaults are a bonus, not a requirement.
>
> Charlton
>
> --
> Charlton Wilbur
>

At first thanks for all the answers. And yes I'm actually using C++; I
just missed some couts. Sorry for that one.
@Roberson: I always thought a for loop does at lest one iteration
before checking the condition. Gotta reread that part ...

, May 16, 2008
7. ### Walter RobersonGuest

In article <>,
<> wrote:

>@Roberson: I always thought a for loop does at lest one iteration
>before checking the condition. Gotta reread that part ...

No, the only iteration form that does at least one iteration before
checking the condition is "do until"

--
"I think Walter's legacy will be that of a man with a God-given
ability that got the most out of it at every possible chance."
-- Eddie Payton

Walter Roberson, May 16, 2008
8. ### Keith ThompsonGuest

"" <> writes:
> 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;

printf("%d <= %d\n", a[j], v); /* This is comp.lang.c. */

> 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.

Here's something you could have tried that probably would have avoided
the need to post the question:

By inserting a printf call at the beginning of the for loop:

printf("j = %d\n", j);

you could have seen that j never has the value -1 (which would leave
you to fix the logic problem that prevents the loop from being
executed at all).

Or you could do the equivalent in a debugger; the details of how to do
that are off-topic here, but should be in your debugger's documentation.

In any case, even if you did evaluate a[j] with j == -1, there's no
guarantee that it would cause a seg fault, or any other particular
result. The behavior is simply undefined (i.e., the C standard says
absolutely nothing about what might happen). It could cause a seg
fault, it could quietly access some piece of memory outside the array,
it could clobber something that causes your program to crash later.
It can't *really* make demons fly out of your nose (as the old joke
here goes), but if it did, it wouldn't be a violation of the C
standard.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 16, 2008
9. ### Keith ThompsonGuest

-cnrc.gc.ca (Walter Roberson) writes:
> In article <>,
> <> wrote:
>
>>@Roberson: I always thought a for loop does at lest one iteration
>>before checking the condition. Gotta reread that part ...

>
> No, the only iteration form that does at least one iteration before
> checking the condition is "do until"

Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
<OT>Pascal has "repeat until"; perhaps that's what you were thinking
of.</OT>

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 16, 2008
10. ### Walter RobersonGuest

In article <>,
Keith Thompson <> wrote:
>-cnrc.gc.ca (Walter Roberson) writes:
>> In article <>,

>> No, the only iteration form that does at least one iteration before
>> checking the condition is "do until"

>Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
><OT>Pascal has "repeat until"; perhaps that's what you were thinking
>of.</OT>

Nah, I just have a thick head today
--
'Roberson' is my family name; my given name is 'Walter'.

Walter Roberson, May 16, 2008
11. ### Guest

On 16 Mai, 21:52, -cnrc.gc.ca (Walter Roberson) wrote:
> In article <>,
> Keith Thompson <> wrote:
>
> >-cnrc.gc.ca (Walter Roberson) writes:
> >> In article <>,
> >> No, the only iteration form that does at least one iteration before
> >> checking the condition is "do until"

> >Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
> ><OT>Pascal has "repeat until"; perhaps that's what you were thinking
> >of.</OT>

>
> Nah, I just have a thick head today
> --
> 'Roberson' is my family name; my given name is 'Walter'.

@Walter: Thought this would be more polite. I'm a German native-
speaker and we have something called T-V distinction. The English
counterpart to "sie" appears to be calling someone with his family
name (at least for me) whereas calling someone by his given name
indicates a much closer relationship.

, May 16, 2008