weixin_54910555 2021-02-21 16:55 采纳率: 20%
浏览 36

问题: 为什么我的删除链表函数总是无法执行,我在主线程里插入了节点。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

typedef struct node
{
	int data;
	struct node *next;
}list;

pthread_mutex_t mutex;
list *p=NULL;

list *create(void)
{
	list *p=NULL;

	p=(list *)malloc(sizeof(list));
	if(p==NULL)
	{
		printf("create malloc is failed\n");
		return NULL;
	}

	p->data=0;
	p->next=NULL;
	return p;
}

int length_link_list(list *h)
{
	int len = 0;
	list *p = h->next;

	while(p != NULL){
		len++;
		p = p->next;
	}
	return len;
}

int instr_list(list *h,int pos,int data)
{
	int i=0;
	list *p=NULL;

	p=(list *)malloc(sizeof(list));
	if(p==NULL)
	{
		printf("instr_list malloc is falled\n");
		return -1;
	}

	for(i=1;i<pos;i++)
	{
		h=h->next;
	}

	h->data=data;

	p->next=h->next;
	h->next=p;

	return 0;
}

int delete_list(list *h,int pos)
{
	int i=0;
	list *p=NULL;
	if(pos<1 || pos>length_link_list(h))
	{
		printf("delete_any_node pos error\n");
		return -1;
	}
	for(i=1;i<pos;i++)
	{
		h=h->next;
	}

	p=h->next;
	h->next=p->next;
	free(p);
	return 0;
}

int fine_list(list *h,int data)
{
	int i=0;
	int pos=1;
	list *p=h->next;

	while(p!=NULL)
	{
		if(p->data==data)
		{
			printf("fine is\n");
		}
		pos++;
		p=p->next;
	}

	return pos;

}

int my_printf(list *h)
{
	list *p=h->next;
	while(p!=NULL)
	{
		printf("%d",p->data);
		p=p->next;
	}
	printf("\n");
	return 0;
}

void *Fun(void *arg)
{
	int pos=0;
	pthread_mutex_lock(&mutex);

	pos=fine_list(p,1);

	delete_list(p,pos);

	printf(" ------------\n");
	my_printf(p);

	pthread_mutex_unlock(&mutex);
	return NULL;
}

int main(int argc, const char *argv[])
{
	int ret=0;
	pthread_t pid;

	ret=pthread_mutex_init(&mutex,NULL);
	if(ret!=0)
	{
		printf("pthread_mutex_init is failed\n");
		return -1;
	}

	pthread_mutex_lock(&mutex);
	p=create();
	instr_list(p,1,0);
	instr_list(p,2,1);
	instr_list(p,3,2);
	pthread_mutex_unlock(&mutex);

	ret=pthread_create(&pid,NULL,Fun,NULL);
	if(ret!=0)
	{
		printf("pthread_create is failed\n");
		return -1;
	}

	pthread_join(pid,NULL);

	return 0;
}
stu@stu-VirtualBox:~/day5$ gcc xiancheng2.c -lpthread
stu@stu-VirtualBox:~/day5$ ./a.out
fine is
delete_any_node pos error
 ------------
120
stu@stu-VirtualBox:~/day5$ 

题目:主线程创建并插入链表节点,新线程查找节点,找到则删除该节点

问题:为什么我的删除链表函数总是无法执行,我在主线程里插入了节点,为什么在子线程里无法找得到,分配了内存在堆上,为什不共享,还有在主线程插入我的想法是应该是012,为什么是120,这是什么原因

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-24 11:28
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    你的代码和输出结果看起来像是一个简单的C语言程序。在你的main()函数中,你创建了一个新的链表节点,并在主线程中将其插入到主链表的头结点位置。

    然而,在子线程中,当你试图查找这个节点时,它没有被正确地复制或共享。这可能是由于以下原因:

    1. 内存泄漏: 如果你在主线程中分配了一个大块内存(例如,用于创建链表),并在子线程中也使用了相同的内存块,则可能会导致内存泄漏。当主线程完成工作后,如果子线程仍然持有这些内存块,则会导致内存泄露。

    2. 未初始化变量: 在子线程中,你尝试访问的是一个已经指向空指针的引用。这表明你可能忘记在子线程开始前将该引用设置为一个有效的地址。你需要确保所有引用都是有效的,并且它们是在正确的上下文中使用的。

    3. 不同作用域: 主线程中的函数和子线程中的函数可能不在同一作用域下运行,因此它们不能直接访问彼此的作用域内的变量。这可能导致变量被意外修改或者获取错误的结果。

    4. 不同的数据结构: 链表是一种单向数据结构,而你可能正在尝试在一个双向链表或其他数据结构上进行操作。如果你的操作依赖于数据结构的行为方式,那么你可能需要调整你的代码以适应这种差异。

    为了修复这些问题,你可以考虑以下几点:

    • 检查是否有内存泄漏的问题。可以使用valgrind等工具来检测内存泄漏。
    • 确保所有的变量在调用之前都已经被初始化。
    • 使用适当的锁机制来防止多线程对同一个资源同时进行访问。
    • 确保你的代码在不同作用域之间有明确的边界。

    此外,为了提高代码可读性和可维护性,建议你:

    • main()函数转换为一个函数,接受一个参数,表示要操作的链表的长度。
    • my_printf()函数提取到一个单独的函数中,以便更好地组织和重用代码。
    • 尽量避免在子线程中更改全局变量,而是通过传递参数的方式来进行操作。
    • 尝试使用更简洁的语法和命名约定,使代码更加清晰易懂。
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵