C中非常奇怪的错误,请帮忙 [英] Very strange bug in C, please help

查看:85
本文介绍了C中非常奇怪的错误,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我只是在C中编写一个非常简单的代码,而且这是一个非常奇怪的错误

,我无法弄明白为什么。


第一个循环用于v [i] [j],第二个循环用于k [i]。 v和k之间没有

关系,但如果我在每个命令之后调试并观察变量

变量。


当k [i]发生sencond循环时,v [i] [j] s的值改变

并设置为等于k [i]的某些值。具体来说,v [1] [1]是

设置为10,v [1] [2]也是如此。

我不明白为什么。似乎v和k都指向内存中相同的

地址。

这很奇怪。


(我在Visual Studio 6中编写此C代码并在Visual Studio中调试它

6)


谢谢

#include< math .h>

#include< stdlib.h>

#include" time.h"


void error( char * name);


int main(int argc,char ** argv)

{

double v [3] [3];

双k [3];

int i,j,l;



for (i = 0; i< 3; i ++)

for(j = 0; j< 3; j ++)

v [i] [j] = 4;


for(i = 0; i< 9; i ++)

{


k [i] = 10; < br $>
}


返回(0);

}


void error(char *姓名)

{

printf(" usage:%s image.tiff \ n \ n",name);


退出(1);

}

解决方案

Vi ******* @ gmail.com 说:


< snip>


double k [3];


将剪断>

为(I = 0; I< 9;我++)<无线电通信/>
{


k [i] = 10;

}



k只有三个元素,你写的是所有九个元素。

这不好。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


Vi ******* @ gmail.com 写道:


大家好,

我只是在C中编写一个非常简单的代码,而且这是一个非常奇怪的错误

我无法弄清楚为什么。


第一个循环用于v [i] [j],第二个用于k [i]。 v和k之间没有

关系,但如果我在每个命令之后调试并观察变量

变量。


当k [i]发生sencond循环时,v [i] [j] s的值改变

并设置为等于k [i]的某些值。具体来说,v [1] [1]是

设置为10,v [1] [2]也是如此。

我不明白为什么。似乎v和k都指向内存中相同的

地址。

这很奇怪。


(我在Visual Studio 6中编写此C代码并在Visual Studio中调试它

6)


谢谢


# include< math.h>

#include< stdlib.h>

#include" time.h"



如果你有自己的私人time.h,你可能不应该。

标准标题是< time.h>。当然,你不能在这里使用。


void error(char * name);


int main (int argc,char ** argv)

{

double v [3] [3];

double k [3];

int i,j,l;

for(i = 0; i< 3; i ++)

for(j = 0; j< 3; j ++)

v [i] [j] = 4;


for(i = 0; i< 9; i ++)



^^ ^^

一旦你访问k [3],你就永远不会登陆。没有

这样的对象如k [3]到k [8]并且你没有拥有它们可能是

的记忆。
< blockquote class =post_quotes>
{

k [i] = 10;

}

返回0;

}


无效错误(字符*姓名)

{

printf(" usage:%s image) .tiff \ n \ n",name);

exit(1);



^^^^^^

使用其中一个定义的参数值退出,即0,

EXIT_SUCCESS和EXIT_FAILURE。 1没有标准含义作为退出的

参数。


}


哇谢谢你们。


4月10日下午5:22,Martin Ambuhl< mamb ... @ earthlink.netwrote:


VijaKh ... @ gmail.com写道:


大家好,

我只是在C中编写一个非常简单的代码并且vthere是一个非常奇怪的错误

我无法弄清楚为什么。


第一个循环用于v [i] [j],第二个循环用于k [i]。 v和k之间没有

关系,但是如果我在每个命令之后调试并观察变量

变量。


当k [i]的sencond循环发生时,v [i] [j] s的值改变

和被设置为等于k [i]的某些值。具体来说,v [1] [1]是

设置为10,v [1] [2]也是如此。

我不明白为什么。似乎v和k都指向内存中相同的

地址。

这很奇怪。


(我在Visual Studio 6中编写此C代码并在Visual Studio中调试它

6)

由于

的#include<文件math.h>

#include< stdlib.h>

#include" time.h"



如果你有自己的私人time.h,你可能不应该。

标准标题是< time.h>。当然,你不能在这里使用。


void error(char * name);


int main(int argc,char ** argv)

{

double v [3] [3];

double k [3];

int i,j,l;

for(i = 0; i< 3; i ++)

for(j = 0; j< 3; j ++)

v [i] [j] = 4;

for(i = 0; i< 9; i ++)



^^^^

一旦访问k [3]你永远不会降落。没有

这样的对象如k [3]到k [8]并且你没有拥有它们可能是

的记忆。
< blockquote class =post_quotes>
{

k [i] = 10;

}

返回0;

}


void error(char * name)

{

printf (用法:%s image.tiff \ n \ n,姓名);

exit(1);



^^^^^^

使用其中一个定义的参数值退出,即0,

EXIT_SUCCESS和EXIT_FAILURE。 1没有标准含义作为退出的

参数。


} - 隐藏引用的文本 -



- 显示引用文字 - 隐藏引用文字 -


- 显示引用文字 -



Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don''t understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"

void error(char *name);

int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;


for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;

for (i=0;i<9;i++)
{

k[i]=10;
}

return(0);
}

void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);

exit(1);
}

解决方案

Vi*******@gmail.com said:

<snip>

double k[3];

<snip>

for (i=0;i<9;i++)
{

k[i]=10;
}

k has only three elements, and you are writing to all nine of them.
That''s not good.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Vi*******@gmail.com wrote:

Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don''t understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"

If you have your own private "time.h", you probably shouldn''t. The
standard header is <time.h>. You don''t use either here, of course.

void error(char *name);

int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;
for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;

for (i=0;i<9;i++)

^^^^
As soon as you access k[3] you are in never-never land. There are no
such objects as k[3] through k[8] and you don''t own the memory where
they might be.

{
k[i]=10;
}
return 0;
}

void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);
exit(1);

^^^^^^
Use one of the defined values for arguments to exit, namely, 0,
EXIT_SUCCESS, and EXIT_FAILURE. 1 has no standard meaning as an
argument for exit.

}


WOW thank you guys very much.

On Apr 10, 5:22 pm, Martin Ambuhl <mamb...@earthlink.netwrote:

VijaKh...@gmail.com wrote:

Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don''t understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks

#include <math.h>
#include <stdlib.h>
#include "time.h"


If you have your own private "time.h", you probably shouldn''t. The
standard header is <time.h>. You don''t use either here, of course.

void error(char *name);

int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;
for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;
for (i=0;i<9;i++)


^^^^
As soon as you access k[3] you are in never-never land. There are no
such objects as k[3] through k[8] and you don''t own the memory where
they might be.

{
k[i]=10;
}
return 0;
}

void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);
exit(1);


^^^^^^
Use one of the defined values for arguments to exit, namely, 0,
EXIT_SUCCESS, and EXIT_FAILURE. 1 has no standard meaning as an
argument for exit.

}- Hide quoted text -


- Show quoted text -- Hide quoted text -

- Show quoted text -



这篇关于C中非常奇怪的错误,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆