Glib介紹

GLib是一種底層庫,創建GDK和GTK應用程序時該庫提供許多有用的定義和函數。
包括基本類型及限制的定義、標準宏、類型轉化、字節序、存儲分配、警告和斷言、消息記錄、計時器、字符串工具、hook函數、句法掃描器、動態加載模塊和字符串自動補全,同時也提供了許多數據類型及相關操作。

包括存儲塊、雙向鏈表、單向鏈表、哈希表、動態列表、關系和元組及緩存。最后GLib具有很好的移植性,所以使用GLib作為底層應用支持,那么也保證了應用的可移植性。


類型定義:

1. 整數類型:gint8、guint8、gint16、guint16、gint32、guint32、gint64、guint64。不是所有的平臺都提供64位整型

2. 整數類型gshort、glong、gint和short、long、int相同

3. 布爾類型gboolean:gboolean可以取兩個值:TRUE和FALSE

4. 字符型gchar和char相同

5. 浮點型gfloat和gdouble和float、double完全等價

6. 指針gpointer對應于標準C的void*

7. gconstpointer對于于標準C的const void*

?

glib宏:

整型與指針類型間的轉換

1. GINT_TO_POINTER(a):將int型轉換成gpointer類型

2. GPOINTER_TO_INT(a):將gpointer類型轉換成int型

3. GUINT_TO_POINTER(a):將uint類型轉換成gpointer類型

4. GPOINTER_TO_UINT(a):將gpointer類型轉換成整型

5. NULL宏的定義:#define NULL (void*)0(也就是說:0是一個整型數據,而NULL則是指針類型)


一、雙向鏈表
雙向鏈表中每個元素都包含一塊數據和指向前后元素的指針。這使得鏈表的雙向移動變的容易。
存儲的數據類型是gpointer,在GLib中,gpointer指向實際數據的指針。
不存在用于創建鏈表的函數,而是簡單的創建一個Glist* 變量,并設置它為NULL。

雙向鏈表中提供的Glib函數:

[cpp]?view plaincopy
  1. GList?*g_list_append(GList?*list,?gpointer?data):將一個新元素加入到鏈表尾????
  2. GList?*g_list_prepend(GList?*list,?gpointer?data):將一個新元素加入到鏈表頭????
  3. GList?*g_list_insert(GList?*list,?gpointer?data,?gint?position):插入一個新元素到鏈表的指定位置????
  4. GList?*g_list_remove(GList?*list,?gpointer?data):從鏈表中移除一個具有值data的元素,如果元素不存在,則鏈表不變????
  5. GList?*g_list_free(GList?*list):數釋放由GList使用的所有存儲區????
  6. GList?*g_list_remove_link(GList?*list,?GList?*link)????
  7. GList?*g_list_reverse(GList?*list):鏈表元素位置反轉????
  8. GList?*g_list_nth(GList?*list,?gint?n):獲取指定位置元素????
  9. GList?*g_list_find(GList?*list,?gpointer?data):在鏈表中查找一個含有指定值的元素,沒有則返回NULL????
  10. GList?*g_list_last(GList?*list):獲取鏈表中最后一個元素????
  11. GList?*g_list_first(GList?*list):獲取鏈表中第一個元素????
  12. gint?g_list_length(GList?*list):返回鏈表元素個數????
  13. void?g_list_foreach(GList?*list,?GFunc?func,?gpointer?data):遍歷鏈表????
  14. gint?g_list_index(GList?*list,?gconstpointer?data):返回指定元素在鏈表中的位置,沒有找到匹配的元素,則返回-1。元素位置從0開始計算。??

[cpp]?view plaincopy
  1. #include?<stdio.h>??
  2. #include?<stdlib.h>??
  3. #include?<glib.h>??
  4. #include?<string.h>??
  5. ??
  6. ??
  7. typedef?struct?_Teacher??
  8. {??
  9. ????gint?age;??
  10. ????gchar?*name;??
  11. }Teacher;??
  12. ??
  13. ??
  14. ??
  15. void?each_callback(gpointer?data,?gpointer?user_data)??
  16. {??
  17. ????Teacher?*t?=?(Teacher?*)data;??
  18. ????g_print("t->name?=?%s,?user?param:%s\n",?t->name,?(char?*)user_data);??
  19. }????
  20. ??
  21. ???
  22. int?main(?int?argc,char?*argv[]?)??
  23. {????
  24. ????GList?*list?=?NULL;??
  25. ????Teacher?*pTeacher1?=?g_new0(Teacher,1);??
  26. ????pTeacher1->name?=?g_new0(char,128);??
  27. ????strcpy(pTeacher1->name,"tiny?Jason");??
  28. ????list?=?g_list_append(list,?pTeacher1);??
  29. ??????
  30. ????Teacher?*pTeacher2?=?g_new0(Teacher,1);??
  31. ????pTeacher2->name?=?g_new0(char,128);??
  32. ????strcpy(pTeacher2->name,"Rorash");??
  33. ????list?=?g_list_prepend(list,?pTeacher2);????
  34. ??
  35. ??
  36. ????Teacher?*pTeacher3?=?g_new0(Teacher,1);??
  37. ????pTeacher3->name?=?g_new0(char,128);??
  38. ????strcpy(pTeacher3->name,"alibaba");??
  39. ????list?=?g_list_prepend(list,?pTeacher3);???
  40. ??????
  41. ????g_list_foreach(list,?each_callback,?"user_data");????
  42. ??
  43. ????GList?*second?=?g_list_find(list,?pTeacher2);??
  44. ????if(second?!=?NULL)??
  45. ????{??
  46. ????????Teacher*?t?=?(Teacher*)second->data;??
  47. ????????g_print("name?:%s\n",t->name);??
  48. ????}??
  49. ??
  50. ??
  51. ????list?=?g_list_remove(list,?pTeacher2);??
  52. ??????
  53. ????g_list_foreach(list,?each_callback,?"user_data");????
  54. ????
  55. ????gint?len?=?g_list_length(list);??
  56. ????g_print("len?:%d\n",len);??
  57. ??????
  58. ????GList?*nNode?=?g_list_nth(list,1);??
  59. ????if(nNode?!=?NULL)??
  60. ????{??
  61. ????????Teacher*?t?=?(Teacher*)nNode->data;??
  62. ????????g_print("name?:%s\n",t->name);??
  63. ????}??
  64. ??????
  65. ????g_list_free(list);??
  66. ?????
  67. ??return?0;????
  68. }????

makefile:

[cpp]?view plaincopy
  1. .SUFFIXES:.c?.o??
  2. ??
  3. CC=gcc??
  4. SRCS=test_glib.c??
  5. OBJS=$(SRCS:.c=.o)??
  6. EXEC=test_glib??
  7. ??
  8. all:$(OBJS)??
  9. ????????$(CC)?-o?$(EXEC)?$(OBJS)?`pkg-config?--libs?glib-2.0`??
  10. ??
  11. .c.o:??
  12. ????????$(CC)?-o?$@?-c?-g?$<?`pkg-config?--cflags?glib-2.0`??
  13. clean:??
  14. ????????rm?-f?$(OBJS)??



運行結果:

[cpp]?view plaincopy
  1. t->name?=?alibaba,?user?param:user_data??
  2. t->name?=?Rorash,?user?param:user_data??
  3. t->name?=?tiny?Jason,?user?param:user_data??
  4. name?:Rorash??
  5. t->name?=?alibaba,?user?param:user_data??
  6. t->name?=?tiny?Jason,?user?param:user_data??
  7. len?:2??
  8. name?:tiny?Jason??



二、單向鏈表
[cpp]?view plain?copy
  1. GSList?*g_slist_append(GSList?*list,?gpointer?data):鏈表最后新增一個元素??
  2. GSList?*g_slist_prepend(GSList?*list,?gpointer?data):鏈表最前面新增一個元素??
  3. GSList?*g_slist_insert(GSList?*list,?gpointer?data,?gint?position):指定鏈表位置插入新元素??
  4. GSList?*g_slist_remove(GSList?*list,?gpointer?data):鏈表中刪除具有值data的元素??
  5. GSList?*g_slist_reverse(GSList?*list):反轉元素位置??
  6. GSList?*g_slist_nth(GSList?*list,?gint?n):返回鏈表中下一個元素??
  7. GSList?*g_slist_find(GSList?*list,?gpointer?data):查找指定data的元素,沒有則返回NULL??
  8. GSList?*g_slist_last(GSList?*list):查找鏈表的最后一個元素??
  9. gint?g_slist_length(GSList?*list):返回鏈表元素個數??
  10. void?g_slist_foreach(GSList?*list,?GFunc?func,?gpointer?data):遍歷鏈表??

三、存儲管理
[cpp]?view plain?copy
  1. gpointer?g_malloc(gulong?size):這是malloc的替代函數,不需要檢查返回值。如果存儲分配因任何原因失敗,則應用程序終止。??
  2. gpointer?g_malloc0(gulong?size):和g_malloc具有相同功能,但在返回指向分配存儲塊的指針前,將該存儲塊清0。??
  3. gpointer?g_realloc(gpointer?mem,?gulong?size):重新分配由mem開始的指針,并設置大小為size字節。??
  4. void?g_free(gpointer?mem):釋放分配的存儲塊。如果mem為NULL,則直接返回。??

四、計時器

計時器函數可用于記錄操作記時,也可以記錄程序的間斷運行時間。

[cpp]?view plain?copy
  1. GTimer?*g_timer_new(void):創建一個新計時器??
  2. void?g_timer_destroy(GTimer?*timer):注銷計時器??
  3. void?g_timer_start(GTimer?*timer):計時器開始??
  4. void?g_timer_stop(GTimer?*timer):停止計時??
  5. void?g_timer_reset(GTimer?*timer):重置計時器??
  6. void?g_timer_continue(GTimer?*timer):繼續計時??
  7. gdobule?g_timer_elapsed(GTimer?*timer,?gulong?*microseconds):決定所耗時間??

Example:
[cpp]?view plain?copy
  1. GTimer?*timer;??
  2. ??
  3. void?each_callback(gpointer?data,?gpointer?user_data)??
  4. {??
  5. ????g_print("element:%s,?user?param:%s\n",?(gchar*)data,?(gchar*)user_data);??
  6. }??
  7. ??
  8. int?main(?int?argc,??
  9. ??????????char?*argv[]?)??
  10. {??
  11. ??GList?*list?=?NULL;??
  12. ??gulong?seconds;??
  13. ??int?i=0;??
  14. ??timer?=?g_timer_new();??
  15. ??
  16. ??list?=?g_list_append(list,?"second");??
  17. ??list?=?g_list_prepend(list,?"first");??
  18. ??
  19. ??g_timer_start(timer);??
  20. ??g_list_foreach(list,?each_callback,?"user_data");??
  21. ??g_timer_stop(timer);??
  22. ??
  23. ??g_timer_elapsed(timer,?&seconds);??
  24. ??
  25. ??g_print("use?seconds:%ld\n",?seconds);??
  26. ??
  27. ??g_timer_continue(timer);??
  28. ??
  29. ??for(i;?i<=1000;?i++)??
  30. ??{??
  31. ??????g_print("%d",?i);??
  32. ??}??
  33. ??g_timer_elapsed(timer,?&seconds);??
  34. ??g_print("use?seconds:%ld\n",?seconds);??
  35. ??return?0;??
  36. }??

五、字符串處理

編程中經常需要對字符串進行拼接、截取、大小寫轉換,原本在C中這些操作是非常繁瑣的。現在GLib定義了一個叫做GString的新類型,它可以自動增長,并且提供了
一系列方便的操作函數。
struct GString{
gchar *str;/*指向當前以\0結尾的字符串*/
gint len;/*當前字符長度*/
}

[cpp]?view plain?copy
  1. GString?*g_string_new(gchar?*init):創建GList類型??
  2. GString?*g_string_truncate(GString?*string,?gint?len):截取指定長度的字符串??
  3. GString?*g_string_append(GString?*string,?gchar?*val):末尾追加字符串??
  4. GString?*g_string_append_c(GString?*string,?gchar?c):末尾最加單個字符??
  5. GString?*g_string_prepend(GString?*string,?gchar?*val):開頭插入字符串??
  6. GString?*g_string_prepend_c(GString?*string,?gchar?c):開頭插入單個字符??
  7. void?g_string_sprintf(GString?*string,?gchar?*fmt,?...):格式化字符串??
  8. gchar?*g_strdup?(const?gchar?*str):復制字符串,返回一個新分配的字符串。??
  9. gchar?*g_strndup(const?gchar?*str,?gsize?n):復制指定個數的字符串,返回新分配的字符串??
  10. gchar?*g_strstr_len(const?gchar?*haystack,?gssize?haystack_len,?const?gchar?*needle):在限定長度內,第一次出現指定字符的指針??
  11. gchar?*g_strrstr(const?gchar?*haystrack,?const?gchar?*needle):搜索字符串haystack中最后一次出現的串針。??
  12. gchar?*g_strrstr_len(const?gchar?*haystrack,?gssize?haystrack_len,?const?gchar?*needle)??
  13. gboolean?g_str_hash_prefix(const?gchar?*str,?const?gchar?*prefix):返回字符串是否以某個前綴開頭??
  14. int?g_strcmp0(const?char?*str1,?const?char?*str2):對比兩個字符串??
  15. gchar?**g_strsplit(const?gchar?*string,?const?gchar?*delimiter,?gint?max_tokens):分割字符串,保存為數組??
  16. gchar?*g_strconcat(const?gchar?*string1,?...):字符串拼接??
  17. gchar?*g_strjoin(const?gchar?*separator,?...):以某個字符串隔離并拼接??

更多 http://gtk-doc-cn.googlecode.com/svn/docs/glib/glib-String-Utility-Functions.html#g-strdup


六、錯誤處理

[cpp]?view plain?copy
  1. gchar?*g_strdup(?const?gchar?*str?):替代strdup函數。把原字符串內容復制到新分配的存儲塊中,返回指向它的指針。??
  2. gchar?*g_strerror(?gint?errnum?);??
  3. void?g_error(?gchar?*format,?...?);錯誤提示:“?**?ERROR?**?”并且退出程序。僅用在致命錯誤上。??
  4. void?g_warning(?gchar?*format,?...?):錯誤提示:“?**?WARNING?**?”??
  5. void?g_message(?gchar?*format,?...?):在傳遞字符串前打印"message"??
  6. void?g_print(?gchar?*format,?...?):替代printf函數??


除了上述之外,GLib還提供了很多功能,包括編碼轉換、正則、XMP解析、Test框架等等。


glib about thread,mutex,cond


[cpp]?view plaincopy
  1. #include?<glib.h>??
  2. #include?<stdio.h>??
  3. ??
  4. static?int?num?=?0;??
  5. GMutex?mutex;??
  6. GCond?cond;??
  7. ??
  8. gboolean?_thread_main1(void?*data)??
  9. {??
  10. ????while(1)??
  11. ????{??
  12. ??????????g_mutex_lock(&mutex);??
  13. ????????while(num?<=?0)??
  14. ????????{??
  15. ????????????g_printf("consumer[%d]?is?wating.....\n",(int)data);??
  16. ????????????g_cond_wait(&cond,?&mutex?);??
  17. ????????????g_printf("consumer[%d]?wake?up.....\n",(int)data);??
  18. ????????}??
  19. ????????g_printf("consmuer[%d]?before,num?=?%d.....\n",(int)data,num);??
  20. ????????num--;??
  21. ????????g_printf("consmuer[%d]?after,num?=?%d.....\n",(int)data,num);??
  22. ????????g_mutex_unlock(&mutex);??
  23. ????????sleep(1);??
  24. ????}??
  25. ????return?TRUE;??
  26. }??
  27. ??
  28. ??
  29. gboolean?_thread_main2(void?*data)??
  30. {??
  31. ????while(1)??
  32. ????{??
  33. ??????????g_mutex_lock(&mutex);??
  34. ????????num++;??
  35. ????????if(num?>?0)??
  36. ????????{??
  37. ????????????g_printf("prepare?to?sigal...please?wait?for?5?seconds\n");??
  38. ????????????sleep(5);??
  39. ????????????g_cond_signal(&cond);??
  40. ????????????g_printf("after?g_cond_signal\n");??
  41. ????????}??
  42. ????????g_mutex_unlock(&mutex);??
  43. ????????sleep(2);??
  44. ????}??
  45. ????return?TRUE;??
  46. }??
  47. ??
  48. ??
  49. int?main(?int?argc,char?*argv[]?)??
  50. {????
  51. ????GThread?*consumer1?=?NULL;??
  52. ????GThread?*consumer2?=?NULL;??
  53. ????GThread?*consumer3?=?NULL;??
  54. ??????
  55. ????GThread?*thread2?=?NULL;??
  56. ??????
  57. ??????
  58. ????g_mutex_init(&mutex);??
  59. ????g_cond_init(?&cond?);??
  60. ??????
  61. ????consumer1?=?g_thread_new("consumer1",?(GThreadFunc)_thread_main1,?(void*)1);??
  62. ????consumer2?=?g_thread_new("consumer2",?(GThreadFunc)_thread_main1,?(void*)2);??
  63. ????consumer3?=?g_thread_new("consumer3",?(GThreadFunc)_thread_main1,?(void*)3);??
  64. ??????
  65. ????thread2?=?g_thread_new("thread2",?(GThreadFunc)_thread_main2,?NULL);??
  66. ??????
  67. ??????
  68. ????g_thread_join?(consumer1);??
  69. ????g_thread_join?(consumer2);??
  70. ????g_thread_join?(consumer3);??
  71. ??????
  72. ??????
  73. ????g_thread_join?(thread2);??
  74. ????return?0;????
  75. }????
  76. ?????

結果:

consumer[1] is wating.....
consumer[2] is wating.....
prepare to sigal...please wait for 5 seconds
after g_cond_signal
consumer[1] wake up.....
consmuer[1] before,num = 1.....
consmuer[1] after,num = 0.....
consumer[3] is wating.....
consumer[1] is wating.....
prepare to sigal...please wait for 5 seconds
after g_cond_signal
consumer[2] wake up.....
consmuer[2] before,num = 1.....
consmuer[2] after,num = 0.....
consumer[2] is wating.....
prepare to sigal...please wait for 5 seconds
after g_cond_signal
consumer[3] wake up.....
consmuer[3] before,num = 1.....
consmuer[3] after,num = 0.....
consumer[3] is wating.....
prepare to sigal...please wait for 5 seconds
after g_cond_signal
consumer[1] wake up.....
consmuer[1] before,num = 1.....
consmuer[1] after,num = 0.....
consumer[1] is wating.....
prepare to sigal...please wait for 5 seconds
after g_cond_signal
consumer[2] wake up.....
consmuer[2] before,num = 1.....
consmuer[2] after,num = 0.....
consumer[2] is wating.....

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385060.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385060.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385060.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

11-散列3 QQ帳戶的申請與登陸 (25 分)

實現QQ新帳戶申請和老帳戶登陸的簡化版功能。最大挑戰是&#xff1a;據說現在的QQ號碼已經有10位數了。 輸入格式: 輸入首先給出一個正整數N&#xff08;≤&#xff09;&#xff0c;隨后給出N行指令。每行指令的格式為&#xff1a;“命令符&#xff08;空格&#xff09;QQ號碼&…

glib字符串

學過面向對象語言的同學一定都知道String類&#xff0c;一定知道這個類對字符串的操作是多麼的方便&#xff0c;但是c語言中是沒有這個類&#xff0c;甚至沒有類的概念&#xff0c;但是glib幫我們做的這個“類” GString 除了使用gchar *進行字符串處理以外&#xff0c;Glib還…

KMP 串的模式匹配 (25 分)

給定兩個由英文字母組成的字符串 String 和 Pattern&#xff0c;要求找到 Pattern 在 String 中第一次出現的位置&#xff0c;并將此位置后的 String 的子串輸出。如果找不到&#xff0c;則輸出“Not Found”。 本題旨在測試各種不同的匹配算法在各種數據情況下的表現。各組測試…

命令行工具tshark使用小記

1、目的 寫這篇博客的目的主要是為了方便查閱&#xff0c;使用wireshark可以分析數據包&#xff0c;可以通過編輯過濾表達式來達到對數據的分析&#xff1b;但我的需求是&#xff0c;怎么樣把Data部分導出來&#xff0c;因為后續的工作主要針對數據包的Data部分&#xff0c;主要…

winshark重要數據結構

說起來有一些慚愧&#xff0c;研究wireshark有一段時間了&#xff0c;但是對源代碼的分析卻至今沒有什么進展。。。 最初想要研究wireshark是因為我的開題是基于wireshark來做的。 現在有很多抓包工具&#xff0c;wireshark的優勢在于完全開源&#xff0c;分析功能強大&#xf…

C語言寫數據庫(二)

簡單的實現增刪查改的操作后&#xff0c;實現了一個先讀寫其中一個表的某兩項內容&#xff0c;再把相關字符段寫入到另外一張表中去。涉及到查詢和插入兩個步驟。 其中還涉及到漢字的讀寫和插入&#xff0c;會有字符的操作產生亂碼。所以要先保證mysql的漢字字符編碼&#xff0…

wireshark源代碼分析

各位親&#xff0c;不是我不想回復你們的問題。是我也不了解。不能誤導。希望大家相互幫助。看看能否幫那些提問的小盆友們回復一下呢&#xff1f; 這些都是轉載的&#xff0c;如果實在沒有辦法&#xff0c;可以打開鏈接到原作者哪里去提問試試看。。。 經過多次嘗試&#xf…

C語言寫數據庫(三)

遇到的問題以及解決思路方法 1.外部導入數據庫文件 進入mysql&#xff0c;創建數據庫sh_robot source /home/exbot/sh_robot.sql 查看數據庫編碼格式 show variables like “%char%”; 2.數據庫插入操作 進入相關數據庫&#xff1a;use 數據庫名&#xff1b; 查詢存在該表是否存…

Makefile(一)

在一個文件夾中建一個c文件 //main.c #include<stdio.h> int main() {printf("version 1.0");return 0; } 在當前目錄下編寫makefile文件 //makefile: test : main.o //一種依賴關系聲明&#xff0c;生成test可執行程序需要以來main.o文件gcc -o test main.…

Defunct進程 僵尸進程

在測試基于 DirectFBGstreamer 的視頻聯播系統的一個 Demo 的時候&#xff0c;其中大量使用 system 調用的語句&#xff0c;例如在 menu 代碼中的 system("./play") &#xff0c;而且多次執行&#xff0c;這種情況下&#xff0c;在 ps -ef 列表中出現了大量的 defunc…

make文件基礎用法

參照&#xff1a;https://www.jianshu.com/p/0b2a7cb9a469 創建工作目錄&#xff0c;包含一下文件 main.cperson.cb.hc.h/*** c.h ***/ //this is c.h /*** b.h ***/ //this is b.h /*** main.c ***/ #include<stdio.h> //#include"a1.h" //#include"b.h&…

一個Linux下C線程池的實現(轉)

1.線程池基本原理 在傳統服務器結構中, 常是 有一個總的 監聽線程監聽有沒有新的用戶連接服務器, 每當有一個新的 用戶進入, 服務器就開啟一個新的線程用戶處理這 個用戶的數據包。這個線程只服務于這個用戶 , 當 用戶與服務器端關閉連接以后, 服務器端銷毀這個線程。然而頻繁地…

二維數組作為函數參數

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

mystrcat

#include<stdio.h> //如果一個數組做為函數的形參傳遞&#xff0c;那么數組可以在被調用的函數中修改 //有時候不希望這個事發生&#xff0c;所以對形參采用const參數 //size_t strlen(const char *s); //strcpy(char* s1,const char* s2); void mystrcat(char *s1,cons…

關于非阻塞的recv的時候返回的處理

注意recv&#xff08;&#xff09;如果讀到數據為0&#xff0c;那么就表示文件結束了&#xff0c;如果在讀的過程中遇到了中斷那么會返回-1&#xff0c;同時置errno為EINTR。 因此判斷recv的條件&#xff1a; 如果read返回<0 如果0 表示文件結束&…

帶參程序

windows環境 #include<stdio.h>int main(int argc, char *argv[]) {printf("argc %d\n", argc);for (int i 0; i < argc; i){printf("argv[%d] %s\n",i, argv[i]);}system("pause");return 0; } windows環境下&#xff0c;帶參函數…

Ubuntu安裝mysql步驟

1.打開終端&#xff0c;輸入&#xff1a; sudo apt-get updata 輸入root用戶密碼 2.更新完畢后&#xff0c;輸入 sudo apt-get install mysql-server ubuntu14.04安裝中間會讓你設置密碼&#xff0c;輸入密碼后點擊確認(mysql123) 3.安裝結束后&#xff0c;查看端口號是否開啟 …

Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候&#xff0c;無意中看到這樣一句話(man pthread_detach): Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. …

二維數組求平均值(指針的使用)

#include<stdio.h>int main() {int buf[3][5] {{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int j;//求行平均值 for(i 0; i < 3; i){int sum 0;for(j 0; j < 5; j){sum (*(*(buf i) j));}printf("sum %d\n",sum/5);}//求列平均值for(i 0; i …

linux終端關閉時為什么會導致在其上啟動的進程退出?

現象 經常在linux下開發的人應該都有這樣的經驗&#xff0c;就是在終端上啟動的程序&#xff0c;在關閉終端時&#xff0c;這個程序的進程也被一起關閉了。看下面這個程序&#xff0c;為了使進程永遠運行&#xff0c;在輸出helloworld后&#xff0c;循環調用sleep&#xff1a; …