-1
#include<stdio.h>  
#include<math.h>  
#include<stdlib.h>  
#include<string.h>  

void allocate_double_2darray(int ip_flag, double **ip_array, int num_row, int num_col); 

int main(void)  
{  

        int num_row, num_col;  
        double **temp_array;  

        num_row = 4;  
        num_col = 2;

        allocate_double_2darray(0, temp_array, num_row, num_col); //Allocate memory
        allocate_double_2darray(1, temp_array, num_row, num_col); //initialize
        allocate_double_2darray(2, temp_array, num_row, num_col); //Free Memory
        return 0;

}

void allocate_double_2darray(int ip_flag, double **ip_array, int num_row, int num_col)  
{  

      int i_loop, j_loop;
      if(ip_flag == 0)  //allocate (free) memeory from array
        { ip_array = malloc(sizeof(double *) * num_row);                   
          for(i_loop = 0; i_loop < num_row; i_loop++)
          ip_array[i_loop] = malloc(sizeof(double) * num_col);                     
        }

      if(ip_flag == 1)  //initialize to zero 
        {    
          for(i_loop = 0; i_loop < num_row; i_loop++)
              for(j_loop = 0; j_loop < num_col; j_loop++)        
                  ip_array[i][j] = 0.0;      
        }   

      if(ip_flag == 2)  //deallocate (free) memeory from array 
        {    
          for(i_loop = 0; i_loop < num_row; i_loop++)
             free(ip_array[i_loop]);
          free(ip_array);         
        }            

}

这是分配内存以创建二维数组的简单函数。它编译成功。但是,当我执行它时,它给了我分段错误错误。有没有人帮我找到我在这里做错了什么?

4

2 回答 2

0

您不能从函数内部更改指针的值。您可以使用指针并更改其内容,但不能更改指针本身并期望它在外部可用。

在您的情况下,调用ip_flag==0没有做任何有用的事情,并且当调用ip_flag==1它时会出现段错误。

可能的解决方案:

  • 尝试使用宏
  • 或者改ip_array = malloc(sizeof(double *) * num_row);到函数外面allocate_double2d_array
于 2013-03-27T20:35:48.310 回答
0

更改函数参数,使其获得指向矩阵的指针,这样您就可以通过传递指针本身的地址来更改指针指向的位置。

#include<stdio.h>  
#include<math.h>  
#include<stdlib.h>  
#include<string.h>  

void allocate_double_2darray(int ip_flag, double **ip_array, int num_row, int num_col); 

int main(void)  
{  

        int num_row, num_col;  
        double **temp_array;  

        num_row = 4;  
        num_col = 2;

        allocate_double_2darray(0, &temp_array, num_row, num_col); //Allocate memory
        allocate_double_2darray(1, &temp_array, num_row, num_col); //initialize
        allocate_double_2darray(2, &temp_array, num_row, num_col); //Free Memory
        return 0;

}

void allocate_double_2darray(int ip_flag, double ***ip_array, int num_row, int num_col)  
{  

      int i_loop, j_loop;
      if(ip_flag == 0)  //allocate (free) memeory from array
        { (*ip_array) = malloc(sizeof(double *) * num_row);                   
          for(i_loop = 0; i_loop < num_row; i_loop++)
          (*ip_array)[i_loop] = malloc(sizeof(double) * num_col);                     
        }

      if(ip_flag == 1)  //initialize to zero 
        {    
          for(i_loop = 0; i_loop < num_row; i_loop++)
              for(j_loop = 0; j_loop < num_col; j_loop++)        
                  (*ip_array)[i][j] = 0.0;      
        }   

      if(ip_flag == 2)  //deallocate (free) memeory from array 
        {    
          for(i_loop = 0; i_loop < num_row; i_loop++)
             free((*ip_array)[i_loop]);
          free((*ip_array));
          (*ip_array) = NULL;         
        }            

}
于 2013-03-27T22:22:24.277 回答