guobinname_301 发表于 2012-12-12 10:58:44

int devices_init (void) 设备初始化的问题咨询

在int devices_init (void)中会依次
调用devlist = ListCreate (sizeof (device_t))    //device_t 是个结构体。
list_t ListCreate (int elementSize)
{
        list_t list;
        list = (list_t) (NewHandle (sizeof (ListStruct)));
        if (list) {
                (*list)->signature = LIST_SIGNATURE;
                (*list)->numItems = 0;
                (*list)->listSize = 0;
                (*list)->itemSize = elementSize;
                (*list)->percentIncrease = kDefaultAllocationPercentIncrease;
                (*list)->minNumItemsIncrease =
                                kDefaultAllocationminNumItemsIncrease;
        }

        return list;
}

Handle NewHandle (unsigned int numBytes)
{
        void *memPtr;
        HandleRecord *hanPtr; /* HandleRecord是一个结构*/

        memPtr = calloc (numBytes, 1); /* 分配ListStruct结构地址空间*/
        hanPtr = (HandleRecord *) calloc (sizeof (HandleRecord), 1);/* 分配HandleRecord结构地址空间*/
        if (hanPtr && (memPtr || numBytes == 0)) {
                hanPtr->ptr = memPtr;   /*hanPtr->ptr指向ListStruct结构的地址*/
                hanPtr->size = numBytes;/*hanPtr->size指向ListStruct结构的大小*/
                return (Handle) hanPtr;
        } else {
                free (memPtr);
                free (hanPtr);
                return NULL;
        }
}
问大侠:上面 return (Handle) hanPtr   //是HandleRecord结构体指针。
      typedef struct
    {
    void            *ptr;
    unsigned int    size;
    } HandleRecord;

      
         list = (list_t) (NewHandle (sizeof (ListStruct))); 这句我理解的意思
是将HandleRecord结构体的地址与ListStruct结构体的地址相同。
       typedef struct ListStructTag
    {
   ......
      } ListStruct;

      typedef struct ListStructTag **list_t;,

那么在下面的语句中是否会存在ListStruct结构体中的内容覆盖HandleRecord结构体中的内容?
if (list) {
                (*list)->signature = LIST_SIGNATURE;
                (*list)->numItems = 0;
                (*list)->listSize = 0;
                (*list)->itemSize = elementSize;
                (*list)->percentIncrease = kDefaultAllocationPercentIncrease;
                (*list)->minNumItemsIncrease =
                                kDefaultAllocationminNumItemsIncrease;
        }

如果不会覆盖的话,那是否是hanPtr->ptr = memPtr;   /*hanPtr->ptr指向ListStruct结构的地址*/
                hanPtr->size = numBytes;/*hanPtr->size指向ListStruct结构的大小*/
这2个语句的顺序不能更改?如果将顺序更改成程序是否会出现问题?

hanPtr->size = numBytes;/*hanPtr->size指向ListStruct结构的大小*/
hanPtr->ptr = memPtr;   /*hanPtr->ptr指向ListStruct结构的地址*/
不知道我讲明白了没有:dizzy:
               

embedsky_lhh 发表于 2012-12-12 14:49:06

楼上的大哥发个贴写就像写技术文档一样,不看标题还以为你是发表技术文章呢,这么长的问题,来论坛讨论的大家一看到这么长的问题估计都跑了,所以建议你以技术问搞或者心得为主题,然后把你知道的想的总结的贴出来,然后再在其中贴出你的问题,这样跟帖的人和回复的及膜拜的估计都会有,
我看了下你贴的代码,NewHandle 创建一个表头,他返回的表头的句柄, ListCreate 这个创建一个链表,返回链表的句柄,创建链表里边肯定需要有个表头,要不然遍历就麻烦了,所以就先创建了表头,这里表头的句柄也就是等于链表的句柄。

guobinname_301 发表于 2012-12-12 16:30:05

embedsky_lhh 发表于 2012-12-12 14:49 static/image/common/back.gif
楼上的大哥发个贴写就像写技术文档一样,不看标题还以为你是发表技术文章呢,这么长的问题,来论坛讨论的大 ...

谢谢大侠了,现阶段还不太明白表头,链表,句柄之间的关系,上网在补习补习,
我还想知道我上面提到的将这2个语句的顺序写颠倒了。
hanPtr->ptr = memPtr;   /*hanPtr->ptr指向ListStruct结构的地址*/
hanPtr->size = numBytes;/*hanPtr->size指向ListStruct结构的大小*/

改成hanPtr->size = numBytes;
      hanPtr->ptr = memPtr;
是否就会存在大侠所说的遍历就出现问题,表头,链表,句柄之间的联系就发生了错乱。

embedsky_lhh 发表于 2012-12-12 17:03:51

guobinname_301 发表于 2012-12-12 16:30 static/image/common/back.gif
谢谢大侠了,现阶段还不太明白表头,链表,句柄之间的关系,上网在补习补习,
我还想知道我上面提到的将 ...

不会的,你指定了成员遍量就可以不管顺序了,如果是映射就要考虑定义的时候的顺序
页: [1]
查看完整版本: int devices_init (void) 设备初始化的问题咨询