Re: [dwm] malloc'ed client in manage()

From: Premysl Hruby <dfenze_AT_gmail.com>
Date: Tue, 9 Sep 2008 10:15:53 +0200

On (09/09/08 09:13), Szabolcs Nagy wrote:
> To: dwm mail list <dwm_AT_suckless.org>
> From: Szabolcs Nagy <nszabolcs_AT_gmail.com>
> Subject: Re: [dwm] malloc'ed client in manage()
> Reply-To: dwm mail list <dwm_AT_suckless.org>
> List-Id: dwm mail list <dwm.suckless.org>
>
> On 9/8/08, Anselm R Garbe <garbeam_AT_gmail.com> wrote:
> >> but if we really care about obscure 30 year old cpus (other than x86
> >> :)) then i'd go with my solution: c = malloc(); and *c = (Client){};
> >
> > Well, I agree on this proposal and go for it. It is fairly simple and
> > nice looking imho.
>
> sorry i misread the standard
> eventhough it sais
>
> "If there are fewer initializers in a brace-enclosed list than there
> are elements or members of an aggregate, or fewer characters in a
> string literal used to initialize an array of known size than there
> are elements in the array, the remainder of the aggregate shall be
> initialized implicitly the same as objects that have static storage
> duration."
>
> the initializer list must contain at least one element so the empty {}
> is not enough. (easy to overlook since it is not stated explicitly,
> just the result of the given initializer grammar)
>
> Client can be initialized with {""} (first member is .name) or {.x =
> 0} or whatever, but general zero-init is impossible this way :(
>
> tl;dr: use the original patch
>

This can be easilly done with patch:

diff -r e4bcaca8e6ef dwm.c
--- a/dwm.c Mon Sep 08 22:24:05 2008 +0100
+++ b/dwm.c Tue Sep 09 10:12:01 2008 +0200
@@ -846,22 +846,21 @@
 
 void
 manage(Window w, XWindowAttributes *wa) {
- static Client cz;
         Client *c, *t = NULL;
         Window trans = None;
         XWindowChanges wc;
 
         if(!(c = malloc(sizeof(Client))))
                 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
- *c = cz;
- c->win = w;
+ *c = (const Client){
+ .win = w,
+ .x = wa->x,
+ .y = wa->y,
+ .w = wa->width,
+ .h = wa->height,
+ .oldbw = wa->border_width,
+ };
 
- /* geometry */
- c->x = wa->x;
- c->y = wa->y;
- c->w = wa->width;
- c->h = wa->height;
- c->oldbw = wa->border_width;
         if(c->w == sw && c->h == sh) {
                 c->x = sx;
                 c->y = sy;

But, I dislike this whole !calloc solution. BTW: there are plenty of places
which expects that NULL == 0 -> false ...

-- 
Premysl "Anydot" Hruby, http://www.redrum.cz/
Received on Tue Sep 09 2008 - 08:15:53 UTC

This archive was generated by hypermail 2.2.0 : Tue Sep 09 2008 - 08:24:09 UTC