Linux時間函數札記

關于gmtimegmtime_rlocaltimelocaltime_r


測試環境:vmware?7?+?Redhat5.5,系統時間使用UTC,時區為上海。

?1、函數功能介紹

??????? 使用man?gmtimeman?localtime都可以的得到這幾個函數的介紹。原型如下:

??????? struct?tm?*gmtime(const?time_t?*timep);

??????? struct?tm?*gmtime_r(const?time_t?*timep,?struct?tm?*result);

??????? struct?tm?*localtime(const?time_t?*timep);

??????? struct?tm?*localtime_r(const?time_t?*timep,?struct?tm?*result);

man手冊中對它們的解釋如下:

??????? The?gmtime()?function?converts?the?calendar?time?timep?to?broken-down?time?representation,?expressed?in?Coordinated?Universal?Time?(UTC).?It?may?return?NULL?when?the?year?does?not?fit?into?an?integer.?The?return?value?points?to?a?statically?allocated?struct?which?might?be?overwritten??by?subsequent?calls?to?any?of?the?date?and?time?functions.?The?gmtime_r()?function?does?the?same,?but?stores?the?data?in?a?user-supplied?struct.

??????? The?localtime()?function?converts?the?calendar?time?timep?to?broken-time?representation,?expressed?relative?to?the?user's?specified?time?zone.?The?function?acts?as?if?it?called?tzset(3)?and?sets?the?external?variables?tzname?with?information?about?the?current?time?zone,?timezone?with??the?difference?between?Coordinated?Universal?Time?(UTC)?and?local?standard?time?in?seconds,?and?daylight?to?a?non-zero?value?if?daylight?savings?time?rules?apply?during?some?part?of?the?year.??The?return?value?points?to?a?statically?allocated?struct?which?might?be?overwritten?by?subsequent?calls?to?any?of?the?date?and?time?functions.?The?localtime_r()?function?does?the?same,?but?stores??the?data?in?a?user-supplied?struct.?It?need?not?set?tzname

翻譯如下:

??????? gmtime()?函數將日歷時間timep轉換為用UTC時間表示的時間。它可能返回NULL,比如年份不能放到一個整數中。返回值指向一個靜態分配的結構,該結構可能會被接下來的任何日期和時間函數調用覆蓋。gmtime_r()函數功能與此相同,但是它可以將數據存儲到用戶提供的結構體中。

??????? localtime()?函數將日歷時間timep轉換為用戶指定的時區的時間。這個函數的行為好像是它調用了tzset(3)?并且將外部變量tzname設置為當前時區的信息,將timezone設為UTC和本地標準時間的差值,并且,如果在一年的部分時間使用日光節約規則時將daylight設置為非空值。返回值指向一個靜態分配的結構,該結構可能會被接下來的任何日期和時間函數調用覆蓋。localtime_r()函數功能與此相同,但是它可以將數據存儲到用戶提供的結構體中。它不需要設置tzname

?

2、功能測試

程序一:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?utc_tm;;

if(?NULL?==?gmtime_r(?&cur_time,?&utc_tm?)?)

{

perror("gmtime"?);

return?-1;

}

?

struct?tm?local_tm;

if(?NULL?==?localtime_r(?&cur_time,?&local_tm?)?)

{

perror("localtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(&utc_tm)?);

printf("LOC?=?%s",?asctime(&local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:16:10?2011

LOC?=?Thu?Oct?27?17:16:10?2011

LOC?=?Thu?Oct?27?17:16:10?2011

由于系統時間使用了UTC,可以看到本地時間= UTC時間?+?8”,輸出正確。

?

程序二:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:20:45?2011

LOC?=?Thu?Oct?27?17:20:45?2011

LOC?=?Thu?Oct?27?17:20:45?2011

同樣是正確的。

?

程序三:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?17:21:59?2011

LOC?=?Thu?Oct?27?17:21:59?2011

LOC?=?Thu?Oct?27?17:21:59?2011

這程序輸出有錯,UTC時間和本地時間相同了,這應該就是由于man文檔中描述的可能會被接下來的任何日期和時間函數調用覆蓋造成的。為驗證這個設想,使用程序四:

?

程序四:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:24:23?2011

LOC?=?Thu?Oct?27?09:24:23?2011

LOC?=?Thu?Oct?27?17:24:23?2011

驗證了該設想。

?

3、總結

??????? 使用gmtimelocaltime后要立即處理結果,否則返回的指針指向的內容可能會被覆蓋,一個好的方法是使用gmtime_rlocaltime_r,由于使用了用戶分配的內存,這兩個函數是不會出錯的。





Linux時間函數

分類: Linux C編程2012-04-28 22:48 22366人閱讀 評論(6) 收藏 舉報

linuxstructnulltimezonetimer

系統環境:ubuntu10.04


簡介

本文旨在為了解Linux各種時間類型與時間函數提供技術文檔。


1Linux下常用時間類型

Linux下常用時間類型有四種:time_tstruct tmstruct timevalstruct timespec


1.1 time_t時間類型

time_t類型在time.h中定義:

  1. #ifndef __TIME_T ?
  2. #define __TIME_T ?
  3. typedef? long? time_t; ?
  4. #endif ?

可見,time_t實際是一個長整型。其值表示為從UTC(coordinated universal time)時間197011000000(也稱為Linux系統的Epoch時間)到當前時刻的秒數。由于time_t類型長度的限制,它所表示的時間不能晚于2038119031407(UTC)。為了能夠表示更久遠的時間,可用64位或更長的整形數來保存日歷時間,這里不作詳述。

使用time()函數獲取當前時間的time_t值,使用ctime()函數將time_t轉為當地時間字符串。

備注UTC時間有時也稱為GMT時間,其實UTCGMT兩者幾乎是同一概念。它們都是指格林尼治標準時間,只不過UTC的稱呼更為正式一點。兩者區別在于前者是天文上的概念,而后者是基于一個原子鐘。


1.2 struct tm時間類型

tm結構在time.h中定義:

  1. #ifndef _TM_DEFINED ?
  2. struct tm{ ?
  3. ? ? int tm_sec; /* - 取值區間為[0, 59]*/ ?
  4. ? ? int tm_min; /* - 取值區間為[0, 59]*/ ?
  5. ? ? int tm_hour; /* - 取值區間為[0, 23]*/ ?
  6. ? ? int tm_mday; /* - 取值區間為[1, 31]*/ ?
  7. ? ? int tm_mon; /*月份 - 取值區間為[0, 11]*/ ?
  8. ? ? int tm_year; /*年份 - 其值為1900年至今年數*/ ?
  9. ? ? int tm_wday; /*星期 - 取值區間[0, 6]0代表星期天,1代表星期1,以此類推*/ ?
  10. ? ? int tm_yday; /*從每年的11日開始的天數-取值區間為[0, 365]0代表11*/ ?
  11. ? ? int tm_isdst; /*夏令時標識符,使用夏令時,tm_isdst為正,不使用夏令時,tm_isdst0,不了解情況時,tm_isdst為負*/ ?
  12. }; ?
  13. #define _TM_DEFINED ?
  14. #endif ?

ANSI C標準稱使用tm結構的這種時間表示為分解時間(broken-down time)

使用gmtime( )localtime( )可將time_t時間類型轉換為tm結構體;

使用mktime( )tm結構體轉換為time_t時間類型;

使用asctime( )struct tm轉換為字符串形式。

?

1.3 struct timeval時間類型

timeval結構體在time.h中定義:

  1. Struct tmieval{ ?
  2. ? ? time_t tv_sec; /*s*/ ?
  3. ? ? suseconds_t tv_usec; /*微秒us*/ ?
  4. }; ?

設置時間函數settimeofday( )與獲取時間函數gettimeofday( )均使用該事件類型作為傳參。

?

1.4 struct timespec時間類型

timespec結構體在time.h定義:


  1. struct timespec{ ?
  2. ? ? time_t tv_sec; /*s*/ ?
  3. ? ? long tv_nsec; /*納秒ns*/ ?
  4. }; ?

?

2Linux下常用時間函數

Linux下常用時間函數有:time( )ctime( )gmtime( )localtime( )mktime( )asctime( )difftime( )gettimeofday( )settimeofday( )


2.1 time( )函數

頭文件:#include <time.h>

函數定義:time_t time(time_t *timer)

功能描述:該函數返回從197011000000秒至今所經過的秒數。如果time_t *timer非空指針,函數也會將返回值存到timer指針指向的內存。

返回值:成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存于errno中。

例:


  1. time_t seconds; ?
  2. seconds = time((time_t *)NULL); ?


2.2 ctime( )函數

頭文件:#include <time.h>

函數定義:char *ctime(const time_t *timep);

功能描述:ctime( )將參數timep指向的time_t時間信息轉換成實際所使用的時間日期表示方法,并以字符串形式返回。字符串格式為:"Wed Jun 20 21:00:00 2012\n"

例:

  1. time_t timep; ?
  2. tmep = time(NULL); ?
  3. printf("%s\n", ctime(&timep)); ?


2.3 gmtime( )函數

頭文件:#include <time.h>

函數定義:struct tm *gmtime(const time_t *timep)

功能描述:gmtime( )將參數timep指向的time_t時間信息轉換成以tm結構體表示的GMT時間信息,并以struct tm*指針返回。

GMTGMT是中央時區,北京在東8,相差8個小時,所以北京時間=GMT時間+8小時。

例:

  1. int main(void) ?
  2. { ?
  3. ? ? char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ?
  4. ? ? time_t timep; ?
  5. ? ? struct tm *p_tm; ?
  6. ? ? timep = time(NULL); ?
  7. ? ? p_tm = gmtime(&timep); /*獲取GMT時間*/ ?
  8. ? ? printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday); ?
  9. ? ? printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec); ?
  10. } ?


2.4 localtime( )函數

頭文件:#include <time.h>

函數定義:struct tm *localtime(const time_t *timep);

功能描述:localtime( )將參數timep指向的time_t時間信息轉換成以tm結構體表示的本地時區時間(如北京時間= GMT+小時)

例:

  1. int main(void) ?
  2. { ?
  3. ? ? char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ?
  4. ? ? time_t timep; ?
  5. ? ? struct tm *p_tm; ?
  6. ? ? timep = time(NULL); ?
  7. ? ? p_tm = localtime(&timep); /*獲取本地時區時間*/ ?
  8. ? ? printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday); ?
  9. ? ? printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec); ?
  10. ? ? return 0; ?
  11. } ?


2.5 mktime( )函數

頭文件:#include <time.h>

函數定義:time_t mktime(struct tm *p_tm);

功能描述:mktime( )將參數p_tm指向的tm結構體數據轉換成從197011000000秒至今的GMT時間經過的秒數。

例:

  1. int main(void) ?
  2. { ?
  3. ? ? time_t timep: ?
  4. ? ? struct tm *p_tm; ?
  5. ? ? timep = time(NULL); ?
  6. ? ? pintf("time( ):%d\n", timep); ?
  7. ? ? p_tm = local(&timep); ?
  8. ? ? timep = mktime(p_tm); ?
  9. ? ? printf("time( )->localtime( )->mktime( ):%d\n", timep); ?
  10. ? ? return 0; ?
  11. } ?


2.6 asctime( )函數

頭文件:#include <time.h>

函數定義:char *asctime(const struct tm *p_tm);

功能描述:asctime( )將參數p_tm指向的tm結構體數據轉換成實際使用的時間日期表示方法,并以字符串形式返回(ctime函數相同)。字符串格式為:"Wed Jun 20 21:00:00 2012\n"

例:

  1. int main(void) ?
  2. { ?
  3. ? ? time_t timep; ?
  4. ? ? timep = time(NULL); ?
  5. ? ? printf("%s\n", asctime(gmtime(&timep))); ?
  6. ? ? return 0; ?
  7. } ?


2.7 difftime( )函數

頭文件:#include <time.h>

函數定義:double difftime(time_t timep1, time_t timep2);

功能描述:difftime( )比較參數timep1timep2時間是否相同,并返回之間相差秒數。

例:

  1. int main(void) ?
  2. { ?
  3. ? ? time_t timep1, timep2; ?
  4. ? ? timep1 = time(NULL); ?
  5. ? ? sleep(2); ?
  6. ? ? timep2 = time(NULL); ?
  7. ? ? printf("the difference is %f seconds\n", difftime(timep1, timep2)); ?
  8. ? ? return 0; ?
  9. } ?


2.8 gettimeofday( )函數

頭文件:#include <sys/time.h>

? ? ? ? #include <unistd.h>

函數定義:int gettimeofday(struct timeval *tv, struct timezone *tz);

功能描述:gettimeofday( )把目前的時間信息存入tv指向的結構體,當地時區信息則放到tz指向的結構體。

struct timezone原型:

  1. struct timezone{ ?
  2. ? ? int tz_minuteswest; /*miniutes west of Greenwich*/ ?
  3. ? ? int tz_dsttime; /*type of DST correction*/ ?
  4. }; ?

例:

  1. struct timeval tv; ?
  2. struct timeval tz; ?
  3. gettimeofday(&tv, &tz); ?


附:

使用time函數族獲取時間并輸出指定格式字符串例子(strftime( )函數):

  1. int main(void) ?
  2. { ?
  3. ? ? char strtime[20] = {0}; ?
  4. ? ? time_t timep; ?
  5. ? ? struct tm *p_tm; ?
  6. ? ? timep = time(NULL); ?
  7. ? ? p_tm = localtime(&timep); ?
  8. ? ? strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm); ?
  9. ? ? return 0; ?
  10. } ?


2.9 settimeofday( )函數

頭文件:#include <sys/time.h>

? ? ? ? #include <unistd.h>

函數定義:int settimeofday(const struct timeval *tv, const struct timezone *gz);

功能描述:settimeofday( )把當前時間設成由tv指向的結構體數據。當前地區信息則設成tz指向的結構體數據。

例:


  1. int main(void) ?
  2. { ?
  3. ? ? char t_string[] = "2012-04-28 22:30:00"; ?
  4. ? ? struct tm time_tm; ?
  5. ? ? struct timeval time_tv; ?
  6. ? ? time_t timep; ?
  7. ? ? int ret = 0; ?
  8. ?
  9. ? ? sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec); ?
  10. ? ? time_tm.tm_year -= 1900; ?
  11. ? ? time_tm.tm_mon -= 1; ?
  12. ? ? time_tm.tm_wday = 0; ?
  13. ? ? time_tm.tm_yday = 0; ?
  14. ? ? time_tm.tm_isdst = 0; ?
  15. ?
  16. ? ? timep = mktime(&time_tm); ?
  17. ? ? time_tv.tv_sec = timep; ?
  18. ? ? time_tv.tv_usec = 0; ?
  19. ?
  20. ? ? ret = settimeofday(&time_tv, NULL); ?
  21. ? ? if(ret != 0) ?
  22. ? ? { ?
  23. ? ? ? ? fprintf(stderr, "settimeofday failed\n"); ?
  24. ? ? ? ? return -1; ?
  25. ? ? } ?
  26. ? ? return 0; ?
  27. } ?

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

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

相關文章

c++實現順序表的相關操作

Myarray.h文件 #pragma once#include<iostream>using namespace std;class MyArray { public:MyArray();//默認構造 默認100容量MyArray(int capacity);MyArray(const MyArray& array);~MyArray();//尾插法void Push_Back(int val);//根據索引獲取值int getData(int…

系統架構札記

什么是高內聚、低耦合&#xff1f; 起因&#xff1a;模塊獨立性指每個模塊只完成系統要求的獨立子功能&#xff0c;并且與其他模塊的聯系最少且接口簡單&#xff0c;兩個定性的度量標準――耦合性和內聚性。 耦合性也稱塊間聯系。指軟件系統結構中各模塊間相互聯系緊密程度的一…

c++中運算符重載(加號運算,左移運算,前置后置++運算符,賦值運算,關系運算,函數運算)

運算符重載注意 重載的運算符要易讀內置的數據類型的表達式的運算符是不可以改變的不要重載&& 和 | | 運算符&#xff0c;[]和->運算符只能通過成員函數進行重載<<和>>只能通過全局函數配合友元函數進行重載 加號運算符重載 如果想讓自定義數據類型 進…

linux fstab解讀

fstab這個文件挺有用的。 從左到右&#xff1a; /dev/device mount-point type rules dump fsck 1. /dev/device: 不用說了吧&#xff1f;例如&#xff0c;/dev/hda1為M$-Win9x下的c:盤。 2. mount-point: 掛載點。例如&#xff0c;把/dev/hda1掛到/mnt/mywinc下。 3. type: ex…

c++實現字符串類的封裝

MyString.h文件 #define _CRT_SECURE_NO_WARNINGS#pragma once#include<iostream>#include<string>using namespace std;class MyString{friend ostream & operator<<(ostream & cout, MyString & str);friend istream & operator>>(…

c++中的繼承--1(引出,繼承方式,繼承的對象模型)

繼承的引出 概念&#xff1a; 繼承(inheritance)機制是面向對象程序設計使代碼可以復用的最重要的手段&#xff0c;它允許程序員在保持原有類特 性的基礎上進行擴展&#xff0c;增加功能&#xff0c;這樣產生新的類&#xff0c;稱派生類。繼承呈現了面向對象程序設計的層次結構…

Makefile經典教程(掌握這些足夠)

makefile很重要 什么是makefile&#xff1f;或許很多Winodws的程序員都不知道這個東西&#xff0c;因為那些Windows的IDE都為你做了這個工作&#xff0c;但我覺得要作一個好的和professional的程序員&#xff0c;makefile還是要懂。這就好像現在有這么多的HTML的編輯器&#xf…

c++中的繼承--2(繼承中的析構函數和構造函數,繼承中同名成員,繼承中靜態成員)

繼承中的構造函數和析構函數 繼承中的構造和析構順序 子類創建對象時&#xff0c;先調用父類的構造&#xff0c;然后調用自身構造析構順序與構造順序相反子類不會繼承父類的構造函數和析構函數如果父類中沒有合適默認構造&#xff0c;那么子類可以利用初始化列表的方式顯示的…

Linux鎖機制和線程安全

鎖機制是多線程編程中最常用的同步機制&#xff0c;用來對多線程間共享的臨界區進行保護。 1. 互斥鎖&#xff1a;pthread_mutex&#xff0c;屬于sleep-waiting類型的鎖 pthread_mutex_t *mutex; int pthread_mutex_int(mutex, attr) //以動態方式創建互斥鎖&#xff0c;參…

c++中的繼承--3(多繼承問題,菱形繼承)

繼承中的多繼承 #include<iostream>using namespace std;class Base1 { public:Base1(){m_A 10;} public:int m_A;};class Base2 { public:Base2(){m_A 10;} public:int m_B;int m_A;};class Son :public Base1, public Base2 {public:int m_C;int m_D; };void test01…

c++中的多態---1(多態概念,靜態聯編和動態聯編,多態原理解析,重載,重寫,重定義的對比)

多態的基本概念 多態是面向對象設計語言數據抽象和繼承之外的第三個基本特征多態性(polymorphism)提供接口與具體實現之間的另一層隔膜&#xff0c;從而將“what”和“how”分離開來&#xff0c;多態性改善了代碼的可讀和組織性&#xff0c;同時也使創建的程序具有可擴展性&am…

Ubuntu下各種服務搭建及操作技巧

Ubuntu下搭建TFTP 1、安裝軟件包 sudo apt-get install tftpd tftp xinetd 2、建立配置文件 在/etc/xinetd.d/下建立一個配置文件tftp sudo vi /etc/xinetd.d/tftp 內容如下 service tftp { socket_type dgram protocol udp wait yes user root …

c++多態--2(計算器,純虛函數和抽象類)

為什么要用多態 早期方法不利于擴展開閉原則 開閉原則 對擴展開放 對修改關閉利用多態實現—利于后期擴展&#xff0c;結構性非常好&#xff0c;可讀性高&#xff0c;效率稍微低&#xff0c;發生多態內部結構復雜 多態成立的條件 又繼承 子類重寫父類虛函數的函數&#xff1…

使用Automake和Autoconf生成Makefile

automake 所產生的 Makefile 除了可以做到程序的自動編譯和鏈接 外&#xff0c;還可以用來生成各種文檔&#xff08;如manual page、info文件&#xff09;&#xff0c;可以將源代碼文件包裝起來以供發布。所以程序源代碼所存放的目錄 結構最好符合GNU的標準慣例。下面以hello.…

c++中多態---3(虛析構和純虛析構,向上類型轉化和向下類型轉化)

虛析構和純虛析構 虛析構virtual ~類名(){}類內聲明&#xff0c;類內實現解決問題&#xff1a;通過父類指針指向子類對象釋放時候不干凈的問題 純虛析構 寫法 virtual ~類名(){}0; 類內聲明 類外實現 如果出現了純虛析構函數&#xff0c;這個類也算是抽象類&#xff0c;不可…

嵌入式開發硬件知識札記

三態邏輯 1. 概念 三態指其輸出既可以是一般二值邏輯電路&#xff0c;即正常的高電平&#xff08;邏輯1&#xff09;或低電平&#xff08;邏輯0&#xff09;&#xff0c;又可以保持特有的高阻抗狀態。高阻態相當于隔斷狀態&#xff08;電阻很大&#xff0c;相當于開路&#xff…

《凡人修仙傳》中打斗場景(c++多態實現)

我們 要實現打斗場景&#xff0c;第一&#xff0c;我們需要有打斗的雙方&#xff0c;一個是英雄&#xff0c;一個是怪物&#xff0c;他們都有自己的屬性&#xff0c;比如攻擊&#xff0c;防御&#xff0c;血量。其次我們的英雄還會有武器。武器上有一些加成屬性&#xff0c;可以…

使用mp4v2將aac音頻h264視頻數據封裝成mp4開發心得

這陣子在搗鼓一個將游戲視頻打包成本地可播放文件的模塊。開始使用avi作為容器&#xff0c;弄了半天無奈avi對aac的支持實在有限&#xff0c;在播放時音視頻時無法完美同步。 關于這點avi文檔中有提到&#xff1a; For AAC, one RAW AAC frame usually spans over 1024 samples…

c++模板---1(模板概念,利用模板實現數組排序,函數模板調用規則)

什么叫泛型編程&#xff1f;1. 參數類型化。 2. 模板 模板概念 c提供了函數模板&#xff0c;所謂函數模板&#xff0c;實際上是建立一個通用函數&#xff0c;其函數類型和形參類型不具體制定&#xff0c;用一個虛擬的類型來代表。這個通用函數就成為函數模板。凡是函數體相同…

c++模板--2(模板機制,模板的局限性,類模板,類模板做函數的參數)

函數模板機制結論 編譯器并不是把函數模板處理成能狗處理任何類型的函數函數模板通過具體類型產生不同的函數編譯器會對函數模板進行兩次編譯&#xff0c;在聲明的地方對模板代碼的本身進行編譯&#xff0c;在調用的地方對參數替換后代碼進行編譯在編譯器編譯階段&#xff0c;…