【发布时间】:2021-03-31 18:45:27
【问题描述】:
所以有这样的代码:
struct HT_Task
{
int tid; /*Task's identifier*/
int difficulty; /*Task's difficulty*/
struct HT_Task *next; /*Pointer to the next node*/
};
struct General_Tasks_HT
{
int count; /*Count of tasks*/
struct HT_Task *tasks[]; /*General tasks hash table*/
};
struct General_Tasks_HT general_tasks_ht; // global variable
unsigned int max_tasks_g;
要为哈希表分配内存,我执行以下操作:
for( i=0; i<max_tasks_g; i++)
{
general_tasks_ht.tasks[i] = malloc(sizeof(struct HT_Task) * max_tasks_g);
}
for( i=0; i<max_tasks_g; i++)
{
general_tasks_ht.tasks[i] = NULL;
}
问题是,当我在 Windows 笔记本电脑上编译并运行它时,它运行良好,但在 Mac 笔记本电脑上,它在 for 循环中出现分段错误。为什么会发生这种情况,哪个是正确的? (在 mac 上,clang 版本是 12.0.0,在 windows MinGw 上是 9.2.0)
【问题讨论】:
-
通常情况下,两个编译器都是正确的,而代码是错误的:)
标签: c windows unix struct compilation