gpt4 book ai didi

c - Xcode 错误 : "EXC_BAD_ACCESS"

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:57 26 4
gpt4 key购买 nike

我正在尝试在 Xcode 中编译和运行 C 程序。该程序需要一个用于读取输入的文本文件和另一个用于写入数据的文本文件。我已将程序和这两个文本文件放在源文件夹中。程序构建成功,但是当我尝试运行程序时出现错误:GDB:程序收到信号:“EXC_BAD_ACCESS”

这可能是什么原因造成的?

 int main() {

FILE *fp;
FILE *fr;

//Declare and intialize all variables to be used
float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0;
float time = 0, deltaTime = .001;
float timeImpact = 0, vyImpact = 0, vxImpact = 0, xImpact = 0, yImpact = 0;

int numBounces = 0;

//Coefficient of Restitution; epsilon = ex = ey
float ex = .5;
float ey = .5;

fr = fopen("input_data.txt", "rt"); //Open file for reading

fp = fopen( "output_data.txt", "w" ); // Open file for writing

if(fr == NULL){ printf("File not found");} //if text file is not in directory...

if(fp == NULL){ printf("File not found");} //if text file is not in directory...

fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy);

while (time < 5) {

//time = time + deltaTime
time = time + deltaTime;
//velocity[new] = velocity[old] + acc * deltaTime
vx = vx + ax*deltaTime;
vy = vy + ay*deltaTime;
//position[new] = position[old] + velocity*deltaTime + .5*acc*(deltaTime)^2
x = x + vx*deltaTime + (.5*ax*deltaTime*deltaTime);
y = y + vy*deltaTime + (.5*ay*deltaTime*deltaTime);

//fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);

//Collision occurs; implement collision response
if (y < 0) {

//Find time of collision by solving for t in equation vy*t + .5*ay*t^2 + y = 0
timeImpact = (-vy + sqrt((vy*vy) - (2*ay*y)) / (2*.5*ay)); //Collision time = 3.7?

//velocity = -epsilon*velocity[Impact] + acc*time
vy = (-1*ey)*vyImpact + ay*((deltaTime - time) - timeImpact);
vx = (-1*ex)*vxImpact + ay*((deltaTime - time) - timeImpact);

//Position = position[Impact] - epsilon*velocity[Impact]*time + 1/2*acc*time^2
x = xImpact - ex*vxImpact*((deltaTime - time) - timeImpact) + .5*ax* ((deltaTime - time) - timeImpact) * ((deltaTime - time) - timeImpact);
y = yImpact - ey*vyImpact*((deltaTime - time) - timeImpact) + .5*ay*((deltaTime - time) - timeImpact) * ((deltaTime - time) - timeImpact);

//velocity = v[o] + ay(time)
vyImpact = vy + ay*(timeImpact - time);
vxImpact = vx + ax*(timeImpact - time);

//position = position[o] + velocity(time) + 1/2*acc*time^2
xImpact = x + vx*(timeImpact - time) + .5*ax*(timeImpact - time)*(timeImpact - time);
//yImpact = y + vy*(timeImpact - time) + .5*ay*(timeImpact - time)*(timeImpact - time);

numBounces++; //Increment number of bounces ball takes

//fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);
printf("timeImpact: %f\nxImpact: %f\nyImpact: %f\nvxImpact: %f\nvyImpact: %f\n", timeImpact, xImpact, yImpact, vxImpact, vyImpact);
printf("Number of Bounce(s): %d\n\n", numBounces);
}
}

fclose(fp); //Close output file
fclose(fr); //Close input file

system ("PAUSE");
return 0;
}

示例输入:

ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5

最佳答案

该程序几乎可以正常运行。我得到了这个输出:

timeImpact: 28.457277
xImpact: 7798.595703
yImpact: 0.000000
vxImpact: 315.561859
vyImpact: 73.370865
Number of Bounce(s): 1

timeImpact: -315.561096
xImpact: 984718.250000
yImpact: 0.000000
vxImpact: -3213.589600
vyImpact: 36.704834
Number of Bounce(s): 2

timeImpact: 3088.760254
xImpact: 94468824.000000
yImpact: 0.000000
vxImpact: 31913.345703
vyImpact: 55.056641
Number of Bounce(s): 3

timeImpact: -30290.189453
xImpact: 9086046208.000000
yImpact: 0.000000
vxImpact: -312763.843750
vyImpact: 45.875000
Number of Bounce(s): 4

timeImpact: 296834.687500
xImpact: 872571076608.000000
yImpact: 0.000000
vxImpact: 3065398.750000
vyImpact: 50.500000
Number of Bounce(s): 5

timeImpact: -2908993.750000
xImpact: 83802579795968.000000
yImpact: 0.000000
vxImpact: -30040802.000000
vyImpact: 48.000000
Number of Bounce(s): 6

timeImpact: 28507292.000000
xImpact: 8047926899113984.000000
yImpact: 0.000000
vxImpact: 294391936.000000
vyImpact: 64.000000
Number of Bounce(s): 7

timeImpact: -279371488.000000
xImpact: 772922520746590208.000000
yImpact: 0.000000
vxImpact: -2885036544.000000
vyImpact: 0.000000
Number of Bounce(s): 8

timeImpact: 2737840640.000000
xImpact: 74231486855715487744.000000
yImpact: 0.000000
vxImpact: 28273358848.000000
vyImpact: 0.000000
Number of Bounce(s): 9

timeImpact: -26831695872.000000
xImpact: 7129642377640744583168.000000
yImpact: 0.000000
vxImpact: -277087289344.000000
vyImpact: 0.000000
Number of Bounce(s): 10

timeImpact: nan
xImpact: nan
yImpact: 0.000000
vxImpact: nan
vyImpact: nan
Number of Bounce(s): 11

sh: PAUSE: command not found

最后一行可能是唯一的问题。 system("PAUSE") 应该做什么?

EXC_BAD_ACCESS 出现在以下行中:

fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy);

在其中设置断点显示fr 为NULL,这就是问题所在。文件的路径是相对于可执行文件位置设置的,但请注意,可执行文件转到构建文件夹,该文件夹不包含输入和输出文件(这些文件位于您的项目文件夹中)。

在 Xcode 中,选择目标,然后右键单击您的程序。选择“在 Finder 中显示”项,这将打开程序所在的文件夹。将您的输入文件放在那里,它应该可以工作。


澄清论点:

在您的主函数中,更改参数以匹配此:

int main (int argc, char *argv[])

简而言之,有两个参数。每个可执行文件都可以在启动时接收参数,这些参数捕获这些参数。例如,考虑何时从命令行启动程序:

$ ./myprogram file1.txt file2.txt

file1.txtfile2.txt 是参数,它们将传递给main。 main 函数有两个参数:argcargv。第一个是 int,保存参数的数量,第二个是字符串数组,参数本身。

在我给出的示例中,argc 设置为 3,因为第一个参数将是程序名称(这是一个小细节)。所以:

argv[1] == "file1.txt"
argv[2] == "file2.txt"

要在 Xcode 中执行此操作,首先修改您的主函数以接受参数。然后,修改这一行:

fr = fopen("input_data.txt", "rt"); //Open file for reading

对此:

fr = fopen(argv[1], "rt"); //Open file for reading

现在,考虑到您使用的是 Xcode4,请选择“Product > Edit Scheme...”菜单项。在运行配置(侧栏)中,选择参数选项卡并在“启动时传递的参数”部分中添加所需输入文件的路径。

就是这样。您的程序现在将像以前一样运行,但是当您需要使用另一个文件时,您不需要重新编译程序。只需更改参数列表中的路径即可。这看起来工作量更大,但是一旦您拥有更大的程序并开始使用命令行,它就会变得更有意义。

现在去读一本关于 C 的好书和另一本关于 Unix 的书,它们解释了我没有解释的细节:-)

关于c - Xcode 错误 : "EXC_BAD_ACCESS",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com