结构体变量中的初始化问题,malloc函数在子函数中的应用

结构体申请变量后,一定要全部初始化,不然在某些IDE上可能出现bug;`

仔细看下面的,错误一大堆一大堆

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

struct TreeNode
{
	int	val;
	struct TreeNode *left;
	struct TreeNode *right;
};



void insert(struct TreeNode * T, int value);
void preorder(struct TreeNode* T);

int main()
{
	int i,j,k;
	struct TreeNode *T=(struct TreeNode*)malloc(sizeof(struct TreeNode));
	//T->left=T->right=NULL;(注释掉的这一行,非常重要,要加回来才是可行的)
	scanf("%d",&k);
	T->val=k;
	insert(T,k); 
	printf("www");
	preorder(T);
	return 0;
} 
void insert(struct TreeNode * T, int value)//创建树
{
	if(value<0) return;
	int i,j;
    T=(struct TreeNode*)malloc(sizeof(struct TreeNode));//创建一个节点
    //T->left=T->right=NULL;(还有这一行也要加回来)
    T->val = value;
    scanf("%d",&i);
    insert(T->left,i);
    scanf("%d",&j);
    insert(T->right,j);
}
void preorder(struct TreeNode* T)
{
	if(T)
	{
		printf("%d\t",T->val);
		printf("a\n");
		preorder(T->left);
		preorder(T->right);
	}
} 

不然就可能是这样,无尽循环在这里插入图片描述
还有关于malloc函数在子函数中的应用

在函数里malloc,如何将地址传到函数外?

一种是用return把地址带出来
也可以用二级指针将函数内的地址带出

func(struct node *p)这个是对指针p指向的节点进行修改
func(struct node **p)这个是对p指针本身进行修改(一般会改变p的指向)
转自:https://bbs.csdn.net/topics/391926409

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//void GetMemory1(char*p, int num)
//{
//	p = (char*)malloc(sizeof(char)*num);
//}
void GetMemory2(char**p, int num)
{
	p =(char**)malloc(sizeof(char*));
	*p = (char*)malloc(sizeof(char)*num);
}
 
//char* GetMemory3(char*p, int num)
//{
//	p = (char*)malloc(sizeof(char)*num);
//	return p;
//}
//char* GetMemory4(void)
//{
//	char p[] = "hello world";
//	return p;
//}
void GetMemory6(char**p, int num)
{

	*p = (char*)malloc(sizeof(char)*num);
}
int main()
{
	
 
		char* str1 = NULL;
		char* str2 = NULL;
		char* str3 = NULL;
		char* str4 = NULL;
		char** pstr5=NULL;
		char** pstr6=(char**)malloc(sizeof(char*));
		//GetMemory1(str1,100);
		//GetMemory2(&str2, 100);
		//str3 = GetMemory3(str3, "hellow");
		//str4 = GetMemory4();
		//GetMemory2(pstr5, 100);
		GetMemory6(pstr6, 100);
 		
 		
		
		//strcpy(str1, "hello");
		//printf("%s\n", str1);       
		//无法输出,将str1的地址拷贝到p中,p虽然分配完内存空间,
		//但是在在函数结束时p被释放,str1只有一个空间,所以str1越界
 
		//strcpy(str2, "hello");
		//printf("%s\n", str2);
		//可以输出hello,因为传递的是&str2值(将str2的地址拷贝到给p)
		//函数执行完毕后,p虽然释放但是p指向的对象(str2)已经被分配完内存空间
 
		/*strcpy(str3, "hello");
		printf("%s\n", str3);*/
		//返回的地址,相当于拷贝给str3,所以str3实际已经有100个空间。
		
		//printf("%s\n", str4);
		//输出乱码,因为函数执行完毕后,函数GetMemory4()中的数组空间
		//被释放,所以str4这个指针指向的内容是乱码,vs2015编译器是
		//烫烫烫,内存是cc cc cc cc cc cc cc cc 四个c对应中文烫
		//与第三个GetMemory3()对比,因为malloc分配的内存空间再函数
		//结束后如果不free是不会被释放的。所以可以输出hello。
		
		//strcpy(*pstr5, "hello");
		//printf("%s\n", *pstr5);
		//无法输出,将pstr5拷贝到p中,但是原来pstr5为空,*p非法 
		
		strcpy(*pstr6, "hello");
		printf("%s\n", *pstr6);
		//输出hello,输入的二级指针需要保证已经分配空间,不能是空指针或者是野指针,
		//否则*p非法,造成堆区溢出 
		return 0;
}
}

基于https://www.cnblogs.com/lzq1126/p/5596838.html的改良版

此处笔者曾多次踩坑,尤其是关于5,6的区别,这里笔者优先采用str2的做法,其次是3,再其次是6(这个实在被坑太多次了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值