Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23386

Re: Taking address of struct temporary

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: Taking address of struct temporary
Date 2012-07-18 22:31 -0400
Organization A noiseless patient Spider
Message-ID <ju7rie$83q$1@dont-email.me> (permalink)
References <73b21861-a104-4d94-8d44-747adbfbb7b3@t1g2000pbl.googlegroups.com>

Show all headers | View raw


On 7/18/2012 9:58 PM, nroberts wrote:
> I've been playing around with an object library and have run into an
> inconsistency between using it under a C compiler and using it under a
> C++ compiler.

     This is about as surprising as if you'd found an inconsistency
when running the same code through a COBOL compiler.  C and C++ are
different languages.  They are similar in many points, but the only
purpose of those similarities is to entrap the unwary.

> I wanted a way to declare "objects" statically, meaning they exist as
> constants.  I did something like the following:

     "Something like."  O frabjous day.  O diem praeclarum.  Whoopee.

> struct Object {
>    struct Class const* isa;
> };
>
> struct String_ {
>    struct Object const _;
>    char const* buf;
>    int len;

     Aside: `size_t' might be better here.

> };
>
> extern void* __X_STR;

     Reserved identifier.  If the implementation attaches some
unexpected meaning to it (and no, it's not required to document
the fact), that's your tough luck for infringing its name space.

> #define xstr(X) &(struct String_){ { __X_STR }, X, sizeof(X) };

     You probably don't want the semicolon.

> The '__X_STR' variable is defined statically in a C file.

     Bad idea, as mentioned above.

> Later I can do the following:
>
> void* s = xstr("Hello World!);

     Missing a quotation mark.

> This works fine under gcc but pitches a fit under g++ about taking the
> address of a temporary.

     Well, no: "this" does not "work fine under gcc" because gcc
won't even compile it.  "Something derivable from this" may be
working for you -- but if I've imagined a different "something"
than what you're actually using, my advice may do you little good.
Again, that's your tough luck.

> In C++ the expression (struct String_)... is in fact a temporary.  I
> expected, but do not know, that in C this would be an object in static
> memory.  Thus in C++ I'm doing something illegal but in C maybe it's
> perfectly fine.
>
> Is this correct?

     It depends on where your `xstr' appears.  If it's outside a
function the literal has static storage duration; inside, it has
automatic duration and vanishes when the enclosing block exits.
See 6.5.2.5p5.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Taking address of struct temporary nroberts <roberts.noah@gmail.com> - 2012-07-18 18:58 -0700
  Re: Taking address of struct temporary Keith Thompson <kst-u@mib.org> - 2012-07-18 19:27 -0700
    Re: Taking address of struct temporary nroberts <roberts.noah@gmail.com> - 2012-07-19 08:54 -0700
  Re: Taking address of struct temporary Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-18 22:31 -0400
  Re: Taking address of struct temporary Ike Naar <ike@sverige.freeshell.org> - 2012-07-19 06:31 +0000
    Re: Taking address of struct temporary nroberts <roberts.noah@gmail.com> - 2012-07-19 08:57 -0700

csiph-web