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


Groups > comp.lang.c > #23385

Re: Taking address of struct temporary

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Taking address of struct temporary
Date 2012-07-18 19:27 -0700
Organization None to speak of
Message-ID <lnk3y0bi3h.fsf@nuthaus.mib.org> (permalink)
References <73b21861-a104-4d94-8d44-747adbfbb7b3@t1g2000pbl.googlegroups.com>

Show all headers | View raw


nroberts <roberts.noah@gmail.com> writes:
> 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.
>
> I wanted a way to declare "objects" statically, meaning they exist as
> constants.  I did something like the following:
>
> struct Object {
>   struct Class const* isa;
> };
>
> struct String_ {
>   struct Object const _;
>   char const* buf;
>   int len;
> };
>
> extern void* __X_STR;
>
> #define xstr(X) &(struct String_){ { __X_STR }, X, sizeof(X) };
>
> The '__X_STR' variable is defined statically in a C file.
>
> Later I can do the following:
>
> void* s = xstr("Hello World!);
>
> This works fine under gcc but pitches a fit under g++ about taking the
> address of a temporary.
>
> 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?

First of all, avoid creating identifiers that start with an underscore;
most of them are reserved to the implementation. (There's a bit more to
it than that, but "never create identifiers starting with _" is an
easier rule to remember.)

In C, 

    (struct String_){ ... }

is a compound literal. As C11 6.5.2.5p5 says (C99 is similar, if not
identical):

    The value of the compound literal is that of an unnamed object
    initialized by the initializer list. If the compound literal
    occurs outside the body of a function, the object has static
    storage duration; otherwise, it has automatic storage duration
    associated with the enclosing block.

I don't think it's referred to as a *temporary*, but there is some
danger of letting a pointer to the object escape the object's scope.

<OT>
C++, even as of the new 2011 ISO standard, does not support compound
literals. They appear to be a g++ extension. You'll have to consult
the gcc documentation to find out how they behave.
</OT>

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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