联合内部的指针是否共享相同的内存位置? [英] Do pointers inside a union share same memory location?

查看:32
本文介绍了联合内部的指针是否共享相同的内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一篇文章以了解union 并且我已经理解 Union 的大小取决于最大的变量大小,并且变量共享相同的内存.所以这个概念对我来说很清楚,但在文章中作者说当二叉树有两个指针指向另外两个孩子时,对二叉树使用联合"是值得的.什么是联合的应用?"在我的脑海中出现了一个问题.那篇文章的部分,联合内部指针的可能解释是什么?链接已在下面给出.

https://www.geeksforgeeks.org/union-c/

原来如此.有谁能帮帮我吗?

解决方案

在文章中,作者说当二叉树有两个指针指向另外两个子树时,对二叉树使用联合"是值得的......联合内部指针的可能解释是什么?

我相信你在谈论这个代码片段:

<代码>结构节点{bool is_leaf;联合{结构{结构节点*左;结构节点*对;} 内部的;双数据;} 信息;};

你在这里误解了作者的意图.他们使用联合来实现树中两种不同类型的节点:具有左右指针的内部节点和具有数据的叶节点.这个联合在 structdouble 之间共享内存.它在左右指针之间共享内存.

I was going through an article to learn about union and I had understood that the size of a Union depends upon the largest variable size and the variables share the same memory. So the concept was pretty clear for me but in the article author said that using "union" for a binary tree was worthwhile when it had two pointers to point other two child. A question arose in my mind for the "What are applications of union?" section of that article, what would be the possible explanations for pointers inside a union? The link has been given below.

https://www.geeksforgeeks.org/union-c/

So, this is it. Is there anybody who can help me out?

解决方案

in the article author said that using "union" for a binary tree is worthwhile when it had two pointers to point other two child...what would be the possible explanations for pointers inside a union?

I believe you are talking about this code snippet:


struct NODE { 
    bool is_leaf; 
    union { 
        struct
        { 
            struct NODE* left; 
            struct NODE* right; 
        } internal; 
        double data; 
    } info; 
}; 

You misunderstand the author's intent here. They are using a union to implement the two different kinds of nodes in a tree: an internal node that has a left and right pointer and a leaf node which has data. This union shares memory between a struct and a double. It does not share memory between the left and right pointers.

这篇关于联合内部的指针是否共享相同的内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆