Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie question: Why does this work?

Reply
Thread Tools

Newbie question: Why does this work?

 
 
Philipp.Weissenbacher@gmail.com
Guest
Posts: n/a
 
      05-16-2008
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[i];
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
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      05-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i];
> 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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-16-2008
In article <(E-Mail Removed)>,
(E-Mail Removed) <(E-Mail Removed)> 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[i];
> 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
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      05-16-2008
>>>>> "P" == Philipp Weissenbacher@gmail com
>>>>> <(E-Mail Removed)> 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
(E-Mail Removed)
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-16-2008
(E-Mail Removed) 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
 
Reply With Quote
 
Philipp.Weissenbacher@gmail.com
Guest
Posts: n/a
 
      05-16-2008
On 16 Mai, 21:01, Charlton Wilbur <(E-Mail Removed)> wrote:
> >>>>> "P" == Philipp Weissenbacher@gmail com
> >>>>> <(E-Mail Removed)> 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
> (E-Mail Removed)


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 ...
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-16-2008
In article <(E-Mail Removed)>,
(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2008
"(E-Mail Removed)" <(E-Mail Removed)> 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[i];
> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed)>,
> (E-Mail Removed) <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-16-2008
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
>> In article <(E-Mail Removed)>,


>> 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'.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments