|
From: | Ivan Medoedov |
Subject: | Re: [Tinycc-devel] { .field = value } struct initialization alternative |
Date: | Sun, 21 Jul 2019 21:37:40 +0200 |
Hi,
On Sun, 21 Jul 2019, Ivan Medoedov wrote:
> Typo:
>
> Foo* foo = ALLOC_INIT(Foo, { .field = 123, .field2 = 456 });
Next time, please be specific in your bug reports, it should contain a
self-contained example of the problem you're seeing. With the info
available I constructed the example below, and it works just fine.
Then I realized that maybe you indeed mean GNU designated initializers and
the runtime behaviour instead of not being able to compile something. I
was able to generate a segfault at runtime when using the GNU form (i.e.
'{ foo: val }'). That's now fixed in mob.
% cat alloc-init.c
typedef __SIZE_TYPE__ size_t;
struct S {
int a, b;
};
extern void * malloc(size_t);
extern void * memcpy(void *, const void *, size_t);
extern int printf(const char *, ...);
void *memdup(const void *src, size_t sz);
#define ALLOC_INIT(type, ...) \
(type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))
int main()
{
struct S *ps = ALLOC_INIT(struct S, {.a = 3, .b = 4});
printf("%d %d\n", ps->a, ps->b);
return 0;
}
void *memdup(const void *src, size_t sz) {
void *mem = malloc(sz);
return mem ? memcpy(mem, src, sz) : 0;
}
Ciao,
Michael.
>
>
> On Sun, Jul 21, 2019 at 7:56 PM Ivan Medoedov <address@hidden>
> wrote:
> Indeed. Sorry, my bad.
> Looks like the error is caused by __VA_ARGS__
>
> I'm using the following macro:
>
> #define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ },
> sizeof(type))
>
> https://tia.mat.br/posts/2015/05/01/initializing_a_heap_allocated_structure
> _in_c.html
>
> Foo* foo = ALLOC_INIT(Foo, { field: 123, field2: 456 });
>
> error: field expected
>
> This macro works fine with GCC and Clang.
>
>
> On Sun, Jul 21, 2019 at 6:50 PM Michael Matz <address@hidden>
> wrote:
> Hi,
>
> On Sun, 21 Jul 2019, Ivan Medoedov wrote:
>
> > Hello,
> > { .field = value } syntax is not supported by TCC.
>
> Why do you think so?
>
> % cat designated-init.c
> struct S {
> int a, b;
> };
>
> struct S s = {.a = 2, .b = 3};
> % ./tcc -c designated-init.c && echo ok
> ok
> %
>
>
> Ciao,
> Michael.
>
> _______________________________________________
> Tinycc-devel mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>
>
>_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
[Prev in Thread] | Current Thread | [Next in Thread] |