| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 342 人关注过本帖
标题:求助,为什么这段代码运行的结果出现内存不能读
只看楼主 加入收藏
_leaf
Rank: 1
来 自:江西农业大学
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-12-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
求助,为什么这段代码运行的结果出现内存不能读
#include<stdio.h>
#include<malloc.h>
#define Maxsize 100
#define NULL 0

typedef struct         //定义迷宫
{
    int arry[Maxsize][Maxsize];   
    int max_x,max_y;            
}sd;

typedef struct point        
{
    int vex_x,vex_y;        
    struct point *next;   
    int direction;         
}Point;

sd create()         
{
    int i,j,x;
    sd a;
    printf("Enter row and lie:");
    scanf("%d  %d",&a.max_x,&a.max_y);   
    printf("提示:请输入0或1,其中0表示通路,1表示不通\n");
    for(i=1;i<=a.max_x;i++)
    {
        printf("enter %d row, (please enter %d datas):",i,a.max_y);
        for(j = 1;j<= a.max_y;j++)
        {
            scanf("%d",&a.arry[i][j]);
        }
    }
    return a;
}


int found(int x,int y,Point *head)  
{
    Point *p = head;
    while(p!=NULL)
    {
        if(x==p->vex_x&&y==p->vex_y)
        return 1;
        p=p->next;
    }
    return 0;
}


Point *secret(sd a)  
{
    Point *top,*p;   
    int i,j,m,x,y;
    p = (Point*)malloc(sizeof(Point));  
    p->vex_x-1;p->vex_y=1;p->next=NULL;
    top=p;        
    j=1;        
    do{
        while(j<=4)
        {
            m=0;  
            x=top->vex_x;
            y=top->vex_y;
            switch(j)
            {
                case 1:   
                    if(y+1<=a.max_y&&!a.arry[x][y+1]&&!found(x,y+1,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x;p->vex_y=y+1;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 2:
                    if(x+1<=a.max_x&&!a.arry[x+1][y]&&!found(x+1,y,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x+1;p->vex_y=y;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 3:
                    if(y-1>1&&!a.arry[x][y-1]&&!found(x,y-1,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x;p->vex_y=y-1;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 4:
                    if(x-1>=1&&!a.arry[x-1][y]&&!found(x-1,y,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x-1;p->vex_y=y;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
            }
            if(m!=0)  
                {j=1;break;}
              else
                j++;  
            }
        if(j>4)  
        {
            if(top!=NULL)   
            {
                top=top->next;j=top->direction+1;top->direction=j;
            }
            else return NULL;   
        }
    }while(top->vex_x!=a.max_x||top->vex_y!=a.max_y);
    return top;
}

void disp(Point *po)
{
    int i=0,top=0;
    Point *stack[Maxsize];
    if(po==NULL)
        printf("NO ROAD!\n");
    else
    {
        while(po!=NULL)
        {
            stack[top++]=po;
            po=po->next;
        }
        while(top>0)
        {
            top--;
            printf("(%d,%d,%d)",stack[top]->vex_x,stack[top]->vex_y,stack[top]->direction);
            i++;
            if(i%8==0)
                printf("\n");
        }
    }
    printf("\n");
}

 main()
{
    sd road;
    Point *po;
    road=create();
    po=secret(road);
    disp(po);
    getchar();
}
搜索更多相关主题的帖子: create include direction 内存 
2012-12-15 21:13
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:2 
回复 楼主 _leaf
不懂,帮顶

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2012-12-17 13:00
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:4 
#define NULL 0

不用定义NULL,<stdio.h>头文件就含有了
太长懒得看,只能帮顶...

I have not failed completely
2012-12-19 21:51
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:14 
帮楼主排版。。。。。

请楼主加些注释吧,看不懂,,,,

问题应该是create函数中的sd被释放了。。。另外,楼主要加一些free语句。
程序代码:
#include <stdio.h>
#include <malloc.h>
#define Maxsize 10
#define NULL 0
typedef struct         //定义迷宫
{
    int arry[Maxsize][Maxsize];  
    int max_x, max_y;           
}sd;
typedef struct point      
{
    int vex_x, vex_y;      
    struct point *next;  
    int direction;       
}Point;
sd a;//全局变量,避免释放
void create()        
{
    int i, j;
    //初始化
    for (i = 0;i < Maxsize;++i)
        for (j = 0;j < Maxsize;++j)
            a.arry[i][j] = 1;

    printf("Enter row and lie:");
    scanf("%d  %d", &a.max_x, &a.max_y);  
    printf("提示:请输入0或1,其中0表示通路,1表示不通\n");
    for(i = 1;i <= a.max_x;i++)
    {
        printf("enter %d row, (please enter %d datas):", i, a.max_y);
        for(j = 1;j <= a.max_y;j++)
        {
            scanf("%d", &a.arry[i][j]);
        }
    }
}

int found(int x, int y, Point *head)
{
    Point *p = head;
    while(p != NULL)
    {
        if(x == p->vex_x && y == p->vex_y)
            return 1;
        p = p->next;
    }
    return 0;
}

Point *secret()
{
    Point *top, *p;  
    int j, m, x, y;
    p = (Point*)malloc(sizeof(Point));
    p->vex_x = 1;
    p->vex_y = 1;
    p->next = NULL;
    top = p;      
    j = 1;      
    do{
        while(j <= 4)
        {
            m = 0;
            x = top->vex_x;
            y = top->vex_y;
            switch(j)
            {
            case 1: 
                if(y + 1 <= a.max_y && !a.arry[x][y+1] && !found(x, y+1, top))
                {
                    p = (Point*)malloc(sizeof(Point));
                    p->vex_x = x;p->vex_y = y+1;p->next = top;
                    top->direction = j;
                    top = p;
                    m = 1;
                }
                break;
            case 2:
                if(x + 1 <= a.max_x && !a.arry[x+1][y] && !found(x+1, y, top))
                {
                    p = (Point*)malloc(sizeof(Point));
                    p->vex_x = x+1;p->vex_y = y;p->next = top;
                    top->direction = j;
                    top = p;
                    m = 1;
                }
                break;
            case 3:
                if(y - 1 > 1 && !a.arry[x][y-1] && !found(x, y-1, top))
                {
                    p = (Point*)malloc(sizeof(Point));
                    p->vex_x = x;p->vex_y = y-1;p->next = top;
                    top->direction = j;
                    top = p;
                    m = 1;
                }
                break;
            case 4:
                if(x - 1 >= 1 && !a.arry[x-1][y] && !found(x-1, y, top))
                {
                    p = (Point*)malloc(sizeof(Point));
                    p->vex_x = x-1;p->vex_y = y;p->next = top;
                    top->direction = j;
                    top = p;
                    m = 1;
                }
                break;
            }
            if(m != 0)
            {
                j = 1;break;
            }
            else
                j++;
        }
        if(j > 4)
        {
            if(top != NULL) 
            {
                top=top->next;
                j = top->direction + 1;
                top->direction = j;
            }
            else
                return NULL; 
        }
    }while(top->vex_x != a.max_x || top->vex_y != a.max_y);
    top->direction = 0;//没看懂这个direction是啥,就随便敲了个0
    return top;
}
void disp(Point *po)
{
    int i = 0, top = 0;
    Point *stack[Maxsize];
    if(po == NULL)
        printf("NO ROAD!\n");
    else
    {
        while(po != NULL)
        {
            stack[top++] = po;
            po = po->next;
        }
        while(top > 0)
        {
            top--;
            printf("(%d,%d,%d)", stack[top]->vex_x, stack[top]->vex_y, stack[top]->direction);
            i++;
            if(i % 8 == 0)
                printf("\n");
        }
    }
    printf("\n");
}
int main()
{
    Point *po;
    create();
    po = secret();
    disp(po);
    getchar();
    return 0;
} 


[fly]存在即是合理[/fly]
2012-12-19 23:53
快速回复:求助,为什么这段代码运行的结果出现内存不能读
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039881 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved