delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/26/17:47:48

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Trouble with bools
Date: Thu, 23 Oct 1997 20:33:42 -0700
Organization: Alcyone Systems
Lines: 66
Message-ID: <34501716.3AE74872@alcyone.com>
References: <199710220357 DOT NAA14199 AT rabble DOT uow DOT edu DOT au>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Brett Porter wrote:

> > bool scrn[640][480] = {0};
>
> Are you sure? I don't ever recall seeing this done.

This is correct.  ANSI C dictates that remaining members are
automatically initialized with all-bits-zero.  Note that this makes the
following code nonportable:

    void *p[10] = { 0 };

p[0] is a null pointer, but the others are initialized with an
all-bits-zero address, which is not necessarily the null pointer.

> > bool foo[2][3] = { 0, 1, 0, 1, 0, 1};
>
> I'm almost certain this won't work, I think it should be
> {{0,1},{0,1},{0,1}}

No, this is legal as well.  You can leave off higher level bracing.

> > bool foo[2][3] = {1};
> > 
> > only sets the first item of the array to 1 but the rest to 0.  As
> > for how
> > efficent (sp) this is, I don't know.

It is done at compile time, so inefficiency is irrelevant.

Writing 

    int a[4] = { 1 };

does _precisely the same thing as 

    int a[4] = { 1, 0, 0, 0 };

> > There could very well be some
> > un
> > desireable affects to this (which I would love to know myself)  but
> > it
> > works.  

The only disadvantage with it is that you can't use it to initialize
pointers to null pointers in the same way, since ANSI C does not
guarantee that the all-bits-zero address is the null pointer.

> This is not really a standard as mar as I know.

It's actually perfectly standard.

> What is wrong with
> just
> memset(foo, 0, sizeof(foo)); ?

It does precisely the same thing, but it is done at runtime, rather than
at compile time.

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                     \
  "After each war there is a little / less democracy to save."
                                   / Brooks Atkinson

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019