Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337971
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 421
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-03 15:18
个人简介

做笔记用,多为转载。

文章分类

全部博文(73)

文章存档

2016年(2)

2015年(29)

2014年(19)

2013年(23)

我的朋友

分类: C/C++

2013-11-04 13:16:03

1、  结构体的使用什么时候用“.”什么时候用“->

这个取决于所使用的是结构体变量还是结构体指针变量,以该结构体为例:

 struct ecc_cnt{

    uint ecc_correctcnt;

    uint ecc_wrongcnt;

    uint ecc_badcnt;

};

结构体变量:struct  ecc_cnt  ecc_count;那么ecc_count就是结构体变量,当引用该结构体的成员时就要使用ecc_count. ecc_correctcnt. 为它分配了sizeof(struct struct_name) 个字节的空间用来保存结构体本身。

结构体指针变量:struct  ecc_cnt  *ecc_count;那么ecc_count就是结构体指针变量,ecc_count代表指向结构体的指针(也就是个地址),那么要引用成员的时候要用ecc_count-> ecc_correctcnt,等价于(*ecc_count). ecc_correctcnt。只为它分配了四个字节的空间用来保存结构体的指针。

2、  结构体指针变量的使用

对于结构体指针变量的使用经常会犯一个错误,就是在定义完之后就直接使用了,不管怎么说你是定义了一个指针,既然是指针就一定要指向一个已知的地址,否则就是个野指针,很危险!正确使用如下:

int func(void)

{

struct ecc_cnt *ecc_count;

        ecc_count = malloc(sizeof(struct ecc_cnt));

                   …….

                   free(ecc_count);

}

3、  关于用printf打印 “%

%代表输出,那么如果想输出%就要再加一个%,例如:

printf("complete %ld%%",(i * 100/times));

4、  关于接口函数

xxx.c中有几个全局变量保存了一些信息,我们在***.c中需要使用这几个变量的信息,于是我们可以在xxx.c中做个接口函数,将这几个变量用函数参数的方式传递出来,例如:

xxx_nand.c中,我们需要导出ecccorrectcnt,eccwrongcnt,eccbadcnt

struct ecc_cnt{

    uint ecc_correctcnt;

    uint ecc_wrongcnt;

    uint ecc_badcnt;

};

uint ecccorrectcnt;

uint eccwrongcnt;

uint eccbadcnt;

static int ti81xx_correct_data(struct mtd_info *mtd, uint8_t *dat,

                               uint8_t *read_ecc, uint8_t *calc_ecc)

{

   uint32_t orig_ecc, new_ecc, res, hm;

   uint16_t parity_bits, byte;

   uint8_t bit;

 

   /* Regenerate the orginal ECC */

   orig_ecc = gen_true_ecc(read_ecc);

   new_ecc = gen_true_ecc(calc_ecc);

   /* Get the XOR of real ecc */

   res = orig_ecc ^ new_ecc;

   if (res) {

            /* Get the hamming width */

            hm = hweight32(res);

            /* Single bit errors can be corrected! */

            if (hm == 12) {

            ecccorrectcnt++;

                     /* Correctable data! */

                     parity_bits = res >> 16;

                     bit = (parity_bits & 0x7);

                     byte = (parity_bits >> 3) & 0x1FF;

                     /* Flip the bit to correct */

                     dat[byte] ^= (0x1 << bit);

            } else if (hm == 1) {

            eccwrongcnt++;

                     printf("Error: Ecc is wrong\n");

                     /* ECC itself is corrupted */

                     return 2;

            } else {

                     /*

                      * hm distance != parity pairs OR one, could mean 2 bit

                      * error OR potentially be on a blank page..

                      * orig_ecc: contains spare area data from nand flash.

                      * new_ecc: generated ecc while reading data area.

                      * Note: if the ecc = 0, all data bits from which it was

                      * generated are 0xFF.

                      * The 3 byte(24 bits) ecc is generated per 512byte

                      * chunk of a page. If orig_ecc(from spare area)

                      * is 0xFF && new_ecc(computed now from data area)=0x0,

                      * this means that data area is 0xFF and spare area is

                      * 0xFF. A sure sign of a erased page!

                      */

                     if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))

                               return 0;

                     printf("Error: Bad compare! failed\n");

            eccbadcnt++;

                     /* detected 2 bit error */

                     return -1;

            }

   }

   return 0;

}

/*get the ecc error count byl*/

int get_ecc_cnt(struct ecc_cnt *ecc_error)

{

    struct ecc_cnt *ecc_count = ecc_error;

   

    ecc_count = malloc(sizeof(struct ecc_cnt));

   

    ecc_count->ecc_correctcnt = ecccorrectcnt;

    ecc_count->ecc_wrongcnt = eccwrongcnt;

    ecc_count->ecc_badcnt = eccbadcnt;

   

    return 0;

}

我们做了接口函数get_ecc_cnt,用结构体指针变量作为函数参数,将变量导出。

在cmd_nandtest.c中外部声明下函数和结构体

extern struct ecc_cnt{

    uint ecc_correctcnt;

    uint ecc_wrongcnt;

    uint ecc_badcnt;

};

extern int get_ecc_cnt(struct ecc_cnt *ecc_error);

然后就可以使用了

struct ecc_cnt *ecc_count;

ecc_count = malloc(sizeof(struct ecc_cnt));

free(ecc_count);

ret = get_ecc_cnt(ecc_count);

这里在用该函数前会free掉ecc_count,因为在get_ecc_cnt的实现中已经malloc了函数的参数。

阅读(964) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~