当前位置:编程学习 > C/C++ >>

C语言软件内存问题,高手来。

我写个程序,在tc上能正确执行,但是在vc++6.0上显示内存错误。程序里面确实用到了内存分配建立动态链表。我系统是Win7,vc++有兼容性问题。请问是我程序内存分配有问题,还是说我系统软件vc++的问题。很困惑啊。
补充:部分代码如下,帮我看看,我加分,谢谢

#include<stdio.h>
#include<stdlib.h>


struct student
{
    int num;
    int score;
    struct student *next;
};
typedef struct student Node;
int  sll_insert(Node **rootp,int new_value);
int sll_remove(Node **rootp,Node *movenode);
void main()
{
    struct student *head,*p1,*move;/*这里多设置一个*move指针是为了测试删除节点那个函数是否有效*/


    p1 = (struct student*)malloc(sizeof(struct student)) ;
    if(p1 == NULL)
    {
        printf("fail to memory it");
    }
    head = p1;
    printf("please input num and score");
    scanf("%d %d",&p1->num,&p1->score);
    while(p1->score>=0&&p1->num>=0)
    {
        p1 -> next = (struct student*)malloc(sizeof(struct student)) ;
        if(p1 == NULL)
        {
            printf("fail to memory it");
        }
        p1 = p1->next;
        printf("please input num and score");
        scanf("%d %d",&p1->num,&p1->score);
    }
    p1->next = NULL;
    p1 = head;
    while(p1!=NULL)
    {
        printf("%d ,%d\n",p1->num,p1->score);
        p1 = p1 -> next;


    }
    /*试验插入函数是否有效*/
    printf("%d\n",sll_insert(&head,4));
    p1 = head;
    while(p1!=NULL)
    {
        printf("%d ,%d\n",p1->num,p1->score);
        p1 = p1 -> next;


    }
    /*试验删除函数是否有效*/
    printf("%d\n",sll_remove(&head,move));
    p1 = head;
    while(p1!=NULL)
    {
        printf("%d %d\n",p1->num,p1->score);
        p1 = p1 -> next;


    }


    free(p1);
    getch();
}
追问:嗯,谢谢你,稍后采纳
答案:
      "程序里面确实用到了内存分配建立动态链表。"肯定是你的代码有问题,因为VC++对内存的检查管理非常严格;tc我没用过,我用Code::Blocks时遇到过类似的问题。
。。。多半还是 代码 有问题 tc上正确运行,不一定vc上就可以!!!

上一个:C语言结构体与共用体小问题
下一个:如何用C语言编一个俄罗斯方块

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,