- 论坛徽章:
- 0
|
- #include <stdlib.h>
- #include <stdio.h>
- #define INIT_SIZE 4
- typedef struct stufile
- {
- .......
-
- }record;
- record * Init_List()
- {
- int i;
- record *L, *Ltemp;
- L = (record *)malloc(sizeof(record));
- L->next = NULL;
-
- record *Lsec = L;
-
- for( i = 0; i < INIT_SIZE; i++)
- {
- Ltemp = (record *)malloc(sizeof(record));
- Ltemp->numb = i;
- Ltemp->score = (float)(90 - i);
- // Ltemp->next = NULL; //问题原因
- Lsec->next = Ltemp;
- Lsec = Ltemp;
- }
-
- return L;
- }
- int List_Length(record *L)
- {
- int total_len = 0;
- record * temp = L;
- while(temp->next != NULL) //vs下这里出错,
- {
- ++total_len;
- temp = temp->next;
- }
- return total_len;
- }
- int main()
- {
- int i = 0;
- record *L1 = NULL;
- L1 = Init_List();
-
- i = List_Length(L1);
- printf("create list length is:%d\n", i);
- }
复制代码 上面的很简单的一个程序,求链表的长度
出问题的地方在Init_List函数里面,没有设置NULL空指针,
因此,List_Length函数的List_Length会出现访问冲突(访存不合法),
但是在gcc里面这个没问题,按理说有些不严谨
但是想想看:
gcc处理时,就把NULL赋值给temp->next了,所以程序可以完好的结束。
vs中处理的是把temp->next弄成某个地址(随便的吧),不是NULL,所以
会出问题。
当然程序员自身要更谨慎(都是自己的错,呵呵),这里只是说一下调试碰到的问题。
ps: 调试环境 gcc:4.4.3 gdb:7.1-ubuntu
vs:2005 |
|