Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12807
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Four variables share one string? |
| Date | 2011-12-25 08:54 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jd79uk$gqa$1@dont-email.me> (permalink) |
| References | <9cbdac30-eb33-45cf-8fe5-661f799c27c2@a17g2000yqj.googlegroups.com> <jd5mqa$804$1@dont-email.me> <ef4d8a53-c945-4064-8a68-c0fa364cae37@t8g2000yqg.googlegroups.com> <87zkeg3kro.fsf@Niukka.kon.iki.fi> |
On 12/25/2011 7:20 AM, Kalle Olavi Niemitalo wrote:
> Nephi Immortal<immortalnephi@gmail.com> writes:
>
>> enum has four constants and require additional 16 bytes.
>
> Usually not true. In typical implementations of C++ (and C),
> if you compile
>
> enum { zero, one, two, three };
> int f(const int p[])
> {
> return p[two] - p[three];
> }
>
> the generated code and data are the same as if you had compiled
>
> int f(const int p[])
> {
> return p[2] - p[3];
> }
>
> i.e. the enum type does not take up space in the executable
> program. It does take up space in debug information, symbol
> tables, and such; but if you are compiling for a device with so
> little memory that you need to worry about 16 bytes, then you
> probably will not install those things into the device anyway.
>
>> Do you have idea if there is another way how to overcome union to
>> accept const instead of static const?
>
> If the union does not need to be anonymous, you can do this:
>
> static const union
> {
> char str[ 5 ];
> struct Letter
> {
> char a;
> char b;
> char c;
> char d;
> } letter;
> } u = {"ABCD"};
>
> int main()
> {
> char A = u.letter.a; // read 'A' from str and put it in A variable
> char B = u.letter.b; // read 'B' from str and put it in B variable
> char C = u.letter.c; // read 'C' from str and put it in C variable
> char D = u.letter.d; // read 'D' from str and put it in D variable
> }
Strictly speaking, the point of unions is to share the memory, the
ability to store something, BUT NOT the value of it. The 2003 Standard
still allowed to read only the value that was put in the union, but
never write one part and read another. That actually has undefined
behavior. I don't know whether that was relaxed in C++11.
V
--
I do not respond to top-posted replies, please don't ask
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-24 14:54 -0800
Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-24 18:21 -0500
Re: Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-24 18:16 -0800
Re: Four variables share one string? Kalle Olavi Niemitalo <kon@iki.fi> - 2011-12-25 14:20 +0200
Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-25 08:54 -0500
Re: Four variables share one string? Leigh Johnston <leigh@i42.co.uk> - 2011-12-25 14:04 +0000
Re: Four variables share one string? Michael DOUBEZ <michael.doubez@free.fr> - 2011-12-26 02:02 -0800
Re: Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-26 07:50 -0800
Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-26 18:34 -0500
csiph-web