开发者社区> 问答> 正文

C代码报Segmentation fault? 400 报错

C代码报Segmentation fault? 400 报错

自己照着数据结构写了个二叉树的先序遍历:

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


typedef struct TreeNode *BinTree;
typedef BinTree position;
struct TreeNode{
	const char *data;
	BinTree left;
	BinTree right;
};

/**
 * 先序遍历二叉树
 */
void preOrderTraversal(BinTree tree);

BinTree createChildTree(BinTree tree, char const *left_data, char const *right_data);

BinTree initTree();

const char *alphbet = "ABCDFGIEH";

int main(int argc, char const *argv[])
{
	// create binary tree...
	BinTree tree = initTree();
	
	preOrderTraversal(tree);
	return 0;
}

void preOrderTraversal(BinTree tree){
	// more smarter way...
	if(tree){
		printf("node data of tree is %s\n", tree->data);
		preOrderTraversal(tree->left);
		preOrderTraversal(tree->right);
	}
}

BinTree initTree(){
	// root tree node....
	BinTree tree = (BinTree) malloc(sizeof(TreeNode));
	tree->data = &alphbet[0]; 
	tree->left = NULL; tree->right = NULL;
	// second level...
	createChildTree(tree, &alphbet[1], &alphbet[2]);

	// third level...
	createChildTree(tree->left, &alphbet[3], &alphbet[4]);
	createChildTree(tree->right, &alphbet[5], &alphbet[6]);

	// fourth level,not complete
	createChildTree(tree->left->right, &alphbet[7], NULL);
	createChildTree(tree->right->left, NULL, &alphbet[8]);
	return tree;
}

BinTree createChildTree(BinTree tree, const char *left_data, char const *right_data){
	if(left_data){
		BinTree left = (BinTree) malloc(sizeof(BinTree));
		left->data = left_data; 
		left->left = NULL; left->right=NULL;
		tree->left = left;
	}
	if(right_data){
		BinTree right = (BinTree) malloc(sizeof(BinTree));
		right->data = right_data; 
		right->left = NULL; right->right = NULL;
		tree->right = right;
	}
	return tree;
}

运行的时候的异常信息如下:

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
bash: line 1:  2401 Segmentation fault: 11  '/Users/apple/dev/study/data_struct/temp'
[Finished in 0.1s with exit code 139]

我的环境是mac + sublime text。

请问下,我代码哪里出错了????


展开
收起
爱吃鱼的程序员 2020-06-03 15:46:56 672 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    段错误一般是访问无效内存导致吧

    ######      The function of createChildTree return  a value, but the function of initTree dosen't take over the value, so the code  occurs Segmentation fault.######接收返回值也是一样的。。 it doesn't work######

    这段编译应该都过不了

    BinTree tree = (BinTree) malloc(sizeof

    (TreeNode));

    structTreeNode{
        constchar*data;
        BinTree left;
        BinTree right;
    };


    ######回复 @Tchaikovsk : 把sizeof(BinTree) 换成sizeof(TreeNode)就好了 因为sizeof BinTree计算的是TreeNode *的字节大小 而实际保存的TreeNode数据的时候超出了这个范围,因此会出现断错误,对吗?######回复 @Tchaikovsk : 我的理解就是声明一个字符串p,并初始化。 &p[] 获取p中元素的地址。######回复 @润群 : const char *p = "ABCDFGIEH"; &p[] 这样用不知道你懂不懂。中间的区别。######回复 @润群 : 不太清楚,你程序问题很多,不过 sizeof(struct node )并不支持写成 sizeof(node) ,sizeof(node) 应该会报 node变量未声明,你试试。######可以编译啊,你试试看 我编译通过哦######

    segmentation falut多数是由于类型不匹配  楼上说的结构体那里的确有问题

    改成sizeof(struct TreeNode)或者

    BinTree tree; sizeof(*tree)

    ######谢谢哈,原因确实是这样######sizeof(BinTree)有问题,BinTree是一个指针,而不是结构体######谢谢哈,已经解决啦
    2020-06-03 20:58:08
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Towards A Fault-Tolerant Speaker Verification System: A Regularization Approach To Reduce The Condition Number 立即下载
Easy,scalable,Fault-tolerant S 立即下载
低代码开发师(初级)实战教程 立即下载