最近寫程式用到了Linux系統下C語言的佇列操作,於是有了下面一個問題
#下面是佇列的程式碼:
這個佇列頭檔
extern struct pqueue Que; /*构造一个空队列*/ extern pQueue *InitQueue(); /*销毁一个队列*/ extern void DestroyQueue(pQueue *pqueue); /*清空一个队列*/ extern void ClearQueue(pQueue *pqueue); /*判断队列是否为空*/ extern int IsEmpty(pQueue *pqueue); /*返回队列大小*/ extern int GetSize(pQueue *pqueue); /*返回队头元素*/ extern PNode GetFront(pQueue *pqueue,char *pitem); /*返回队尾元素*/ extern PNode GetRear(pQueue *pqueue,char *pitem); /*将新元素入队*/ extern PNode InQueue(pQueue *pqueue,char *pitem); /*队头元素出队*/ extern PNode OutQueue(pQueue *pqueue,char *pitem);
下面是佇列函數
struct pqueue Queue;
/*构造一个空队列*/
pQueue *InitQueue()
{
pQueue *pqueue = (pQueue *)malloc(sizeof(Queue));
if(pqueue!=NULL)
{
pqueue->front = NULL;
pqueue->rear = NULL;
pqueue->size = 0;
}
return pqueue;
}
/*销毁一个队列*/
void DestroyQueue(pQueue *pqueue)
{
if(IsEmpty(pqueue)!=1)
ClearQueue(pqueue);
free(pqueue);
}
/*清空一个队列*/
void ClearQueue(pQueue *pqueue)
{
while(IsEmpty(pqueue)!=1)
{
OutQueue(pqueue,NULL);
}
}
/*判断队列是否为空*/
int IsEmpty(pQueue *pqueue)
{
if(pqueue->front==NULL&&pqueue->rear==NULL&&pqueue->size==0)
return 1;
else
return 0;
}
/*返回队列大小*/
int GetSize(pQueue *pqueue)
{
return pqueue->size;
}
/*返回队头元素*/
PNode GetFront(pQueue *pqueue,char *pitem)
{
if(IsEmpty(pqueue)!=1)
{
//pitem = pqueue->front->data;
strcpy(pitem,pqueue->front->data);
}
return pqueue->front;
}
/*返回队尾元素*/
PNode GetRear(pQueue *pqueue,char *pitem)
{
if(IsEmpty(pqueue)!=1)
{
//pitem = pqueue->rear->data;
strcpy(pitem,pqueue->rear->data);
}
return pqueue->rear;
}
/*将新元素入队*/
PNode InQueue(pQueue *pqueue,char *pitem)
{
//DBG0_PR("dbg QueueIn front=%d, rear=%d, count=%d\n", pqueue->front, pqueue->rear, pqueue->size);
PNode pnode = (PNode)malloc(sizeof(Node));
if(pnode != NULL)
{
strcpy(pnode->data, pitem);
pnode->next = NULL;
if(IsEmpty(pqueue))
{
pqueue->front = pnode;
}
else
{
pqueue->rear->next = pnode;
}
pqueue->rear = pnode;
pqueue->size++;
}
return pnode;
}
/*队头元素出队*/
PNode OutQueue(pQueue *pqueue,char *pitem)
{
PNode pnode = pqueue->front;
if(IsEmpty(pqueue)!=1 && pnode!=NULL)
{
if(pitem!=NULL)
strcpy(pitem,pnode->data);
//pitem = pnode->data;
pqueue->front = pnode->next;
free(pnode);
pqueue->size = pqueue->size - 1;
if(pqueue->size == 0 ){
pqueue->rear = NULL;
}
}
return pqueue->front;
}
問題在使用佇列的outque時,說明如下:
入隊操作,佇列大小size為1,出隊操作佇列大小運算為0,然後程式循環一圈回來再判斷佇列大小,size值變成了393216,改了半天也不知道怎麼回事,
提示錯誤是這樣的
*** glibc detected *** double free or corruption (!prev):
如果哪位大蝦看見了,求解答或給個思路,感覺自己已經進了死胡同了,跪謝! ! ! !
回覆討論(解決方案)
#註解掉一部分程式碼,如果問題消失,問題就出在註解掉的程式碼裡
即時印出size的值,看哪一步出現的例外
僅供參考
#ifndef __PQUEUE_H__ #define __PQUEUE_H__ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_DATA_SIZE 256 typedef struct _node { char data[MAX_DATA_SIZE]; struct _node* next; } Node, *pNode; typedef struct __pqueue { pNode front; pNode rear; int size; } Queue, *pQueue; /*构造一个空队列*/ extern pQueue InitQueue(); /*销毁一个队列*/ extern void DestroyQueue(pQueue pqueue); /*清空一个队列*/ extern void ClearQueue(pQueue pqueue); /*判断队列是否为空*/ extern int IsEmpty(pQueue pqueue); /*返回队列大小*/ extern int GetSize(pQueue pqueue); /*返回队头元素*/ extern int GetFront(pQueue pqueue, char *pitem); /*返回队尾元素*/ extern int GetRear(pQueue pqueue, char *pitem); /*将新元素入队*/ extern int InQueue(pQueue pqueue, char *pitem); /*队头元素出队*/ extern int OutQueue(pQueue pqueue, char *pitem); #endif /* __PQUEUE_H__ */ //////////////////////////////////////////////////////// #include "pqueue.h" #define err_log(fmt, ...) printf("[%s:%d]"fmt"\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) #define err_assert(con) { \ if (!(con)) { \ printf("[%s:%d]ASSERT>>> %s failed\n", __FUNCTION__, __LINE__, #con); \ abort(); \ } \ } /*构造一个空队列*/ pQueue InitQueue() { return (pQueue)calloc(1, sizeof(Queue)); } /*销毁一个队列*/ void DestroyQueue(pQueue pqueue) { err_assert(pqueue != NULL); if(!IsEmpty(pqueue)) ClearQueue(pqueue); free(pqueue); } /*清空一个队列*/ void ClearQueue(pQueue pqueue) { err_assert(pqueue != NULL); while (!IsEmpty(pqueue)) { OutQueue(pqueue, NULL); } } /*判断队列是否为空*/ int IsEmpty(pQueue pqueue) { err_assert(pqueue != NULL); return !pqueue->size; } /*返回队列大小*/ int GetSize(pQueue pqueue) { err_assert(pqueue != NULL); return pqueue->size; } /*返回队头元素*/ int GetFront(pQueue pqueue, char *pitem) { err_assert(pqueue != NULL); if (IsEmpty(pqueue)) { return -1; } if (pitem) { err_assert(pqueue->front != NULL); strcpy(pitem, pqueue->front->data); } return 0; } /*返回队尾元素*/ int GetRear(pQueue pqueue, char *pitem) { err_assert(pqueue != NULL); if (IsEmpty(pqueue)) { return -1; } if (pitem) { err_assert(pqueue->rear != NULL); strcpy(pitem,pqueue->rear->data); } return 0; } /*将新元素入队*/ int InQueue(pQueue pqueue, char *pitem) { err_assert(pqueue != NULL); pNode pnode = (pNode)calloc(1, sizeof(Node)); if(NULL == pnode) { return -1; } strcpy(pnode->data, pitem); pnode->next = NULL; if(IsEmpty(pqueue)) { pqueue->front = pnode; } else { pqueue->rear->next = pnode; } pqueue->rear = pnode; pqueue->size++; return 0; } /*队头元素出队*/ int OutQueue(pQueue pqueue,char *pitem) { err_assert(pqueue != NULL); pNode pnode = pqueue->front; if (IsEmpty(pqueue)) { err_log("empty queue"); return -1; } if (pitem) strcpy(pitem, pnode->data); pqueue->front = pnode->next; free(pnode); pqueue->size--; if (pqueue->size == 0 ){ pqueue->rear = NULL; } return 0; } //////////////////////////////////////////////////////// #include "pqueue.h" int main(void) { pQueue queue = NULL; queue = InitQueue(); InQueue(queue, "I'm "); InQueue(queue, "a "); InQueue(queue, "boy. "); while (!IsEmpty(queue)) { char buf[MAX_DATA_SIZE]; if (OutQueue(queue, buf) < 0) { break; } printf("%s", buf); } printf("\n"); DestroyQueue(queue); return 0; }
##找到問題了,不是佇列的原因,我在一個函數裡面malloc了一個char*,然後在線程中調用該函數返回的這個char*,用完之後free(在該功能尾部),結果就報上面的錯誤了,我註釋掉這句free之後就沒事了,不解的是不知道為啥不能freefree應該放在你寫malloc的函數裡面*** glibc detected *** double free or !prev): 通常是指操作已釋放的對象,如:
1.已釋放對象,卻再次操作該指標所指對象。
以上是Linux下關於C語言佇列問題的詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

維護模式用於系統維護和修復,允許管理員在簡化環境中工作。 1.系統修復:修復損壞的文件系統和啟動加載器。 2.密碼重置:重置root用戶密碼。 3.軟件包管理:安裝、更新或刪除軟件包。通過修改GRUB配置或使用特定鍵進入維護模式,執行維護任務後可安全退出。

Linux網絡配置可以通過以下步驟完成:1.配置網絡接口,使用ip命令臨時設置或編輯配置文件持久化設置。 2.設置靜態IP,適合需要固定IP的設備。 3.管理防火牆,使用iptables或firewalld工具來控製網絡流量。

維護模式在Linux系統管理中扮演關鍵角色,幫助進行系統修復、升級和配置變更。 1.進入維護模式可以通過GRUB菜單選擇或使用命令“sudosystemctlisolaterescue.target”。 2.在維護模式下,可以執行文件系統修復和系統更新等操作。 3.高級用法包括重置root密碼等任務。 4.常見錯誤如無法進入維護模式或掛載文件系統,可通過檢查GRUB配置和使用fsck命令修復。

使用Linux維護模式的時機和原因:1)系統啟動問題時,2)進行重大系統更新或升級時,3)執行文件系統維護時。維護模式提供安全、控制的環境,確保操作的安全性和效率,減少對用戶的影響,並增強系統的安全性。

Linux中不可或缺的命令包括:1.ls:列出目錄內容;2.cd:改變工作目錄;3.mkdir:創建新目錄;4.rm:刪除文件或目錄;5.cp:複製文件或目錄;6.mv:移動或重命名文件或目錄。這些命令通過與內核交互執行操作,幫助用戶高效管理文件和系統。

在Linux中,文件和目錄管理使用ls、cd、mkdir、rm、cp、mv命令,權限管理使用chmod、chown、chgrp命令。 1.文件和目錄管理命令如ls-l列出詳細信息,mkdir-p遞歸創建目錄。 2.權限管理命令如chmod755file設置文件權限,chownuserfile改變文件所有者,chgrpgroupfile改變文件所屬組。這些命令基於文件系統結構和用戶、組系統,通過系統調用和元數據實現操作和控制。

MaintenancemodeInuxisAspecialBootenvironmentforforcalsystemmaintenancetasks.itallowsadMinistratorStoperFormTaskSlikerSettingPassingPassingPasswords,RepairingFilesystems,andRecoveringFrombootFailuresFailuresFailuresInamInimAlenimalenimalenrenmentrent.ToEnterMainterMainterMaintErmaintErmaintEncemememodeBoode,Interlecttheboo

Linux的核心組件包括內核、文件系統、Shell、用戶空間與內核空間、設備驅動程序以及性能優化和最佳實踐。 1)內核是系統的核心,管理硬件、內存和進程。 2)文件系統組織數據,支持多種類型如ext4、Btrfs和XFS。 3)Shell是用戶與系統交互的命令中心,支持腳本編寫。 4)用戶空間與內核空間分離,確保系統穩定性。 5)設備驅動程序連接硬件與操作系統。 6)性能優化包括調整系統配置和遵循最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中