Something about C

来源:互联网 发布:mac管理员名称和密码 编辑:程序博客网 时间:2024/05/02 00:32

What the hell? How long since I touch with C?
What a pity, I have to work with it now.

Global variable
Better define a global.h, define variable as
extern int i;

And when U use it in another C, do as
unsigned char SysEventCounter = 0;
U can do it in one C file, if U do it in another C file, it will broadcast error.

Operator
Sometimes U encounter & and &&, what’s difference between them? & is bit operator for instance: 5 & 3 = 3; As for &&, a logical operator, 1 && 3 = 1.

Pointer
To allocation two dimension dynamic array:

#include <malloc.h>reconstructedMVField**MVField;MVField=(reconstructedMVField**)malloc((picSizeY>>2) * sizeof(reconstructedMVField *));for(j = 0; j < (picSizeY >> 2); j++){    MVField[j]=(reconstructedMVField*)malloc((picSizeX>>2) * sizeof(reconstructedMVField));}  for(j = 0; j < (picSizeY >> 2); j++)    free(MVField[j]);free(MVField);

This guy’s blog is not bad.

http://www.cnblogs.com/chenwenbiao/archive/2011/11/04/2236679.html

http://blog.sina.com.cn/s/blog_5d6189e50100bbnd.html

http://blog.sina.com.cn/s/blog_67299aec0100mqek.html

http://zhidao.baidu.com/link?url=4pICOIhwQAtDmMO3LYT-rKMCY-OQWsQJ2TjKGQM56I_wCit92g_s1IaXoMODJOEzpXUol_CItlPVv6AnXBOCbq

MVField **a;MVField *temp;a = (MVField**)malloc(10*sizeof(MVField*));for (int i = 0; i < 10; i++)    a[i] = (MVField*)malloc(9*sizeof(MVField));for (int i = 0; i < 10; i++){    temp = a[i];    for (int j = 0; j < 9; j++)    {        (*temp).priority = i + j;        (*temp).MVdistance = 0;        (*temp).location.X = j;        (*temp).location.Y = i;        *temp++;    }}

http://blog.csdn.net/happen23/article/details/4710368

Type Transition

double temp_mvX = 0;double temp_mvY = 0;int mvX;int mvY;    int index_X = 0;int index_Y = 0;int region_cover_X = 0;int region_cover_Y = 0;temp_mvX = round( (refPic->mv_info[i_row][j_column]).mv[0].mv_x/4.0 );  // X MV in pixel temp_mvY = round( (refPic->mv_info[i_row][j_column]).mv[0].mv_y/4.0 );  // Y MV in pixel                // to get which region the 4x4 block has coveredmvX = temp_mvX;mvY = temp_mvY;region_cover_X = (mvX >= 0) ? (mvX % 4) : ((4 - abs(mvX) % 4)%4);region_cover_Y = (mvY >= 0) ? (mvY % 4) : ((4 - abs(mvY) % 4)%4);index_X = j_column + mvX / 4;  // to index block positionindex_Y = i_row + mvY / 4;

-> and .
If pointer, “->”
else “.”
But it doesn’t matter, as compiler will tell U.

Multiple Dimension
Be careful of X and Y. Hehe!

Defines like Taps

#ifdef  ERC_BYCOPY_YUV#undef  ERC_BYCOPY_YUV#endif
#define IntraMBinSliceAndMBNumInSlice   "D:/CY/JM18_6_RC/scene_change_p_slice.txt"
#if (DEBUG_OPEN == 1)    FILE *erc_dec_mv_info = NULL;    recfr.p_Vid = p_Vid;    VideoParameters *p_Vid1 = recfr.p_Vid;    StorablePicture* refPic = p_Vid1->ppSliceList[0]->listX[0][0];//p_Vid->ppSliceList[0]->listX[0][0];    #if(DEBUG_INFO_FOR_MV == 1)        erc_dec_mv_info = fopen(DEBUG_INFO_FOR_MV_TXT, "a+");        if(p_Vid->dec_picture->frame_num == 3)  // Just look the second frame's mv information and be attention to the start frame and end frame in Concealment_CY.h         {            for(i_row = 0;i_row < ((*dec_picture)->size_y) /4 ;i_row++)            {                for(j_column = 0;j_column < ((*dec_picture)->size_x) / 4;j_column++)                {                    fprintf(erc_dec_mv_info, "(%d,%d) -> (%d,%d)->(%d,%d)  ,",i_row,j_column,(refPic->mv_info[i_row][j_column]).mv[0].mv_x,(refPic->mv_info[i_row][j_column]).mv[0].mv_y,(refPic->mv_info[i_row][j_column]).ref_idx[0],(refPic->mv_info[i_row][j_column]).ref_idx[1]);                }                fprintf(erc_dec_mv_info,"\n");            }            fclose(erc_dec_mv_info);        }    #endif#endif

Bad Code

#define  _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <math.h>#include <malloc.h>#define LENCorrupetedBLKMVList sizeof(CorrupetedBLKMVList)#define AddrNull  0typedef struct motion_vector{    int mvX;    int mvY;} ercMV;// This struct means that the reconstructed MV of each 4x4 blocktypedef struct important_POS{    int Block_X;  // Here is just for P frame.     int Block_Y;            // 1 means the best suit, next 2, then 3, then 4, then 5 and other number means this mv is empty.}Position;void simpletest(ercMV Cst_mv){    printf("%d  %d\n",Cst_mv.mvX,Cst_mv.mvY);}typedef struct Corrupted_Per_Block_Ref_MV_Field_Information{    Position PPixle;    ercMV   estiMV;    int *  pixel;   // With chroma and luma }RefBLKMVInfor;// This struct is used for every corrupted block  typedef struct Corrupted_Block_MV_List{    RefBLKMVInfor  blkInfor;    struct Corrupted_Block_MV_List* next;}CorrupetedBLKMVList;/*! ************************************************************************ * \brief *      To create MV list for block in corrupted frameMV field. * \return *      Pointer of struct CorrupetedBLKMVList. * \param NONE * \2015/10/9 14:56:30 ************************************************************************ */CorrupetedBLKMVList* CreateMVFieldPerBlockMVListHead(void){    CorrupetedBLKMVList* head;       head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);    head -> next = AddrNull;    return head;}/*! ************************************************************************ * \brief *      To insert MV block node  for block in corrupted frameMV field. * \return *      Pointer head of struct CorrupetedBLKMVList. * \param NONE * \2015/10/9 14:56:30 ************************************************************************ */void InsertMVNodeIntoBlockList(CorrupetedBLKMVList* head, RefBLKMVInfor  insertData, int blockSize){    CorrupetedBLKMVList*temp;    int i;    temp = AddrNull;    temp = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);    if(temp == AddrNull)    {        printf("Error:In malloc memory for CorrupetedBLKMVList\n");        return;     }       temp -> next = AddrNull;    temp -> blkInfor.PPixle.Block_X =  insertData.PPixle.Block_X;    temp -> blkInfor.PPixle.Block_Y =  insertData.PPixle.Block_Y;    temp -> blkInfor.estiMV.mvX =  insertData.estiMV.mvX;    temp -> blkInfor.estiMV.mvY = insertData.estiMV.mvY;    temp -> blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));    for(i = 0; i < blockSize; i++)    {        temp -> blkInfor.pixel[i] = insertData.pixel[i];    }    head -> next = temp;}int main(){    int i = 0;    const int blockSize = 16;    RefBLKMVInfor  testInsertData;    RefBLKMVInfor  testInsertData2;    // Create node    CorrupetedBLKMVList* test_create = AddrNull;    CorrupetedBLKMVList* search = AddrNull;    test_create = CreateMVFieldPerBlockMVListHead();    if (test_create->next == AddrNull)        printf("Good!\n");    if(test_create != AddrNull)        printf("Create OK:%d \n",test_create);    //  Insert node    testInsertData.pixel = (int*)malloc(blockSize*sizeof(int));    for(i = 0; i < blockSize; i++)    {        testInsertData.pixel[i] = i;    }    testInsertData.PPixle.Block_X = 1;    testInsertData.PPixle.Block_Y = 2;    testInsertData.estiMV.mvX = 3;    testInsertData.estiMV.mvY = 5;    testInsertData2.pixel = (int*)malloc(blockSize*sizeof(int));    for (i = 0; i < blockSize; i++)    {        testInsertData2.pixel[i] = i+1;    }    testInsertData2.PPixle.Block_X = 100;    testInsertData2.PPixle.Block_Y = 21;    testInsertData2.estiMV.mvX = 23;    testInsertData2.estiMV.mvY = 15;    InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);    InsertMVNodeIntoBlockList(test_create, testInsertData2, blockSize);    InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);    search = test_create;    while (search != AddrNull)    {        printf("%d\n", search);        printf("%d\n", search->blkInfor.PPixle.Block_X);        printf("%d\n", search->blkInfor.PPixle.Block_Y);        printf("%d\n", search->blkInfor.estiMV.mvX);        printf("%d\n", search->blkInfor.estiMV.mvY);//      printf("%d\n", search->blkInfor.pixel[5]);        search = search->next;    }    search = test_create;    while (search->next != AddrNull)    {    //  free(search->blkInfor.pixel);        search = search->next;    }    free(testInsertData.pixel);     free(testInsertData2.pixel);    system("pause");    return 0;}

Good List

#define  _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <math.h>#include <malloc.h>#define LENCorrupetedBLKMVList sizeof(CorrupetedBLKMVList)#define AddrNull  0typedef struct motion_vector{    int mvX;    int mvY;} ercMV;// This struct means that the reconstructed MV of each 4x4 blocktypedef struct important_POS{    int Block_X;  // Here is just for P frame.     int Block_Y;            // 1 means the best suit, next 2, then 3, then 4, then 5 and other number means this mv is empty.}Position;void simpletest(ercMV Cst_mv){    printf("%d  %d\n",Cst_mv.mvX,Cst_mv.mvY);}typedef struct Corrupted_Per_Block_Ref_MV_Field_Information{    Position PPixle;    ercMV   estiMV;    int *  pixel;   // With chroma and luma }RefBLKMVInfor;// This struct is used for every corrupted block  typedef struct Corrupted_Block_MV_List{    RefBLKMVInfor  blkInfor;    struct Corrupted_Block_MV_List* next;}CorrupetedBLKMVList;/*! ************************************************************************ * \brief *      To create MV list for block in corrupted frameMV field. * \return *      Pointer of struct CorrupetedBLKMVList. * \param NONE * \2015/10/9 14:56:30 ************************************************************************ */CorrupetedBLKMVList* CreateMVFieldPerBlockMVListHead(void){    CorrupetedBLKMVList* head;       head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);    head -> next = AddrNull;    return head;}/*! ************************************************************************ * \brief *      To insert MV block node  for block in corrupted frameMV field. * \return *      Pointer head of struct CorrupetedBLKMVList. * \param NONE * \2015/10/9 14:56:30 ************************************************************************ */CorrupetedBLKMVList* InsertMVNodeIntoBlockList(CorrupetedBLKMVList* head, RefBLKMVInfor  insertData, int blockSize){    CorrupetedBLKMVList*temp = AddrNull;    int i;    if (head == AddrNull)  // first node    {        head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);        head->next = AddrNull;        head->blkInfor.PPixle.Block_X = insertData.PPixle.Block_X;        head->blkInfor.PPixle.Block_Y = insertData.PPixle.Block_Y;        head->blkInfor.estiMV.mvX = insertData.estiMV.mvX;        head->blkInfor.estiMV.mvY = insertData.estiMV.mvY;        head->blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));        for (i = 0; i < blockSize; i++)        {            head->blkInfor.pixel[i] = insertData.pixel[i];        }    }    else    {        temp = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);        if (temp == AddrNull)        {            printf("Error:In malloc memory for CorrupetedBLKMVList\n");            return;        }        temp->blkInfor.PPixle.Block_X = insertData.PPixle.Block_X;        temp->blkInfor.PPixle.Block_Y = insertData.PPixle.Block_Y;        temp->blkInfor.estiMV.mvX = insertData.estiMV.mvX;        temp->blkInfor.estiMV.mvY = insertData.estiMV.mvY;        temp->blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));        for (i = 0; i < blockSize; i++)        {            temp->blkInfor.pixel[i] = insertData.pixel[i];        }        temp->next = head;        head = temp;    }    return head;}int main(){    int i = 0;    const int blockSize = 16;    RefBLKMVInfor  testInsertData;    RefBLKMVInfor  testInsertData2;    // Create node    CorrupetedBLKMVList* test_create = AddrNull;    CorrupetedBLKMVList* search = AddrNull;    if(test_create != AddrNull)        printf("Create OK:%d \n",test_create);    //  Insert node    testInsertData.pixel = (int*)malloc(blockSize*sizeof(int));    for(i = 0; i < blockSize; i++)    {        testInsertData.pixel[i] = i;    }    testInsertData.PPixle.Block_X = 1;    testInsertData.PPixle.Block_Y = 2;    testInsertData.estiMV.mvX = 3;    testInsertData.estiMV.mvY = 5;    testInsertData2.pixel = (int*)malloc(blockSize*sizeof(int));    for (i = 0; i < blockSize; i++)    {        testInsertData2.pixel[i] = i+1;    }    testInsertData2.PPixle.Block_X = 100;    testInsertData2.PPixle.Block_Y = 21;    testInsertData2.estiMV.mvX = 23;    testInsertData2.estiMV.mvY = 15;    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData2, blockSize);    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);    search = test_create;    while (search != AddrNull)    {        printf("%d\n", search);        printf("%d\n", search->blkInfor.PPixle.Block_X);        printf("%d\n", search->blkInfor.PPixle.Block_Y);        printf("%d\n", search->blkInfor.estiMV.mvX);        printf("%d\n", search->blkInfor.estiMV.mvY);//      printf("%d\n", search->blkInfor.pixel[5]);        search = search->next;    }    search = test_create;    while (search->next != AddrNull)    {    //  free(search->blkInfor.pixel);        search = search->next;    }    free(testInsertData.pixel);     free(testInsertData2.pixel);    system("pause");    return 0;}

Malloc Two Dimension Pointer
I think the code I write is so beautiful!

    CorrupetedBLKMVList*** Pic;    Pic = (CorrupetedBLKMVList***)malloc(height*sizeof(CorrupetedBLKMVList**));    for(j = 0; j < height;j++)    {        Pic[j] = (CorrupetedBLKMVList**)malloc(width*sizeof(CorrupetedBLKMVList*));    }       for(j = 0; j < height;j++)    {        free(Pic[j]);    }    free(Pic);

Flexible Pointer Using

static void GetLumaBlock10(imgpel **block, imgpel **imgY, int blockSizeX, int blockSizeY, int XPosPixel, int YPosPixel, int maxImgpelValue){    imgpel *p0, *p1, *p2, *p3, *p4, *p5;    imgpel *orig_line, *cur_line;    int i, j;    int result;    for (j = 0; j < blockSizeY; j++)    {        cur_line = &(cur_imgY[j][XPosPixel]);        p0 = &cur_imgY[j][XPosPixel - 2];        p1 = p0 + 1;        p2 = p1 + 1;        p3 = p2 + 1;        p4 = p3 + 1;        p5 = p4 + 1;        orig_line = block[j];                    for (i = 0; i < blockSizeX; i++)        {                    result  = (*(p0++) + *(p5++)) - 5 * (*(p1++) + *(p4++)) + 20 * (*(p2++) + *(p3++));            *orig_line = (imgpel) iClip1(maxImgpelValue, ((result + 16)>>5));            *orig_line = (imgpel) ((*orig_line + *(cur_line++) + 1 ) >> 1);            orig_line++;        }    }}

Divide
‘&’ is a excellent operation. Just think that (-5) & 3 = 3, you will get the right result of U want.
‘%’ is another operation that (-5) % 4 = -1
‘a & b’ can be get by ‘b < 0? (b - abs(a%b)) : (a%b)’
I found that the operation is excellent.
(-3) >> 2 = -1 <=> (int)floor((-3.0)/4.0)
(-3)/4 = 0 <=> (int)ceil((-3.0)/4.0)

Create Files and Copy String

const char* s = "../../new3";

is the same as

#define s "../../new3"
#include <stdio.h>#include <math.h>#include <malloc.h>#include <process.h>#include <direct.h>#include <sys/stat.h>#include <string.h>static void try_create_dir(const char* dest_dir){    if (_access(dest_dir, 0) != 0)    {        _mkdir(dest_dir);    }}int main(){    const char* s = "../../new3";    char s1[100];    FILE *f1 = NULL;    try_create_dir(s);    strcpy(s1, s);     strcat(s1,"/file1.txt" );     if ((f1 = fopen(s1, "w+")) == NULL)    {        printf("Error in create log file of reconstructed MV field.\n");    }    fprintf(f1, "----------------------Successful-------------------------------------\n");    fclose(f1);    system("pause");    return 0;}

VS Skills
1、improve Ur VS running speed:

http://www.jb51.net/os/windows/73851.html
2、Condition Breakpoint:
Double click then tight click on the breakpoint and set condition:
for instance:
pxlLocation.Y == 0x110

MEMCPY
For two dynamic arrays copy by memcpy, be careful of data type.

// Y component assignment.        frame_ref->imgY_ref = (imgPel**)malloc(tempHeight * sizeof(imgPel*));        for(i = 0; i < tempHeight; i++)        {            frame_ref->imgY_ref[i]=(imgPel*)malloc(tempWidth * sizeof(imgPel));            if(refPic->imgY[i] != NULL)            {                   memcpy(frame_ref->imgY_ref[i], refPic->imgY[i], tempWidth * sizeof(imgPel));            }        }  

C-free using and debug skills

http://www.programarts.com/cfree_ch/doc/help/UsingCF/Debug.htm
http://www.cnblogs.com/lidabo/p/3631224.html

C File Operetion

http://blog.csdn.net/gneveek/article/details/6848473
http://www.cppblog.com/Tongy0/archive/2014/03/23/206305.html

C Using Skill As 默认参数

http://blog.sina.com.cn/s/blog_933d4eec0100vdk0.html

Stack Overflow

http://www.cnblogs.com/fanzhidongyzby/archive/2013/08/10/3250405.html

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机淘宝卡怎么办 淘宝联盟网址打不开怎么办 淘宝买东西卖家不退货怎么办 手机清理后微信打不开视频怎么办 搜索历史已关闭怎么办 微博重新激活怎么办 淘宝直播反应慢怎么办 微信新设备无法登录怎么办 dnf自动连接失败怎么办 APP注册没有成功怎么办 忘记绑定微信号怎么办 淘宝钻石绣被骗怎么办 safari出现闪退怎么办 12123手机号被占用怎么办 12306换手机了怎么办 51串口打开失败怎么办 打开com串口失败怎么办 xp串口打开失败怎么办 台式电脑没光驱怎么办 相机功能用不了怎么办 支付宝登录失败怎么办 淘宝号限制登陆怎么办 海信电视看不了怎么办 淘宝不记得密码怎么办 淘宝号忘记了怎么办 号码注销支付宝怎么办 旺旺号限制登录怎么办 淘宝单被监控了怎么办 晚上手机网速慢怎么办 卖家淘金币怎么办 淘宝不能下单怎么办 淘宝店铺失效了怎么办 淘宝订单没货怎么办 拼多多预售到期怎么办 直通车出价太高怎么办 htc手机黑屏打不开怎么办 商品被屏蔽该怎么办 遇到恶意差评怎么办 银行账户被锁定怎么办 淘宝卖家说退货不全怎么办 买家旺旺被限制怎么办