如何解决通过内存指针进行迭代时,C代码错误:EXC_BAD_ACCESScode = EXC_I386_GPFLT
我正在为一个学校的C编程项目工作,它工作正常,但是现在突然之间,当它尝试访问numbers_array [j]下的内存块中的数字时,它将无法访问索引2。数组,并不断给我标题中的错误
我已经调试了大约一个小时,无法弄清楚出了什么问题。它将把编号为0的索引和编号为1的罚款作为因子,但是此后程序崩溃。知道有什么问题吗?
可以在注释Requirement 2下声明factor_struct后在循环中找到错误。
我的数字分解程序代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
/* Requirement 2:
Creating a struct with serveral pieces of data */
struct factor_struct {
int number,count;
clock_t start;
clock_t end;
double time_to_factor;
double times_array[];
};
/* Function header decleration */
void factor(struct factor_struct *fs);
/* The main program loop */
int main(int argc,const char * argv[]) {
/* Intializing the variables we'll be using during the run loop */
int input,number,continue_loop_one = TRUE,continue_loop_two = TRUE;
int *numbers_array;
/* Printing the welcome message to the screen */
printf("Welcome to Factor Factory!\n\n");
/* Asking how many numbers the user would like to factor*/
printf("How many different numbers would you like to factor?\n");
/* Start of LOOP_1: Ask user to input the amount of numbers to factor.
This will let us allocate the proper amount of memory */
LOOP_1: do {
printf("Please enter a number from 1 to 10: ");
scanf("%d",&input);
printf("\n");
/* Check if user input is between 1 and 10,if not return to start of LOOP_1 */
if(input < 1 || input > 10) {
printf("%d isn't a valid number...don't play games with me human. Try again! \n\n",input);
/* Need to flush the incorrect input from the buffer or else scanf will cause a continuous loop*/
fflush(stdin);
goto LOOP_1;
}
/* Break out of the loop and continue our program */
continue_loop_one = FALSE;
} while (continue_loop_one == TRUE);
/* Requirement 4:
Intialize our numbers array with the correct amount of memory */
numbers_array = (int*) calloc(input,sizeof(int));
/* Check if memory was allocated properly */
if (numbers_array == NULL) {
printf("Error: Memory not allocated. Closing program...\n");
exit(0);
}
/* Requirement 1:
Asking the user to enter the numbers that we will be using our algorithm for to get their factors */
LOOP_2: do {
printf("Excellent! Lets get started...");
printf("enter %d number(s) between 2 and 1,000,000.\n",input);
/* Variable to track how many numbers have been input just incase a user enters an invalid number*/
int entered_number_count = 0;
LOOP_3: for(int i = 0 + entered_number_count; i < input; i++) {
printf("Number %d: ",i + 1);
scanf("%d",&number);
if (number < 2 || number > 1000000000) {
printf("%d isn't a valid number...don't play games with me human. Try again! \n\n",number);
fflush(stdin);
goto LOOP_3;
}
/* Assinging a number to the value at the proper memory location in the numbers_array */
numbers_array[i] = number;
/* Incrementing the amount of entered numbers */
entered_number_count++;
}
/* Break out the loop and continue our program */
continue_loop_two = FALSE;
} while(continue_loop_two == TRUE);
printf("\n");
/* Requirement 2:
Declaring our factor_struct for use */
struct factor_struct fs;
fs.count = 0;
/* For each number in the array,assign it to fs.number and factor it by passing it to our factor function
Each pass over the array will reassign fs.number */
for(int j = 0; j < input; j++) {
fs.number = numbers_array[j]; // **ERROR APPEARS IF INDEX J > 1**
factor(&fs);
fs.count++;
}
/* Requirement 3:
Using a pointer to move through time_array which will calculate
the total time to factor all numbers */
double total_time = 0.0;
double *ptr = fs.times_array;
for(; *ptr; ++ptr) { total_time += *ptr; }
printf("Total computation time: %f seconds. \n\n",total_time);
/* Ask the user if they would like to continue or quit the program*/
printf("Would you like to continue factoring numbers?\n");
printf("Enter 'y' to continue or 'q' to quit: ");
/* Control loop that handles the continue or quit commands */
char c;
while(c != 'q') {
scanf("%c",&c);
if(c == 'y') {
printf("\n");
goto LOOP_1;
}
}
/* Thank the user and quit the program */
printf("\n");
printf("Thank you for using Factor Factory!\n");
printf("Closing program...\n\n");
/* Requirement 4:
Deallocating the memory for the numbers_array */
free(numbers_array);
return 0;
}
/* Requirement 5:
Taking data and passing it through the facotring algorithm */
void factor(struct factor_struct *fs) {
/* Marking the starting time of the function */
fs->start = clock();
printf("Factors of %d: ",fs->number);
/* Algorithm for finding all factors of our number */
for(int i = 1; i <= fs->number/2; i++) {
if (fs->number % i == 0) {
printf("%d ",i);
}
}
printf("%d\n",fs->number);
/* Marking the ending time of the function */
fs->end = clock();
/* Calculating the total time it took to find all factors*/
fs->time_to_factor = (double)(fs->end - fs->start) / CLOCKS_PER_SEC;
fs->times_array[fs->count] = fs->time_to_factor;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。