C ++联合内存用户 [英] C++ Unions Memory User

查看:69
本文介绍了C ++联合内存用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,在Union中,仅为所有变量分配了内存位置,并且一次只能在内存中存储一​​个变量.
我们可以不使用单独的标志而找出当前存储在内存中的成员变量吗?

假设我们有一个工会

We know that in Union only memory location is allocated for all variable and at a time only one variable can be stored in memory.
Can we find out which member variable is presently stored in memory without using seperate flags?

suppose we have an union

union cde
{
    char c;
    int d;
    float e
}



现在在这里如何找出当前存储在内存中的成员变量?
假设成员变量中包含垃圾值.



Now here how to find out which member variable is presently stored in memory?
Assume that the member variables have garbage values in them.

推荐答案

您不能说当前存储在内存中的变量是什么,但都是相同的地点;如果您更改c,则d和e也将更改.

有关工会的更多信息; http://www.cplusplus.com/doc/tutorial/other_data_types/ [
You can''t say which variable is currently stored in memory, they all are, but at the same location; if you change c, then d and e will change too.

Some more info on unions; http://www.cplusplus.com/doc/tutorial/other_data_types/[^].


男孩,你是今天充满了奇怪的问题.

联合允许您以多种方式解释位和/或字节的序列".它不能容纳多个变量,但是您可以将单个内容视为不同的内容.

有时,如您的示例所示,这样做没有任何意义.有时,联合是具有键入字段的较大记录的一部分,因此您确切地知道如何解释.

如果不输入标志,就无法真正分辨出另一种.哦,您可以根据位模式做出一些猜测,但实际上,您无法从大整数值中分辨出非常小的浮点数,并且几乎任何8位序列都可以是字符串,因此除非您要在字符串中查找单词特定的语言,您无法分辨.
Boy, you are full of strange questions today.

A union allows you to interpret a "sequence of bits and/or bytes" in multiple ways. It cannot hold multiple variables but you can look at the single contents as if it were different things.

Sometimes it doesn''t make any sense to do so, as in your example. Sometimes the union is part of a larger record that has the typing field so you know exactly how to interpret.

Without typing flags, you cannot really tell one kind from another. Oh, you can make some guesses based on bit patterns but, in reality, you cannot tell really small floating numbers from large integer values and just about any 8 bit sequence can be a character string so unless you''re looking for words in a particular language, you cannot tell.


这篇关于C ++联合内存用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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