首页 新闻 会员 周边 捐助

C语言堆内存破坏

0
悬赏园豆:10 [已解决问题] 解决于 2013-09-26 16:51
 1 void calc_probability(int num)
 2 {
 3     int i = 0, j = 0 , k = 0;   //数组指针
 4     #define SIZE_NUM 8
 5     int *array_num = NULL; 
 6     int *remember_num = NULL;
 7     char **array_detail[SIZE_NUM] = {NULL};
 8     int len_array = 0;
 9     int count_num = 0;
10     int count = 0;
11 
12     len_array = 2 * num - 1;
13     array_num = (int*)calloc(len_array, sizeof(int));
14     array_detail[SIZE_NUM] = (char **)calloc(len_array, sizeof(char*));
15     remember_num = (int*)calloc(len_array, sizeof(int));
16 
17     for(i = 0; i < len_array; i++)
18     {
19         array_num[i] = i + 2;
20         array_detail[SIZE_NUM][i] = (char*)calloc(count_num + 1, sizeof(char));
21     }
22     
23     for(i = 0; i < len_array; i++)
24     {   
25         count = 0; // 计数清零
26         for(j = 1; j <= num; j++)
27         {
28            for(k = 1; k <= num; k++)
29            {
30                if(j + k == array_num[i])
31                {
32                   sprintf(&array_detail[SIZE_NUM][i][count_num], "(%d, %d)", j, k);
33                   printf("%s", &array_detail[SIZE_NUM][i][count_num]);
34                   count_num++;
35                   count++;
36                   array_detail[SIZE_NUM][i] = (char*)realloc(array_detail[SIZE_NUM][i], (count_num + 1) * sizeof(char));
37                }
38            }
39         }
40         remember_num[i] = count;
41     }
42 
43     for(i = 0; i < len_array; i ++)
44     {   
45         printf("数字 %d 的个数是: %d\n", i + 2, remember_num[i]);
46         for(j = 0; j < count_num; j++)
47         {
48             printf("%s ", &array_detail[SIZE_NUM][i][j]);
49         }
50         printf("\n");
51     }
52  
53 
54     //释放内存
55     free(remember_num);
56     remember_num = NULL;
57 
58     free(array_num);
59     array_num = NULL;
60 
61     for(i = 0; i < len_array; i++)
62     {
63         free(array_detail[SIZE_NUM][i]);
64         array_detail[SIZE_NUM][i] = NULL;
65     }
66     free(array_detail[SIZE_NUM]);
67     array_detail[SIZE_NUM] = NULL;
68 
69     return;
70 
71 }

主函数调用时在36行,堆内存报错,这个函数肯定有问题,但不知哪里错误了?
请大牛指点.

kongmeng的主页 kongmeng | 初学一级 | 园豆:193
提问于:2013-09-26 11:36
< >
分享
最佳答案
0

不知道C语言数组是从0开始的吗?

int  arr[8]; //八个元素的数组

arr[7]  = 1; //访问最后一个元素

arr[8] = 1; //这个就越界了

 

通篇的array_detail[SIZE_NUM],不知道你想干啥,全部越界

收获园豆:10
嗷嗷 | 小虾三级 |园豆:757 | 2013-09-26 15:23
 1 void calc_probability(int num)
 2 {
 3     int i = 0, j = 0 , k = 0;   
 4     int *array_num = NULL; 
 5     int *remember_num = NULL;
 6     char (**array_detail)[SIZE_NUM] = NULL; //数组指针
 7     int len_array = 0;
 8     int count_num = 0;
 9     int count = 0;
10 
11     len_array = 2 * num - 1;
12     array_num = (int*)calloc(len_array, sizeof(int));
13     array_detail = (char (**)[SIZE_NUM])calloc(len_array, sizeof(char(*))); //这里注意是sizeof(char(*)不是sizeof(char(*)[SIZE_NUM])
14     remember_num = (int*)calloc(len_array, sizeof(int));
15 
16     for(i = 0; i < len_array; i++)
17     {
18         array_num[i] = i + 2;
19         array_detail[i] = (char(*)[SIZE_NUM])calloc(count_num + 1, sizeof(char[SIZE_NUM]));
20     }
21 
22     for(i = 0; i < len_array; i++)
23     {   
24         count = 0; // 计数清零
25         count_num = 0;
26         for(j = 1; j <= num; j++)
27         {
28             for(k = 1; k <= num; k++)
29             {
30                 if(j + k == array_num[i])
31                 {
32                     sprintf(array_detail[i] + count_num, "(%d, %d)", j, k);
33                     printf("%s", array_detail[i] + count_num);
34                     count_num++;
35                     count++;
36                     array_detail[i]  = (char(*)[SIZE_NUM])realloc(array_detail[i], (count_num + 1) * sizeof(char[SIZE_NUM]));
37                     memset(array_detail[i] + count_num, 0x0, SIZE_NUM);
38                 }
39             }
40         }
41         remember_num[i] = count;
42     }
43 
44     for(i = 0; i < len_array; i ++)
45     {   
46         printf("数字 %d 的个数是: %d\n", i + 2, remember_num[i]);
47         for(j = 0; j < remember_num[i]; j++)
48         {
49             printf("%s ", array_detail[i] + j);
50         }
51         printf("\n");
52     }
53 
54 
55     //释放内存
56     free(remember_num);
57     remember_num = NULL;
58 
59     free(array_num);
60     array_num = NULL;
61 
62     for(i = 0; i < len_array; i++)
63     {
64         free(array_detail[i]);
65         array_detail[i] = NULL;
66     }
67     free(array_detail);
68     array_detail = NULL;
69 
70     return;
71 
72 }

这样改就没有问题了。谢谢大家的帮助。

kongmeng | 园豆:193 (初学一级) | 2013-09-27 10:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册