新手问题:为什么这样做? [英] Newbie question: Why does this work?
问题描述
大家好!
这肯定是一个全新的问题,但为什么
以下代码会导致段错误?
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<<结束;
休息;
}
a [j + 1] = a [j];
}
a [j + 1] = v;
}
}
恕我直言,应该发生段错误在第9行,如第一次通过
第一个for-loop i = o ergo j为-1,导致[j]引发
段错误。 />
提前谢谢,请原谅我提出这样一个愚蠢的问题
博士******************* @ gmail.com 说:
大家好!
这肯定是一个全新的问题,但为什么不是
以下代码导致段错误?
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){
这并没有因为,第一次通过,i = 0,所以j变为i - 1 =
-1,这不是> = 0,所以循环体被完全跳过。
cout<< a [j]<< " < =" << v<< ENDL;
仅供记录,这是一个C新闻组,而不是C ++新闻组。
< snip>
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:/ /万维网。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Ph ******************* @ gmail.com 写道:
嗨全部!
这肯定是一个全新的问题,但是为什么不跟随代码的
导致段错误?
void insertion_sort(int a [],int length)
{
int i;
for(i = 0; i<长度; i ++)
{
int j,v = a [i];
for(j = i - 1; j>如果(a [j] <= v){
cout<< a [j]<< " < =" << v<<结束;
休息;
}
a [j + 1] = a [j];
}
a [j + 1] = v;
}
}
恕我直言,应该发生段错误在第9行,如第一次通过
第一个for-loop i = o ergo j为-1,导致[j]引发
段错误。 />
在此先感谢,请原谅我提出这样一个愚蠢的问题
第二个for循环只会在它''时输入s条件为真,
ie,仅当j为> = 0时才会发生这种情况。这不会发生在外循环的第一次
迭代之后。在外部
循环的第二次迭代期间,j将初始化为0,因此将执行内部for循环
下的代码。
>
文章< 64 ********************************* * @ a23g2000hsc。 googlegroups.com>,
Ph ******* ************ @ gmail.com < Ph ******************* @ gmail.comwrote:
>这肯定是一个全新的问题,但为什么以下代码不会导致段错误?
> 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;
上面的行不会编译,因为你没有定义名为''cout''的变量
,也没有定义名为''的变量ENDL。此外,左移'/ b $ ba指针的值,或指针的值,不是在C中定义的
操作。也许在复制程序中用于发布,
你意外地从另一个窗口粘贴在一行中,你b $ b有一些东西与其他编程语言有关,比如
Rogaine?
>休息;
}
a [j + 1] = a [j];
}
a [j + 1] = v;
}
}
>恕我直言,segfault应该在第9行发生,就像第一次通过第一个for-loop一样i = o ergo j为-1,导致[j]引发一个
段错误。
当i = 0时,你启动for(j = i - 1; j> = 0; j--)循环,
然后j -will-被初始化为-1,但循环条件j> = 0
将在通过循环进行任何行程之前进行评估,并将
被发现为false,因此循环不会被执行。当j不是至少为0时,j> = 0
保护循环不被执行。
-
Man生活只是一种玩笑,
梦想,阴影,泡沫,空气,最好的蒸汽。
- George Walter Thornbury
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解决方案Ph*******************@gmail.com said:
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) {This isn''t broken because, first time through, i = 0, so j becomes i - 1 =
-1, which is not >= 0, so the loop body is skipped completely.
cout << a[j] << " <= " << v << endl;Just for the record, this is a C newsgroup, not a C++ newsgroup.
<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Ph*******************@gmail.com 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 questionThe 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.
In article <64**********************************@a23g2000hsc. googlegroups.com>,
Ph*******************@gmail.com <Ph*******************@gmail.comwrote:
>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
这篇关于新手问题:为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!