免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1188 | 回复: 3
打印 上一主题 下一主题

高手看看链队列有没有问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-16 22:42 |只看该作者 |倒序浏览
我在GDB和VC里调试过,没有发现问题,为了没有错,请大家看看。谢谢
主要是看看REQ里的info操作有没有错。


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



typedef struct _REQUEST
{
        char id[20];
        char node[24];
        char passwd[24];
        char *info;
        int  len;                //it is len of info

        struct _REQUEST *next;
}REQ;

struct TaskQueue
{
        REQ *front;
        REQ *rear;
        int Size;
};
struct TaskQueue CurTaskQueue;


void InitQueue();
void Enqueue(char *id, char *node, char *passwd, char *info, int InfoLen);
void Dequeue(REQ *req);

int main(int argc, char **argv)
{
        char info[100]={0};
        REQ req;
        InitQueue();
       
        Enqueue("id1", "node1", "passwd1", "info1", 5);
        Enqueue("id2", "node2", "passwd2", "info2", 5);

        Dequeue(&req);
        printf("a:%sn",req.info);
        memcpy(info, req.info, req.len);
        free(req.info);

        Dequeue(&req);
        printf("b:%sn",req.info);
        memcpy(info, req.info, req.len);
        free(req.info);

        exit(0);
}




void InitQueue()
{
        CurTaskQueue.front=NULL;
        CurTaskQueue.rear=NULL;
        CurTaskQueue.Size = 0;
        return;
}


void Enqueue(char *id, char *node, char *passwd, char *info, int InfoLen)
{

        REQ *item = (REQ *)malloc(sizeof(REQ));
        if(item == NULL)
        {
                perror("Enqueue malloc errorn");
                exit(-1);
        }
        memset(item, 0, sizeof(REQ));

        strcpy(item->id, id);
        strcpy(item->node, node);
        strcpy(item->passwd, passwd);

        item->info = (char *)malloc(InfoLen+1);
        memset(item->info, 0, InfoLen+1);
        memcpy(item->info, info, InfoLen);
        item->len = InfoLen;


        if (CurTaskQueue.front == NULL)
        {
                CurTaskQueue.front = item;
                CurTaskQueue.rear = item;
        }
        else
        {
                CurTaskQueue.rear->next = item;
                CurTaskQueue.rear = CurTaskQueue.rear->next;
                CurTaskQueue.rear->next = NULL;
        }
        CurTaskQueue.Size++;
        return;
}



void Dequeue(REQ *req)
{
        REQ *temptask = NULL;
        memset(req, 0, sizeof(REQ));


        if (CurTaskQueue.front == NULL)
        {
                CurTaskQueue.Size=0;
                req = NULL;
                return;
        }
        else
        {
                temptask = CurTaskQueue.front;

                strcpy(req->id, temptask->id);
                strcpy(req->node, temptask->node);
                strcpy(req->passwd, temptask->passwd);
                req->len =  temptask->len;
                req->info = temptask->info;

                CurTaskQueue.front = CurTaskQueue.front->next;
                free(temptask);
                CurTaskQueue.Size--;
        }
}

[ 本帖最后由 reviewmyself 于 2005-11-16 22:43 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-11-16 23:10 |只看该作者
提两个问题。
1。在Dequeue中,CurTaskQueue.Size=0;有什么意义?
2。在Dequeue中,req->info = temptask->info;
这样直接赋值可以吗,req->nfo不用分配空间了?

论坛徽章:
0
3 [报告]
发表于 2005-11-17 10:30 |只看该作者

re

原帖由 lenovo 于 2005-11-16 23:10 发表
提两个问题。
1。在Dequeue中,CurTaskQueue.Size=0;有什么意义?
2。在Dequeue中,req->info = temptask->info;
这样直接赋值可以吗,req->nfo不用分配空间了?

解答:
问题1:CurTaskQueue.Size=0;用于条件变量。一些互斥与条件变量我删除

问题2:在函数Enqueu里申请内存,还有CurTaskQueue是个全局变量

item->info = (char *)malloc(InfoLen+1);
        memset(item->info, 0, InfoLen+1);
        memcpy(item->info, info, InfoLen);
        item->len = InfoLen;

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2005-11-17 12:52 |只看该作者
我明白你的意思了。
当出了req变量的作用域后,
req.info空间你是怎么释放的?

还有当CurTaskQueue.front == NULL成立时,
CurTaskQueue.Size一定等于0。那句话不是多余吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP