首页 运维 linux运维 Linux下关于C语言队列问题的详解

Linux下关于C语言队列问题的详解

Jun 07, 2017 am 09:56 AM

最近写程序用到了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&#39;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之后就没事了,不解的是不知道为啥不能free

free应该放在你写malloc的函数里面

*** glibc detected ***      double free or corruption (!prev): 
通常是指操作已释放的对象,如: 
1.已释放对象,却再次操作该指针所指对象。 

2.多线程中某一动态分配的对象同时被两个线程使用,一个线程释放了该对象,而另一线程继续对该对象进行操作。

加个线程同步 应该就没有问题

以上是Linux下关于C语言队列问题的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

vscode需要什么电脑配置 vscode需要什么电脑配置 Apr 15, 2025 pm 09:48 PM

VS Code 系统要求:操作系统:Windows 10 及以上、macOS 10.12 及以上、Linux 发行版处理器:最低 1.6 GHz,推荐 2.0 GHz 及以上内存:最低 512 MB,推荐 4 GB 及以上存储空间:最低 250 MB,推荐 1 GB 及以上其他要求:稳定网络连接,Xorg/Wayland(Linux)

vscode 无法安装扩展 vscode 无法安装扩展 Apr 15, 2025 pm 07:18 PM

VS Code扩展安装失败的原因可能包括:网络不稳定、权限不足、系统兼容性问题、VS Code版本过旧、杀毒软件或防火墙干扰。通过检查网络连接、权限、日志文件、更新VS Code、禁用安全软件以及重启VS Code或计算机,可以逐步排查和解决问题。

notepad怎么运行java代码 notepad怎么运行java代码 Apr 16, 2025 pm 07:39 PM

虽然 Notepad 无法直接运行 Java 代码,但可以通过借助其他工具实现:使用命令行编译器 (javac) 编译代码,生成字节码文件 (filename.class)。使用 Java 解释器 (java) 解释字节码,执行代码并输出结果。

Linux体系结构:揭示5个基本组件 Linux体系结构:揭示5个基本组件 Apr 20, 2025 am 12:04 AM

Linux系统的五个基本组件是:1.内核,2.系统库,3.系统实用程序,4.图形用户界面,5.应用程序。内核管理硬件资源,系统库提供预编译函数,系统实用程序用于系统管理,GUI提供可视化交互,应用程序利用这些组件实现功能。

vscode 可以用于 mac 吗 vscode 可以用于 mac 吗 Apr 15, 2025 pm 07:36 PM

VS Code 可以在 Mac 上使用。它具有强大的扩展功能、Git 集成、终端和调试器,同时还提供了丰富的设置选项。但是,对于特别大型项目或专业性较强的开发,VS Code 可能会有性能或功能限制。

VSCode怎么用 VSCode怎么用 Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) 是一款跨平台、开源且免费的代码编辑器,由微软开发。它以轻量、可扩展性和对众多编程语言的支持而著称。要安装 VSCode,请访问官方网站下载并运行安装程序。使用 VSCode 时,可以创建新项目、编辑代码、调试代码、导航项目、扩展 VSCode 和管理设置。VSCode 适用于 Windows、macOS 和 Linux,支持多种编程语言,并通过 Marketplace 提供各种扩展。它的优势包括轻量、可扩展性、广泛的语言支持、丰富的功能和版

git怎么查看仓库地址 git怎么查看仓库地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 仓库地址,请执行以下步骤:1. 打开命令行并导航到仓库目录;2. 运行 "git remote -v" 命令;3. 查看输出中的仓库名称及其相应的地址。

vscode是什么 vscode是干什么用的 vscode是什么 vscode是干什么用的 Apr 15, 2025 pm 06:45 PM

VS Code 全称 Visual Studio Code,是一个由微软开发的免费开源跨平台代码编辑器和开发环境。它支持广泛的编程语言,提供语法高亮、代码自动补全、代码片段和智能提示等功能以提高开发效率。通过丰富的扩展生态系统,用户可以针对特定需求和语言添加扩展程序,例如调试器、代码格式化工具和 Git 集成。VS Code 还包含直观的调试器,有助于快速查找和解决代码中的 bug。

See all articles