游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2666|回复: 4

调试程序,调试窗口有点不明白

[复制链接]

10

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
发表于 2011-6-17 21:04:00 | 显示全部楼层 |阅读模式

pa_last = La->elem + La->length - 1;
La->elem=0x00384058
La->length = 3
pa_last = La->elem + La->length - 1应该等于0x0038405a
为什么调试窗口pa_last =0x003840a8

10

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
 楼主| 发表于 2011-6-17 21:06:00 | 显示全部楼层

Re:调试程序,调试窗口有点不明白

学习数据结构中遇到的一个控制台程序,全部代码
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>

#define ERROR 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

struct STU
{
        char name[20];
        char stuno[10];
        int age;
        int score;
}stu[50];
typedef struct STU ElemType;

struct LIST
{
        ElemType *elem;
        int length;
        int listsize;
};
typedef struct LIST List;

//初始化线形表
int init(List *L)
{
        L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
        if(!L->elem)
                exit(OVERFLOW);
        L->length = 0;
        L->listsize = LIST_INIT_SIZE;
        return OK;
}/*init */

//返回线形表长度
int ListLength(List *L)
{
        return L->length;
}

void GetElem(List L, int i, ElemType *e)
{
        *e=L.elem;
}

int EqualList(ElemType *e1, ElemType *e2)
{
        if (strcmp(e1->name, e2->name)==0)
                return 1;
        else
                return 0;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{
        if (strcmp(e1->name,e2->name)<=0)
                return 1;
        else
                return 0;
}

int LocateElem(List *La, ElemType e, int type)
{
        int i;
        switch (type)
    {
        case EQUAL:
                for(i=0; i<La->length; i++)
                        if(EqualList(&La->elem, &e))
                                return 1;
                        break;
        default:
                break;
    }
        return 0;
}

void MergeList(List *La, List *Lb, List *Lc)
{
        ElemType *pa, *pb, *pc, *pa_last, *pb_last;
        pa = La->elem;
        pb = Lb->elem;
        Lc->listsize = Lc->length = La->length + Lb->length;
        pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType));
        if(!Lc->elem)  
                exit(OVERFLOW);
        pa_last = La->elem + La->length - 1;
        pb_last = Lb->elem + Lb->length - 1;
        while(pa<=pa_last && pb<=pb_last)
    {
                if(Less_EqualList(pa, pb))
                        *pc++ = *pa++;
                else
                        *pc++ = *pb++;
    }
        while(pa<=pa_last)
                *pc++ = *pa++;
        while(pb <= pb_last)
                *pc++ = *pb++;
}

int ListInsert(List *L, int i, struct STU e)
{
        struct STU *p,*q;
        if (i<1||i>L->length+1)
                return ERROR;
        q=&(L->elem[i-1]);
        for(p=&L->elem[L->length-1]; p>=q; --p)
                *(p+1)=*p;
        *q=e;
        ++L->length;
        return OK;
}/*ListInsert Before i */

void UnionList(List *La, List *Lb)
{
        int La_len,Lb_len;
        int i;
        ElemType e;
       
        La_len=ListLength(La);
        Lb_len=ListLength(Lb);
        for(i=0; i<Lb_len; i++)
    {
                GetElem(*Lb, i, &e);
                if(!LocateElem(La, e, EQUAL))
                        ListInsert(La, ++La_len, e);
    }
       
}

void printlist(List L)
{
        int i;
        printf("name       stuno        age     score\n");
        for(i=0; i<L.length; i++)
                printf("%-10s %s\t%d\t%d\n",  L.elem.name,  L.elem.stuno,
                L.elem.age,  L.elem.score);
        printf("\n");
}

void main()
{
        struct STU e;
        List La, Lb,  Lc;
       
        system("cls");
       
        printf("\n\n-------------------List Demo is running...----------------\n\n");
        printf("First is InsertList function.\n");
        init(&La);
       
        //插入stu1
        strcpy(e.name,"stu1");
        strcpy(e.stuno,"100001");
        e.age=80;
        e.score=1000;
        ListInsert(&La, 1, e);
       
        //插入stu3
        strcpy(e.name,"stu3");
        strcpy(e.stuno,"100002");
        e.age=80;
        e.score=1000;
        ListInsert(&La,2,e);
       
        //打印List A
        printlist(La);
        printf("List A length now is  %d.\n\n",La.length);
        getch();
       
        //插入stu5
        strcpy(e.name,"stu5");
        strcpy(e.stuno,"100003");
        e.age=80;
        e.score=1000;
        ListInsert(&La,3,e);
       
        //打印List A
        printlist(La);
        printf("List A length now is  %d.\n\n",La.length);
        getch();
       
        init(&Lb);
       
        //Lb表插入stu2
        strcpy(e.name,"stu2");
        strcpy(e.stuno,"100001");
        e.age=80;
        e.score=1000;
        ListInsert(&Lb,1,e);
       
        //Lb表插入stu4
        strcpy(e.name,"stu4");
        strcpy(e.stuno,"100002");
        e.age=80;
        e.score=1000;
        ListInsert(&Lb,2,e);
       
        //Lb表插入stu6
        strcpy(e.name,"stu6");
        strcpy(e.stuno,"100001");
        e.age=80;
        e.score=1000;
        ListInsert(&Lb,3,e);
       
        //打印List B
        printlist(Lb);
        printf("List B length now is  %d.\n\n",Lb.length);
        getch();
       
        MergeList(&La, &Lb, &Lc);
        printlist(Lc);
        getch();
       
        printf("Second is UnionList function.\n");
        printf("Now union List A and List B.....\n");
        UnionList(&La, &Lb);
        printlist(La);
        printf("List A length now is  %d.\n\n",La.length);
        getch();
       
       
        printf("\n\n\n\n\n\nWelcome to visit http://zmofun.heha.net !\n\n\n\n\n\n\n");
       
}

10

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
 楼主| 发表于 2011-6-17 21:10:00 | 显示全部楼层

Re:调试程序,调试窗口有点不明白

图片部分看不到,可以打开附件里的图片

2

主题

429

帖子

435

积分

中级会员

Rank: 3Rank: 3

积分
435
发表于 2011-6-18 00:15:00 | 显示全部楼层

Re:调试程序,调试窗口有点不明白

pLast = pFirst + x
不是表面上那样是pFist的地址加上x的偏移。
它实际上表示的是, pFirst + x * sizeof(*pFirst)。即x表示的是x个pFirst所指向对象的大小。

10

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
 楼主| 发表于 2011-6-19 08:31:00 | 显示全部楼层

Re: Re:调试程序,调试窗口有点不明白

Enigmaya: Re:调试程序,调试窗口有点不明白

pLast = pFirst + x
不是表面上那样是pFist的地址加上x的偏移。
它实际上表示的是, pFirst + x * sizeof...

明白了,谢谢
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-8 13:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表