Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Sunday, September 2, 2018

Memory leak check command

valgrind --leak-check=full --show-leak-kinds=all --verbose opp_run -u Cmdenv -c uptp-2STAs-11ax-Udp-BoraFw-TPC -n ../..:../../../src -l ../../../src/inet biTp_fwcode_udp_tpc.ini -r 2 &> ul_twoSta_tpc_debug2.txt

Friday, July 4, 2014

如何快速打印出記憶體內容 for C programming


       
char *buf;
...
for (i=0; i<buf_size; i++)
    printf("%02x ", buf[i] & 0xff);
 

or

       
unsigned char *buf;
...
for (i=0; i<buf_size; i++)
    printf("%02x ", buf [i]);
 

Sunday, June 8, 2014

Tuesday, April 29, 2014

Source Insight 使用注意事項

除了一開始建project外
打開發現找不到symbol不能trace code的主要原因
應該是.......

沒有 Synchronize Files...    (Alt + Shift +S)

切記啊啊啊!!!

Tuesday, April 1, 2014

上高斯(ceiling function) in C programming

假設有一可變大小的封包(packet),會被MAC切成固定48 bytes大小的訊框(frame),如何計算這個封包需要多少個訊框來裝載呢?

答案 :

可以運用int的特性,以兩個極端的例子來說 packet_size #1=96 bytes, packet_size #2=97bytes
依照判斷pkt #1是切成2個訊框,pkt #2是切成3個訊框,應該怎麼寫呢?

C語言寫法如下:
    frame_number = (int) (packet_size + 47)/48
即可得到上面所需的答案囉!!

另外,也可以把它寫成函數的方式來呈現
#define PKT_SIZE 48 /* the maximum packet size */ 

/* calculate the number of packets */
int cal_pkt_number(int packet_size)
{
    return (int) (packet_size+PKT_SIZE-1)/PKT_SIZE
}

int main(void)
{
    int curr_pkt_size = 47;
    int pkt_no;
    
    pkt_no = cal_pkt_number(curr_pkt_size);
    printf("The number of packets is %d\n", pkt_no);
}
    

Thursday, March 20, 2014

清空記憶體的函數

memset(elems, 0, sizeof(*elems));

bzero(buffer, sizeof(buffer));

Wednesday, March 19, 2014

一次註解數行程式碼in Vim

你可能在有在Vim寫程式的習慣,打開一隻程式如下,妳可能會有多行需要註解的需要



可以透過以下操作來達成


  • 加入多行註解
  1. 為了確定要加入註解在某些行數,我們要先打開行數的顯示,請打
    :set nu


  2. 接著確定要註解的行數,舉例是要註解108到113行的話,可以輸入以下指令
    :108,113s/^/\/\/
  3. 完成結果如下,不難吧!

  • 刪除多行註解
  1. 剛剛產生了多行註解,如果想要一次消除,可以輸入以下指令
    :108,113s/\/\///


 有沒有很方便:)



參考文件:

Tuesday, March 18, 2014

如何在C語言中用signal()函數偵測鍵盤的訊號

不知道怎麼偵測鍵盤的訊號嗎?
可以參考以下程式碼唷!

#include <stdio.h>       
#include <signal.h>

void handler1(int sig) {
    printf(" Your input is Ctrl + C\n");
    signal(SIGINT, SIG_DFL);
}

void handler2(int sig) {
    printf(" Your input is Ctrl + Z\n");
    signal(SIGTSTP, SIG_DFL);
}

void main(void)
{
    /* I/O detect */
    signal(SIGINT, handler1);
    signal(SIGTSTP, handler2);

    while (1) {
        printf("waiting ...\n");
        sleep(5);
    }
}
 
執行結果:






參考資料:
http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_21.html

Thursday, February 10, 2011

Linux 下寫程式囉~

以工科背景的傻孩子而言
C/C++是我寫程式最好的夥伴
在Linux下寫完程式需要編譯
可能就會用到下列兩行指令

在終端機(terminal)模式下,依照程式碼類型分別下:

For C code:
       
gcc example.c -o c_result
./c_result
 



For C++ code:
       
gcc example.cpp -o cpp_result
./cpp_result