我正在做一個程式,該程式使用兩個堆疊對學校專案進行排序,我想通過蠻力方法處理堆疊大小低于或等于 5 的情況,為此我有不同的包含查找表的結構,其中包含表示數字順序的 int 陣列陣列(對于每種可能的順序),以及包含對它們進行排序的相應操作序列的函式指標陣列陣列,并且這些結構中的每一個都對應于特定的陣列大小(2、3、4、5)。并且為了避免一個巨大的if森林,我想使用帶有公共結構的 while 回圈訪問它們(t_combi) 具有指向不同資料的指標,但顯然它不起作用,我以某種方式丟失了全域陣列的地址,因為我在第一次比較訂單時遇到了段錯誤(嘗試使用 valgrind 訪問未分配的資料) .
代碼:
// brute_force.h
#ifndef BRUTE_FORCE_H
# define BRUTE_FORCE_H
# include "resolver.h"
# define CNT_2 2
# define CNT_3 6
# define CNT_4 24
# define CNT_5 120
typedef struct s_mlx *t_mlxptr;
typedef const char *(*t_op)(t_stack *, t_stack *);
typedef struct s_combi {
int cnt;
int **order;
t_op **op;
} t_combi;
static const struct {
int cnt;
int order[CNT_2][2];
t_op op[CNT_2][2];
} g_2combi = {
.cnt = CNT_2,
.order = {
{0, 1},
{1, 0}
},
.op = {
{NULL},
{sa, NULL}
}
};
static const struct {
int cnt;
int order[CNT_3][3];
t_op op[CNT_3][3];
} g_3combi = {
.cnt = CNT_3,
.order = {
{0, 1, 2},
{0, 2, 1},
{1, 0, 2},
...
},
.op = {
{NULL},
{rra, sa, NULL},
{sa, NULL},
...
}
};
static const struct {
int cnt;
int order[CNT_4][4];
t_op op[CNT_4][7];
} g_4combi = {
.cnt = CNT_4,
.order = {
{0, 1, 2, 3},
{0, 1, 3, 2},
{0, 2, 1, 3},
...
},
.op = {
{NULL},
{pb, rra, sa, pa, NULL},
{ra, sa, rra, NULL},
...
}
};
static const struct {
int cnt;
int order[120][5];
t_op op[120][10];
} g_5combi = {
.cnt = CNT_5,
.order = {
{0, 1, 2, 3, 4},
{0, 1, 2, 4, 3},
{0, 1, 3, 2, 4},
...
},
.op = {
{NULL},
{rra, rra, sa, ra, ra, NULL},
{pb, pb, sa, pa, pa, NULL},
{pb, pb, rra, pa, pa, NULL},
...
}
};
int brute_force(t_mlxptr mlx, t_stack *st[2], t_queue *instr);
#endif
// brute_force.c
#include <brute_force.h>
#include <resolver.h>
bool same_order(const int *a, const int *b, size_t size)
{
size_t i = 0;
while ( i < size)
{
if ((a[i - 1] < a[i] && b[i - 1] > b[i])
|| (a[i - 1] > a[i] && b[i - 1] < b[i]))
return (false);
}
return (true);
}
void apply_instructions(t_mlxptr mlx, t_stack *st[2], t_op *op, t_queue *instr)
{
while (*op)
{
add_op(mlx, *op, st, instr); // function from resolver.h
op;
}
}
void init_combi(t_combi combi[4])
{
combi[0] = (t_combi) {
.cnt = g_2combi.cnt,
.order = (int**)g_2combi.order,
.op = (t_op **)g_2combi.op
};
combi[1] = (t_combi) {
.cnt = g_3combi.cnt,
.order = (int**)g_3combi.order,
.op = (t_op **)g_3combi.op
};
combi[2] = (t_combi) {
.cnt = g_4combi.cnt,
.order = (int**)g_4combi.order,
.op = (t_op **)g_4combi.op
};
combi[3] = (t_combi) {
.cnt = g_5combi.cnt,
.order = (int**)g_5combi.order,
.op = (t_op **)g_5combi.op
};
}
int brute_force(t_mlxptr mlx, t_stack *st[2], t_queue *instr)
{
const int *const a_raw = stkcbegin(st[ST_A]); // pointer to a int array
const size_t size = stksize(st[ST_A]); // nbcount
int i;
t_combi combi[4];
init_combi(combi);
if (stksorted(st[ST_A]))
return (0);
if (size > 5)
return (1);
i = -1;
while ( i < combi[size - 2].cnt)
{
if (same_order(combi[size - 2].order[i], a_raw, size))
apply_instructions(mlx, st, combi[size - 2].op[i], instr);
}
return (0);
}
uj5u.com熱心網友回復:
一個大問題是您將指標隱藏在 typedef 后面。這只有一個目的:混淆程式員和其他閱讀代碼的人。
例如, t_op **op;在沒有 typedef 的情況下擴展會創建一個型別為的函式指標
typedef const char *(***t_op)(t_stack *, t_stack *);
正如你所希望的那樣,這簡直是瘋了。在你做任何其他事情之前,你需要擺脫這些型別定義和不必要的多重間接層。
一個明智typedef的函式指標可能看起來像這樣(它實際上是一個函式的 typedef):
typedef char* t_op (t_stack*, t_stack*);
然后您將其用作t_op* op.
總體而言,你的程式的方式太復雜的東西,聽起來像一個相當簡單的任務。作為另一個例子,任何地方都不應該有任何強制轉換——所有這些都是為了隱藏錯誤。
uj5u.com熱心網友回復:
主要問題是您將指向某種型別陣列的指標轉換為指向某種型別指標的指標。例如,在這段代碼中:
void init_combi(t_combi combi[4])
{
combi[0] = (t_combi) {
.cnt = g_2combi.cnt,
.order = (int**)g_2combi.order,
.op = (t_op **)g_2combi.op
};
/* ... */
}
g_2combi.order是int:的二維陣列int [CNT_2][2]。在初始化程式中,該值被轉換為指向其第一個元素的指標。元素的型別是int [2](array length 2 of int),所以指向元素的指標的型別是int (*)[2](pointer to array length 2 of int)。但是,型別轉換操作正在將其轉換為指向不兼容型別的int **指標(指向 指向 的指標的指標int)。combi[0].order[0]應該是 type int *,它與底層物件的型別不兼容:int [2]。
類似地,g_2combi.op是一個二維陣列t_op: t_op [CNT_2][2]。在初始化程式中,該值被轉換為指向其第一個型別元素t_op [2](陣列長度 2 of t_op)的指標,因此該指標的型別為t_op (*)[2](指向陣列長度 2 of 的指標t_op)。型別轉換操作將指標轉換為t_op **(pointer to pointer to t_op)。combi[0].op[0]應該是 type t_op *,它與底層物件的型別不兼容:t_op [2]。
解決這一問題的一種方法是定義所有的變數g_2combi,g_3combi等等是同一型別的t_combi。保持一切不變,復合文字可用于初始化等中的指標g_2combi.order。例如:
typedef struct s_combi {
int cnt;
const int * const *order;
const t_op * const *op;
} t_combi;
static const t_combi g_2combi = {
.cnt = CNT_2,
.order = (const int * const []){
(const int []){0, 1},
(const int []){1, 0}
},
.op = (const t_op * const []){
(const t_op []){NULL},
(const t_op []){sa, NULL}
}
};
/* define g_3combi etc. in a similar way to the above. */
void init_combi(t_combi combi[4])
{
combi[0] = g_2combi;
combi[1] = g_3combi;
combi[2] = g_4combi;
combi[3] = g_5combi;
}
(由于const上面的添加,函式的op引數apply_instruction需要從 更改t_op *op為const t_op *op。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314975.html
上一篇:使用物件指標訪問運算子多載函式
下一篇:C 指標和記憶體分配
