teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

質問をわかりやすく変更した

2021/09/26 10:09

投稿

alizona
alizona

スコア126

title CHANGED
@@ -1,1 +1,1 @@
1
- MallocでOver flow がおきます。C言語
1
+ パスカルの三角形 MallocでOver flow がおきます。C言語
body CHANGED
File without changes

5

引数の順ばんが間違えていました。申し訳ありません。

2021/09/26 10:09

投稿

alizona
alizona

スコア126

title CHANGED
File without changes
body CHANGED
@@ -48,6 +48,41 @@
48
48
 
49
49
  ```
50
50
 
51
+ //修正後のコード
52
+ ```C
53
+
54
+
55
+ int** generate(int numRows, int* returnSize , int** returnColumnSizes) {
56
+
57
+ int **q, i, j;
58
+
59
+ *returnSize = numRows;
60
+ q = (int **) malloc (sizeof(int *) * numRows);
61
+ *returnColumnSizes = (int *) malloc (sizeof(int) * numRows);
62
+
63
+
64
+ for (i = 0; i < numRows; i++) {
65
+ (*returnColumnSizes)[i] = i + 1;
66
+ q[i] = (int *) malloc (sizeof(int) * (i + 1));
67
+ q[i][0] = 1;
68
+ q[i][i] = 1;
69
+
70
+ if(i!=0){
71
+ for(j=1; j<i; j++){
72
+ q[i][j] = q[i-1][j-1] + q[i-1][j];
73
+ }
74
+ }
75
+ }
76
+
77
+ return q;
78
+ }
79
+
80
+
81
+
82
+
83
+
84
+ ```
85
+
51
86
  ```Error
52
87
  =================================================================
53
88
  ==32==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffece7f8470 at pc 0x557d430bf63e bp 0x7ffece7f83a0 sp 0x7ffece7f8390

4

質問をわかりやすくした

2021/09/26 10:07

投稿

alizona
alizona

スコア126

title CHANGED
@@ -1,1 +1,1 @@
1
- パスカルの三角形をCコーディングしています。LeetCode RuntimeError
1
+ MallocOver flow がおきます。C言語
body CHANGED
File without changes

3

エラーの出ている場所を説明しました。

2021/09/26 09:33

投稿

alizona
alizona

スコア126

title CHANGED
File without changes
body CHANGED
@@ -13,15 +13,16 @@
13
13
 
14
14
 
15
15
  int **q, i, j;
16
-
17
- *returnSize = numRows;
18
- *returnColumnSizes = (int *) malloc (sizeof(int) * numRows);
19
16
 
17
+
18
+ *returnSize = numRows;
20
19
  q = (int **) malloc (sizeof(int *) * numRows);
21
20
 
21
+
22
22
  for (i = 0; i < numRows; i++) {
23
23
 
24
- (*returnColumnSizes)[i] = i + 1;
24
+ printf("%d\n",i);
25
+
25
26
  q[i] = (int *) malloc (sizeof(int) * (i + 1));
26
27
  q[i][0] = 1;
27
28
  q[i][i] = 1;
@@ -32,9 +33,19 @@
32
33
  }
33
34
  }
34
35
  }
36
+
37
+ //この下の3行でエラーが出ています。
38
+ *returnColumnSizes = (int *) malloc (sizeof(int) * numRows);
39
+ for (i = 0; i < numRows; i++)
40
+ (*returnColumnSizes)[i] = i + 1;
41
+
35
42
  return q;
43
+
44
+
36
45
  }
37
46
 
47
+
48
+
38
49
  ```
39
50
 
40
51
  ```Error

2

みやすくしました。

2021/09/26 09:31

投稿

alizona
alizona

スコア126

title CHANGED
File without changes
body CHANGED
@@ -2,11 +2,12 @@
2
2
  アドバイスをいただけないでしょうか?
3
3
  [leetcodeの問題のリンクです](https://leetcode.com/problems/pascals-triangle/)
4
4
 
5
- /**
5
+
6
6
  * Return an array of arrays of size *returnSize.
7
7
  * The sizes of the arrays are returned as *returnColumnSizes array.
8
8
  * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
9
- */
9
+
10
+
10
11
  ```C
11
12
  int** generate(int numRows, int** returnColumnSizes, int* returnSize) {
12
13
 
@@ -26,7 +27,7 @@
26
27
  q[i][i] = 1;
27
28
 
28
29
  if(i!=0){
29
- for(int j=1; j<i; j++){
30
+ for(j=1; j<i; j++){
30
31
  q[i][j] = q[i-1][j-1] + q[i-1][j];
31
32
  }
32
33
  }

1

問題の要件を追記

2021/09/26 09:02

投稿

alizona
alizona

スコア126

title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,11 @@
2
2
  アドバイスをいただけないでしょうか?
3
3
  [leetcodeの問題のリンクです](https://leetcode.com/problems/pascals-triangle/)
4
4
 
5
+ /**
6
+ * Return an array of arrays of size *returnSize.
7
+ * The sizes of the arrays are returned as *returnColumnSizes array.
8
+ * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
9
+ */
5
10
  ```C
6
11
  int** generate(int numRows, int** returnColumnSizes, int* returnSize) {
7
12