Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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_Arrays_Segmentation Fault_Free_Dynamic Arrays - Fatal编程技术网

C 函数末尾的分段错误

C 函数末尾的分段错误,c,arrays,segmentation-fault,free,dynamic-arrays,C,Arrays,Segmentation Fault,Free,Dynamic Arrays,当我的函数完成最后一个for循环时,它返回一个segfault。我不知道为什么。这是我的密码 void initArray(int a[], int n){ int i, j; a = (int *) malloc(n * sizeof(int)); for(i = 0; i < n; i++){ a[i] = i+1; } for(j = n-1; j >= 0; j--){ int num1 = rand()

当我的函数完成最后一个for循环时,它返回一个segfault。我不知道为什么。这是我的密码

void initArray(int a[], int n){
    int i, j;
    a = (int *) malloc(n * sizeof(int));
    for(i = 0; i < n; i++){
        a[i] = i+1;
    }
    for(j = n-1; j >= 0; j--){
        int num1 = rand() % (j+1); // generates a random number from 0 to n
        int container2 = a[j];
        a[j] = a[num1];
    a[num1] = container2;
    }
    // Array checking purposes
    int k;
    for(k = 0; k < n; k++){
        printf("%i ", a[k]);
    }
    printf("\n");
    free(a);
}
void initArray(int a[],int n){
int i,j;
a=(int*)malloc(n*sizeof(int));
对于(i=0;i=0;j--){
int num1=rand()%(j+1);//生成一个从0到n的随机数
int container2=a[j];
a[j]=a[num1];
a[num1]=容器2;
}
//数组检查目的
int k;
对于(k=0;k
它应该创建一个动态数组,然后随机化数组中的数字。我做错了什么?
谢谢。

分段错误的原因似乎是在调用方中的函数调用之后使用传递给函数的参数

函数没有意义,因为它的第一个参数的值没有被使用,并且在函数的开头被覆盖

void initArray(int a[], int n){
    int i, j;
    a = (int *) malloc(n * sizeof(int));
    //...
考虑传递给函数的原始参数不会更改,因为函数处理函数参数值的副本

并使用
free

free(a);
与函数名冲突

似乎您需要像这样声明函数

void initArray(int **a, int n){
    //...
    *a = (int *) malloc(n * sizeof(int));
    for( int i = 0; i < n; i++){
        ( *a )[i] = i+1;
    }
    //...
而不是

int num1 = rand() % (j+1);
它返回一个segfault

中的数组
int a[]

void initArray(int a[], int n){
不能在函数
initArray
之外使用。创建的内存被
空闲(a)
破坏

将其更改为:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int * initArray(int n){
    int i, j;

    int *a =  malloc(n * sizeof(int));
    for(i = 0; i < n; i++){
        a[i] = i+1;
    }
    for(j = n-1; j >= 0; j--){

        int num1 = rand() % (j+1); // generates a random number from 0 to n
        int container2 = a[j];

        a[j] = a[num1];

        a[num1] = container2;
    }

    printf("\n");
    return a;  
} 

int main(void)
{
    int *a;
    int k;
    int n = 5;

    a = initArray(n);

    // Array checking purposes
    for(k = 0; k < n; k++){
        printf("%i ", a[k]);
    }

    free(a);
    return 0;   
}

. 您没有试图访问
a
外部
initArray
,是吗?@Baka Baka函数没有意义,因为函数void initArray(int a[],int n){/…a=(int*)malloc(n*sizeof(int))中没有使用其参数a的值;当使用传递的参数时,似乎在函数调用后的调用方中发生了分段错误。因此,您应该显示函数及其参数在被调用方中的使用方式。在函数旁边,我初始化了
int n,*a
,并将函数调用为
initArray(a,n)
这很有帮助。我修复了它,并将分配从函数中取出,放到main中。谢谢。因为我们不知道调用代码是什么样子,所以我们可以"I don’我不说会发生什么。显示的函数对参数a没有任何作用。@ArndtJonasson我建议首先阅读问题作者的评论,而不是写你愚蠢的评论,然后投票否决正确的答案。如果我记得的话,我会在可能的时候再次投票,因为OP发现你的代码很有用。我们永远不知道实际的答案是什么错误是,这没关系。@ArndtJonasson错误的原因已经足够清楚了。OP按值传递指针,认为原始指针也会更改。他错误地使用了free。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int * initArray(int n){
    int i, j;

    int *a =  malloc(n * sizeof(int));
    for(i = 0; i < n; i++){
        a[i] = i+1;
    }
    for(j = n-1; j >= 0; j--){

        int num1 = rand() % (j+1); // generates a random number from 0 to n
        int container2 = a[j];

        a[j] = a[num1];

        a[num1] = container2;
    }

    printf("\n");
    return a;  
} 

int main(void)
{
    int *a;
    int k;
    int n = 5;

    a = initArray(n);

    // Array checking purposes
    for(k = 0; k < n; k++){
        printf("%i ", a[k]);
    }

    free(a);
    return 0;   
}
5 2 1 3 4