Newbie question: Why does this work?

  • Thread starter Philipp.Weissenbacher
  • Start date
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
 
S

santosh

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

Walter Roberson

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

Charlton Wilbur

"P" == Philipp Weissenbacher@gmail com
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
 
D

Default User

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
 
P

Philipp.Weissenbacher

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

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

Walter Roberson

@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"
 
K

Keith Thompson

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

Keith Thompson

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

Philipp.Weissenbacher

Nah, I just have a thick head today :(

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top