fopen 后数组指针的值发生变化

Value of array pointer is getting changed after fopen

我正在对图像进行卷积运算。我在动态存储在数组指针中的数据中遇到问题。 fopen 函数后,数组指针变量之一的数据会自动更改。

我从名为 maxpool1 的函数获取数组指针,然后我将此数组传递给 conv2d2 卷积函数。

卷积函数:

float* Conv2d2(float (*ptr)[1][96][27][27],int filters,int kernel,int strides,int prevFilters, int prevOutput_shape){

    FILE *myFile;
    FILE *Weight_file;
    int i,j,k,l,m;
    int output_shape;
    int moveX = 0;
    int moveY = 0;

    output_shape = ceil(((prevOutput_shape-kernel)/strides+1) );//important is to select height

    // load Weights Values from CSV Files

    char buffer2[65536] ;             //specifying it without pointer(like in conv1) produce error
    char *record2,*line2;
    int a=0,b=0,c=0,p=0;
    Weight_file = fopen("SecondLayer.csv","r");
    if(Weight_file == NULL)
    {
       printf("\n file opening failed ");
       //return -1 ;
    }
    while((line2=fgets(buffer2,sizeof(buffer2),Weight_file))!=NULL)
    {
        record2 = strtok(line2,",");
        while(record2 != NULL)
      {
            Weightsecond[a][b][p][c++] = atof(record2) ;
            record2 = strtok(NULL,",");
      }
        p++;
        c=0;
        if(p==prevFilters){
            p=0;
            b++;
        }
        if(b==kernel){
            b=0;
            a++;
        }
    }



        //calculating feature map by moving kernel window on the image and writing feature map to text file
    myFile = fopen("featureMapSecond.txt","w");
    float featureVector[1][filters][output_shape][output_shape] ;

    for (m = 0;m < filters ;m++){       //loop for filters
        for (k = 0;k < output_shape; k++){  //loop for Y-featuremap
            for (l=0; l < output_shape; l++){   //loop for X-featuremap

                float featureMapValue = 0;
                for(p=0;p<prevFilters;p++){     //loop for filters in previous input
                    for (i = 0; i < kernel; i++){       //loop for Y-kernel
                        for (j = 0; j < kernel; j++){   //loop for X-kernel
                            featureMapValue = Weightsecond[i][j][p][m] * ptr[0][0][p][i+moveY][j+moveX] + featureMapValue;
                        }
                    }
                    if(p==72){
                        int temp=0;
                    }

                }
                fprintf(myFile,"%f\n", featureMapValue );
                featureVector[0][m][k][l] = featureMapValue;
                if (l != output_shape-1){
                    moveX = moveX + strides;}
                else {moveX = 0 ;}

            }

            if (k != output_shape-1){
                moveY = moveY + strides;}
                    else {moveY = 0 ;}

        }

        }
    fclose(myFile);
    float (*featureVectorSecond)[1][filters][output_shape][output_shape] = featureVector;
    return featureVectorSecond;
}

主要功能:

int main(){
    Image image_maiz;
    Image image_maiz_gray;
    Image_load(&image_maiz, "testMaize.jpg");
    ON_ERROR_EXIT(image_maiz.data == NULL, "Error in loading the image");
    Image_to_gray(&image_maiz,&image_maiz_gray);
    printf("width=%d ,height=%d, channel=%d",image_maiz_gray.width,image_maiz_gray.height,image_maiz_gray.channels);
    Image_save(&image_maiz_gray, "gray.jpg");

    float (*featureVector)[1][96][55][55] = Conv2dInput(&image_maiz_gray,96,11,4);
    featureVector=ActivationRelu1(featureVector,96,55);
    float (*featureVectorMaxPool1)[1][96][27][27]=MaxPooling1(featureVector,3,2,96,55);
    //float *featureVectorMaxPool1=MaxPooling1(featureVector,3,2,96,55);
    free(featureVector);
    float (*featureVector2)[1][256][23][23] = NULL;
    featureVector2=Conv2d2(featureVectorMaxPool1,256,5,1,96,27);
}

MaxPool1 函数:

float* MaxPooling1(float (*ptr)[1][96][55][55],int kernel,int strides,int prevFilters,int prevOutput_shape){

    int height=prevOutput_shape;
    int filters=prevFilters;
    float max=0;
    FILE *myFile;
    int moveX=0,moveY=0;
    int output_shape = ceil(((height-kernel)/strides+1) );
    float featureVector[1][filters][output_shape][output_shape];
    myFile = fopen("featureMapMaxpool1.txt","w");
    for(int m=0;m<filters;m++){
        for(int i =0;i<output_shape;i++){
            for(int j=0;j<output_shape;j++){
                for(int k=0;k<kernel;k++){
                    for(int l=0;l<kernel;l++){
                        if (*(*(*(*(*(ptr + 0)+0)+m)+k)+l)>max){
                            max=*(*(*(*(*(ptr + 0)+0)+m)+(k+moveY))+(l+moveX));
                        }
                    }

                }
                featureVector[0][m][i][j]=max;
                fprintf(myFile,"%f\n", max );
                max=0;
                if (j != output_shape-1){
                    moveX = moveX + strides;}
                else {moveX = 0 ;}


            }
            if (i != output_shape-1){
                moveY = moveY + strides;}
            else {moveY = 0 ;}
        }
    }

    fclose(myFile);
    float (*featureVectorMaxPool)[1][filters][output_shape][output_shape] = malloc(filters*output_shape*output_shape);
    featureVectorMaxPool = featureVector;
    return featureVectorMaxPool;
}

MaxPooling1 的最后两行是featureVectorMaxPool = featureVector; return featureVectorMaxPool;。在该函数的前面,featureVector 被定义为局部变量(分配在堆栈上)。当返回该函数并调用另一个函数时,MaxPooling1 用于 featureVector 的堆栈内存将用于另一个函数的局部变量。在这种情况下,其他函数是 Conv2d2,其局部变量 Weight_file 位于 MaxPooling1 用于 featureVector 的堆栈内存中。