消除数组C6386警告

0

运行环境:VS2019(C语言编写)

程序目的:实现树的非递归前序和后序遍历

问题:前序和后序遍历方法执行时出现C6386警告(错误位置方法内已经标记

补充说明:C6386  官方解释:https://docs.microsoft.com/zh-cn/cpp/code-quality/c6386?f1url=%3FappId%3DDev16IDEF1%26l%3DZH-CN%26k%3Dk(C6386)%26rd%3Dtrue&view=msvc-160

#include<stdio.h>
#include<malloc.h>
typedef struct BiTNode {
	struct BiTNode* left;
	struct BiTNode* right;
	int val;
}BiTnode;
typedef struct stack{
	struct BiTNode* node;
	struct stack* next;
	int val;
}Stack;
void InitStack(Stack** S) {
	*S = NULL;
}
void Push(Stack** S, BiTnode* node, int val) {
	Stack* NewNode = (Stack*)malloc(sizeof(Stack));
	if (NewNode == NULL) return;
	NewNode->node = node;
	NewNode->val = val;
	NewNode->next = *S;
	*S = NewNode;
}
/***
* 判断栈是否为空
***/
bool IsEmpty(Stack* S) {
	return S == NULL ? true : false;
}
/***
* 执行出栈操作
***/
BiTnode* Pop(Stack** S) {
	if (IsEmpty(*S)) return NULL;
	Stack* tmp = *S;
	(*S) = (*S)->next;
	return tmp->node;
}
/***
* 创建二叉树
****/
void  CreateBiTree(BiTnode **root){
	int value;
	printf("请输入节点的值(以-1结束):\n");
	scanf_s("%d", &value);
	if (value == -1) {
		*root = NULL;
	}
	else {
		*root =(BiTnode*)malloc(sizeof(BiTnode));
		if (*root == NULL)
			return;
		(*root)->val = value;
		CreateBiTree(&(*root)->left);
		CreateBiTree(&(*root)->right);
	}
}
/***
* 计算树的节点数目
****/
int NodeCount(BiTnode* root) {
	if (root == NULL) return 0;
	return  NodeCount(root->left) + NodeCount(root->right) + 1;
}

int *PreOrderTraverse(BiTnode *root,int size){
	if (root == NULL) return NULL;
	int i = 0;
	int* res =(int*)malloc(sizeof(int)*size);
	if (res == NULL) return NULL;
	Stack* S;
	InitStack(&S);
	Push(&S, root, root->val);
	while (!IsEmpty(S)) {
		BiTnode* tmp = Pop(&S);
		printf("%d ", tmp->val);
//报错位置
		res[i] = tmp->val;
		i++;
		if (tmp->right) {
			Push(&S, tmp->right, tmp->right->val);
		}
		if (tmp->left) {
			Push(&S, tmp->left, tmp->left->val);
		}
	}
	return res;
}
/***
* 非递归后序遍历
****/
int* PostOrderTraverse(BiTnode *root,int size){
	if (root == NULL) return NULL;
	int i = 0;
	int* res =(int*)malloc(sizeof(int)*size);
	if (res == NULL) return NULL;
	Stack* S1, * S2;
	InitStack(&S1);
	InitStack(&S2);
	Push(&S1, root, root->val);
	while (!IsEmpty(S1)){
		BiTnode* tmp = Pop(&S1);
		Push(&S2,tmp,tmp->val);
		if (tmp->left) {
			Push(&S1, tmp->left, tmp->left->val);
		}
		if (tmp->right) {
			Push(&S1, tmp->right, tmp->right->val);
		}
	}
	while (!IsEmpty(S2)) {
		BiTnode* tmp = Pop(&S2);
		printf("%d ", tmp->val);
     //报错位置
		res[i] = tmp->val;
		i++;
	}
	return res;
}
int main(){
	BiTnode* root;
	CreateBiTree(&root);
	int count = NodeCount(root);
	printf("树的节点数目:%d\n",count);
	printf("前序遍历:\n");
	PreOrderTraverse(root,count);
	printf("\n后序遍历:\n");
	PostOrderTraverse(root, count);
	return 0;
}


补充说明:C6386 警告

C6386
警告 C6386:缓冲区溢出: <buffer name> 正在访问,可写的大小为 <size1> 字节,但 <size2> 可能会写入字节:行: x,y
此警告表明指定缓冲区的可写范围可能小于用于写入它的索引。 这可能会导致缓冲区溢出。
示例
下面的代码将生成此警告和 C6201:
C++

复制
#define MAX 25

void f ( )
{
  char ar[MAX];
  // code ...
  ar[MAX] = '\0';
}
若要更正这两个警告,请使用以下代码:
C++

复制
#define MAX 25

void f ( )
{
   char a[MAX];
   // code ...
   a[MAX - 1] = '\0';
}
C
ava
凤栖梧

2020-12-30

1 解决方案有了,读取数组的值使用指针解引 res[i]=tmp->val,改成 *(res+i)=tmp->val;即可,原因不晓得 -  凤栖梧  2020-12-30
0

我估计没人帮你去查 C6386 是什么意思,答个问题太累。

ava
慢羊羊

2020-12-30

大佬,,辛苦了,,,这个文档编辑不了了,本来我想提交相关错误的解释还有微软官方解释链接。 -  凤栖梧  2020-12-30
@凤栖梧 这回又补充的太过了。其实只需要贴上来 VS 的提示就足够了,简单明了。 -  慢羊羊  2020-12-30
技术讨论社区