Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

God is real, unless declared integer.


devel / comp.lang.c / Re: How to check in compile time if variable is an array?

SubjectAuthor
* How to check in compile time if variable is an array?Thiago Adams
+- Re: How to check in compile time if variable is an array?Thiago Adams
+* Re: How to check in compile time if variable is an array?David Brown
|+* Re: How to check in compile time if variable is an array?Thiago Adams
||`- Re: How to check in compile time if variable is an array?Thiago Adams
|`- Re: How to check in compile time if variable is an array?David Brown
`- Re: How to check in compile time if variable is an array?Keith Thompson

1
How to check in compile time if variable is an array?

<472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16165&group=comp.lang.c#16165

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:205:: with SMTP id b5mr9673642qtx.186.1620398039409;
Fri, 07 May 2021 07:33:59 -0700 (PDT)
X-Received: by 2002:ac8:5e54:: with SMTP id i20mr9861539qtx.151.1620398039193;
Fri, 07 May 2021 07:33:59 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 7 May 2021 07:33:58 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
Subject: How to check in compile time if variable is an array?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 07 May 2021 14:33:59 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Fri, 7 May 2021 14:33 UTC

I want to add some safety checks for "not null" for
this I would like to have a macro that checks if
some variable is an array and not a pointer.

#define IS_ARRAY(A) _Static_assert(sizeof(A)/sizeof(A[0]) > 1, "not array")

int main() {

char a[] = "abc";
char* b = "123";

IS_ARRAY(a); //OK
IS_ARRAY(b); //SHOULD FAIL
}

any ideas?

Or check if is pointer.. also helps.

Re: How to check in compile time if variable is an array?

<3bd71ef5-48e6-4312-9519-7623d769204an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16166&group=comp.lang.c#16166

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:88:: with SMTP id o8mr10101716qtw.39.1620400454881;
Fri, 07 May 2021 08:14:14 -0700 (PDT)
X-Received: by 2002:ac8:7fc7:: with SMTP id b7mr9949962qtk.109.1620400454556;
Fri, 07 May 2021 08:14:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 7 May 2021 08:14:14 -0700 (PDT)
In-Reply-To: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3bd71ef5-48e6-4312-9519-7623d769204an@googlegroups.com>
Subject: Re: How to check in compile time if variable is an array?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 07 May 2021 15:14:14 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Fri, 7 May 2021 15:14 UTC

On Friday, May 7, 2021 at 11:34:09 AM UTC-3, Thiago Adams wrote:
> I want to add some safety checks for "not null" for
> this I would like to have a macro that checks if
> some variable is an array and not a pointer.

Something I would like to emulate is the check for

void f(char s[static 5]) { }

In this case I think I could use:

void f(char s[5]) {
printf("%s", s);
}

#define F(S) _Static_assert(sizeof(S)/sizeof(S[0]) >= 5, "too small"); f(S)

int main() {

char a[] = "abcde";
//char* b = "123";
F(a);
}

But also the check for null would be for something like:

void F(struct X* p);

Re: How to check in compile time if variable is an array?

<s73rim$9ck$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16170&group=comp.lang.c#16170

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How to check in compile time if variable is an array?
Date: Fri, 7 May 2021 18:57:57 +0200
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <s73rim$9ck$1@dont-email.me>
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 7 May 2021 16:57:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="07b816242612234a95d12ddf8d0ab2d7";
logging-data="9620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1834nA4lMK+2aS6TB+BLPE1pxla0tdjlJE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:sevag4W4UtRR968/kYyiYkVlCEg=
In-Reply-To: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Fri, 7 May 2021 16:57 UTC

On 07/05/2021 16:33, Thiago Adams wrote:
> I want to add some safety checks for "not null" for
> this I would like to have a macro that checks if
> some variable is an array and not a pointer.
>
>
> #define IS_ARRAY(A) _Static_assert(sizeof(A)/sizeof(A[0]) > 1, "not array")
>
> int main() {
>
> char a[] = "abc";
> char* b = "123";
>
> IS_ARRAY(a); //OK
> IS_ARRAY(b); //SHOULD FAIL
> }
>
> any ideas?
>
> Or check if is pointer.. also helps.
>

I am fairly sure there is a macro of this sort used in the Linux kernel,
but I am afraid I can't remember what it is, and I don't have the time
to look for it just now. But with that hint and a bit of googling,
maybe you'll find it. (Of course, it could be that it uses "typeof" or
some other gcc extension.)

Re: How to check in compile time if variable is an array?

<0630ba6e-75fc-4a18-969e-20e6893258f3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16174&group=comp.lang.c#16174

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:63d2:: with SMTP id x201mr11235680qkb.202.1620410782686;
Fri, 07 May 2021 11:06:22 -0700 (PDT)
X-Received: by 2002:a05:622a:1186:: with SMTP id m6mr10308254qtk.319.1620410782340;
Fri, 07 May 2021 11:06:22 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 7 May 2021 11:06:22 -0700 (PDT)
In-Reply-To: <s73rim$9ck$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com> <s73rim$9ck$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0630ba6e-75fc-4a18-969e-20e6893258f3n@googlegroups.com>
Subject: Re: How to check in compile time if variable is an array?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 07 May 2021 18:06:22 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Fri, 7 May 2021 18:06 UTC

On Friday, May 7, 2021 at 1:58:09 PM UTC-3, David Brown wrote:
> On 07/05/2021 16:33, Thiago Adams wrote:
> > I want to add some safety checks for "not null" for
> > this I would like to have a macro that checks if
> > some variable is an array and not a pointer.
> >
> >
> > #define IS_ARRAY(A) _Static_assert(sizeof(A)/sizeof(A[0]) > 1, "not array")
> >
> > int main() {
> >
> > char a[] = "abc";
> > char* b = "123";
> >
> > IS_ARRAY(a); //OK
> > IS_ARRAY(b); //SHOULD FAIL
> > }
> >
> > any ideas?
> >
> > Or check if is pointer.. also helps.
> >
> I am fairly sure there is a macro of this sort used in the Linux kernel,
> but I am afraid I can't remember what it is, and I don't have the time
> to look for it just now. But with that hint and a bit of googling,
> maybe you'll find it. (Of course, it could be that it uses "typeof" or
> some other gcc extension.)

I found some links and I create this

#define IS_POINTER(P) (void (*)(void**)){0}(&(P))

int main() {

char x[10];
IS_POINTER(x);//warning as expected
//warning C4047: 'function': 'void **' differs in levels of indirection from 'char (*)[10]'

char* p;
IS_POINTER(p);//OK
}

This macros gives us a warning if P is not a pointer.
Taking the address of a variable if it is a pointers it gives **.

no success for array yet...

Re: How to check in compile time if variable is an array?

<56e0f8e3-58cb-491d-8434-9af2f03e16c9n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16175&group=comp.lang.c#16175

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:ee23:: with SMTP id l3mr11446611qvs.55.1620411926210;
Fri, 07 May 2021 11:25:26 -0700 (PDT)
X-Received: by 2002:a37:a854:: with SMTP id r81mr10576749qke.83.1620411925998;
Fri, 07 May 2021 11:25:25 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 7 May 2021 11:25:25 -0700 (PDT)
In-Reply-To: <0630ba6e-75fc-4a18-969e-20e6893258f3n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
<s73rim$9ck$1@dont-email.me> <0630ba6e-75fc-4a18-969e-20e6893258f3n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <56e0f8e3-58cb-491d-8434-9af2f03e16c9n@googlegroups.com>
Subject: Re: How to check in compile time if variable is an array?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 07 May 2021 18:25:26 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Fri, 7 May 2021 18:25 UTC

#define IS_ARRAY(P) _Static_assert(sizeof(P) > sizeof(void*) || sizeof(P)/sizeof(P[0]) < 4, "not array")

int main() {
char x[] = "ghdd";
IS_ARRAY(x);

char* p;
IS_ARRAY(p);
}

This fails only if array has exactly 4 elements

Re: How to check in compile time if variable is an array?

<87v97u8duz.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16180&group=comp.lang.c#16180

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to check in compile time if variable is an array?
Date: Fri, 07 May 2021 13:53:40 -0700
Organization: None to speak of
Lines: 35
Message-ID: <87v97u8duz.fsf@nosuchdomain.example.com>
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="139435df6a8282e929ed4f0d9cf9f4c9";
logging-data="9172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vD3g5hHiE1LNEa5ohQrEp"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux)
Cancel-Lock: sha1:dZNRu6p9stqBqK/IIMJ9PEES89E=
sha1:ZG196JYgY3N7QuEJrO51NWTDXGk=
 by: Keith Thompson - Fri, 7 May 2021 20:53 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> I want to add some safety checks for "not null" for
> this I would like to have a macro that checks if
> some variable is an array and not a pointer.
>
>
> #define IS_ARRAY(A) _Static_assert(sizeof(A)/sizeof(A[0]) > 1, "not array")
>
> int main() {
>
> char a[] = "abc";
> char* b = "123";
>
> IS_ARRAY(a); //OK
> IS_ARRAY(b); //SHOULD FAIL
> }
>
> any ideas?
>
> Or check if is pointer.. also helps.

The condition is incorrect.
sizeof(a) is 4; sizeof(a[0]) is 1.
sizeof(b) is likely 8 (sizeof(char*)); sizeof(b[0]) is 1, so the
condition is true even though b is a pointer.

And if p is pointer, sizeof(p) could be equal to sizeof(p[0]) by coincidence.

(And you'd want to parenthesize all occurrences of A in the macro
definition.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to check in compile time if variable is an array?

<s75tgo$6p3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16193&group=comp.lang.c#16193

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How to check in compile time if variable is an array?
Date: Sat, 8 May 2021 13:43:19 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <s75tgo$6p3$1@dont-email.me>
References: <472f2af6-abc4-439b-a9de-75a01ef47d8fn@googlegroups.com>
<s73rim$9ck$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 8 May 2021 11:43:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7720917587a734d9308436692dbd21a5";
logging-data="6947"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FfiRBhD1UzCPEXOJXquNvShX+1fVPeyI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:3Z4R8uGp48bFv8rPiiLqDzRgRAY=
In-Reply-To: <s73rim$9ck$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 8 May 2021 11:43 UTC

On 07/05/2021 18:57, David Brown wrote:
> On 07/05/2021 16:33, Thiago Adams wrote:
>> I want to add some safety checks for "not null" for
>> this I would like to have a macro that checks if
>> some variable is an array and not a pointer.
>>
>>
>> #define IS_ARRAY(A) _Static_assert(sizeof(A)/sizeof(A[0]) > 1, "not array")
>>
>> int main() {
>>
>> char a[] = "abc";
>> char* b = "123";
>>
>> IS_ARRAY(a); //OK
>> IS_ARRAY(b); //SHOULD FAIL
>> }
>>
>> any ideas?
>>
>> Or check if is pointer.. also helps.
>>
>
>
> I am fairly sure there is a macro of this sort used in the Linux kernel,
> but I am afraid I can't remember what it is, and I don't have the time
> to look for it just now. But with that hint and a bit of googling,
> maybe you'll find it. (Of course, it could be that it uses "typeof" or
> some other gcc extension.)
>

These are from Zephyr, and are probably the same as the Linux kernel.
It turns out it uses typeof /and/ another gcc extension.

/** @brief 0 if @p cond is true-ish; causes a compile error otherwise. */
#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)])
- 1)

#define IS_ARRAY(array) \
ZERO_OR_COMPILE_ERROR( \
!__builtin_types_compatible_p(__typeof__(array), \
__typeof__(&(array)[0])))

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor