Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么我的程序经常给奇数整数一个分段错误,但对任何偶数整数都可以正常工作?_C_Malloc_Function Pointers - Fatal编程技术网

C 为什么我的程序经常给奇数整数一个分段错误,但对任何偶数整数都可以正常工作?

C 为什么我的程序经常给奇数整数一个分段错误,但对任何偶数整数都可以正常工作?,c,malloc,function-pointers,C,Malloc,Function Pointers,我的程序应该取一个整数作为数组长度值,然后创建该长度为1到1000000的随机数数组。然后,使用埃拉托什筛,它必须从随机数数组中找到每个数的最大素因子。然后它应该打印出随机数数组中具有最大素因子的所有元素 现在的问题是,对于任何大小的偶数整数,它似乎都可以正常工作,但对于奇数,它每隔一段时间就会出现一次分割错误。我假设*findMaxPrimeFactor函数中存在错误,但不知道具体是什么 我是用Linux编译的 主要功能 #include <stdio.h> #include &l

我的程序应该取一个整数作为数组长度值,然后创建该长度为1到1000000的随机数数组。然后,使用埃拉托什筛,它必须从随机数数组中找到每个数的最大素因子。然后它应该打印出随机数数组中具有最大素因子的所有元素

现在的问题是,对于任何大小的偶数整数,它似乎都可以正常工作,但对于奇数,它每隔一段时间就会出现一次分割错误。我假设*findMaxPrimeFactor函数中存在错误,但不知道具体是什么

我是用Linux编译的

主要功能

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"ArrayOfRandom.h"
#include"MaxPrimeFactor.h"
int main(int argc, char* argv[]) {
int arrayLength = 0;
if (argc < 3) { // checking if there are enough arguments in the input
        for (int j = 0; j < argv[1][j]; j++) { // checking every character
            if (argv[1][j] >= 48 && argv[1][j] <= 57) {   // ASCII codes from 48 to 57 represent characters from '0' to '9' respectively 
                arrayLength = atoi(argv[1]); // atoi function converts the command line arguments into integers. mod 10 is used to finally get the arrayLength value 
            } else {
                printf("The input should be of an integer value.\n"); // once the invalid character is found, the error message is printed
                exit(0); // exiting the program
                }

        }
} else {
    printf("Please input just 1 value\n"); // if the user tries to put values with spaces, print error message
    exit(0);  // exit the program
}
int *array = arrayOfRandom(arrayLength);// calling of the function that creates the array of random integers

for (int i = 0; i < arrayLength; ++i)
{
    printf("Largest prime factor of %d is %d", *(array + i), findMaxPrimeFactor(*(array + i)));
    printf("\n");
}

return 0;

请注意您正在泄漏内存(返回前应调用free()。另外,粘贴的代码编译不好:返回(void*)数组;应该只是返回数组。。。您能确保您发布的示例编译良好吗?在
}返回(void*)数组;自由(数组);}免费的(
是死代码;
return
表示从不执行
free()
}return(void*)maxFactor同上;自由(arrayOfBoolean);}
intj=0;j
可疑。我希望
intj=0;argv[1][j];j++
。许多其他问题。在众多问题中,这可能是导致崩溃的原因:
i
int*findMaxPrimeFactor
-->为什么代码返回指针而不是
int
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include"ArrayOfRandom.h"
int *arrayOfRandom(int arrayLength) {
srand(time(NULL)); // seed generator for pseudo-random numbers
int *array = NULL;
array = (int*)malloc(arrayLength * sizeof(*array)); 
if (array == NULL) {
    printf("ERROR: memory allocation did not succeed\n");
} else {
    int i; // loop counter
    int range = 1000000;
    for (i = 0; i <= arrayLength; i++) {
        *(array + i) = rand() % range + 1; // range of random integers from 1 to 1000000
    }
}
return (void *)array;
free(array); // Freeing the reserved memory for the array
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include"MaxPrimeFactor.h"
int *findMaxPrimeFactor(int number) { 
int *arrayOfBoolean = NULL;
int maxFactor = 0;
arrayOfBoolean = malloc(number * sizeof(*arrayOfBoolean)); /* Length of the boolean array is supposed to be the same as the value of each number from the array of random numbers(every time it is called)*/
if (arrayOfBoolean == NULL) {
    printf("ERROR: memory allocation did not succeed\n");
}
else {
    int i = 0, j = 0; // loop counters
    for (i = 2; i < number; i++) { // initializing the array elements to true
        *(arrayOfBoolean + i) = 1;
    }

    for (i = 2; i*i < number; i++) { // Implementation of the Sieve
        if (*(arrayOfBoolean + i) == 1) {
            for (j = i * i; j < number; j += i) {
                *(arrayOfBoolean + j) = 0;
            }
        }
    }

    /* Finding the largest prime factor   */
    for (i = 2; i <= number; i++) { 
        if ((*(arrayOfBoolean + i) == 1) && (number%i == 0) && (maxFactor < i)) {
            maxFactor = i;
        }
        else if(maxFactor == 0){
            maxFactor = number;
        }
        }
}
return (void *)maxFactor;
free(arrayOfBoolean);
int *arrayOfRandom(int arrayLength);
int *findMaxPrimeFactor(int number);