日期:2014-05-16  浏览次数:20609 次

win&linux 在同一个问题上的不同表现


?我在写单链表的逆转时,遇到了一些很有意思的事情。

?

同样一段代码,在 win linux 下是完全两种不同的结果。

#include <stdio.h>


#include <stdlib.h>


#include <string.h>


#define SIZE    64





typedef struct info{


    int     num;


    char    msg[SIZE];


    struct info *next;


}Info;





typedef struct vec{


    Info *element;


    struct vec *next;


}Vector;





const unsigned int SIZE_INFO = sizeof(Info);





Info *createInfo(Info *header, int nums)


{


    int num = 0;


    char *msg = "hello msg";


    Info *tmp = NULL;


    Info *h2 = header;


    int i = 0;


    #define NUM 4


    char strNum[NUM];


    memset(strNum, 0x00, NUM);


    header->num = 0;





    for (i = 0; i < nums; i++) {


        tmp = (Info *)calloc(SIZE_INFO, 1);


        tmp->num = num++;


        strcpy(tmp->msg, msg);


        sprintf(strNum, ": %d", tmp->num);


        strcat(tmp->msg, strNum);


        h2->next = tmp;


        h2 = h2->next;


        h2->next = NULL;


        header->num++;


	memset(strNum, 0x00, NUM);


    }





    return header;


}





void  deleteInfo(Info *header)


{


    Info *tmp   = header->next;


    Info *tmp2  = NULL;


    while(tmp != NULL) {


        tmp2 = tmp->next;


        free(tmp);


        tmp = tmp2;


    }


    header->next = NULL;


}





void printInfo(const Info *header)


{


    Info *tmp = header->next;


    while (tmp != NULL) {


        printf("[num]->%d\t[msg]->%s\n", tmp->num, tmp->msg);


        tmp = tmp->next;


    }


    printf("**END**\n");


}





Info *reserve(Info *header)


{


    Info *tail   = NULL;


    Info *tmp    = NULL;


    Vector *v    = (Vector *)calloc(sizeof(Vector), 1);


    v->next      = NULL;


    Vector *vTmp = NULL;





    tmp = header;


    while(tmp->next != NULL) {


        tmp = tmp->next;


        vTmp = (Vector *)calloc(sizeof(Vector), 1);


        vTmp->element = tmp;


        if (v->next == NULL) {


            v->next = vTmp;


            vTmp->next = NULL;


        } else {


            vTmp->next = v->next;


            v->next = vTmp;


        }


    }





    vTmp = v;


    tmp = header;


    while (vTmp->next != NULL) {


        tmp->next = vTmp->next->element;


        vTmp = vTmp->next;


        tmp = tmp->next;


    }


    tmp->next = NULL;





    vTmp = v->next;


    Vector *vTmp2 = NULL;


    while (vTmp != NULL) {


        vTmp2 = vTmp->next;


        free(vTmp);


        vTmp = vTmp2;


    }


    vTmp = NULL;





    free(v);


    v = NULL;


    return header;


}





void re(Info *header, Info *cur)


{


    if (cur->next == NULL) {


        header->next = cur;


        cur->next = NULL;


    } else {


        Info *a = NULL;


        Info *b = NULL;


        if (header == cur) {


            a = cur->next;


            b = a->next;


        } else {


            a = cur;


            b = cur->next;


        }


        if (b != NULL) {


            re(header, b);


            b->next = a;


            if (cur == header) a->next = NULL;


        }


    }


}





int main(int argc, char *argv[])


{


    Info *header = (Info *)calloc(SIZE_INFO, 1);


    int num;


    if (argc == 1) num = 11;


    else sscanf(argv[1], "%d", &num);





    header = createInfo(header, num);


    printInfo(heade