分配值0的整數我有一個鏈表結構是這樣的malloc - C程序 - 在GCC
typedef struct list_node {
int data;
struct list_node *next;
}l_node;
void print_list(l_node *head) {
l_node *cur_node = head;
while(cur_node!=NULL) {
printf("%d\t", cur_node->data);
cur_node = cur_node->next;
}
}
void main() {
printf("List");
l_node *new_node = (l_node*)malloc(sizeof(l_node));
print_list(new_node);
}
當我編譯GCC linkedlist.c做./a.out 我得到的輸出 名單我試圖訪問無效的內存位置在cur_node-> next)。但是,當我在VC++中嘗試它時,我得到錯誤(因爲我試圖訪問無效的內存位置cur_node->下一步)。
那麼,gcc的malloc默認情況下是否將0值分配給結構體內的整型變量?爲什麼我在gcc中執行相同的print_list時沒有得到相同的錯誤?
感謝:-)剛剛讀到關於calloc – harihb 2011-05-08 06:44:29