Mail Archives: djgpp/2011/08/27/17:15:16
X-Authentication-Warning: | delorie.com: mail set sender to djgpp-bounces using -f
|
From: | Rugxulo <rugxulo AT gmail DOT com>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Array initialization question
|
Date: | Sat, 27 Aug 2011 13:11:23 -0700 (PDT)
|
Organization: | http://groups.google.com
|
Lines: | 87
|
Message-ID: | <f773923a-afb3-49d8-a6ac-d0477ee283f2@u26g2000yqu.googlegroups.com>
|
References: | <j393ip$q9n$1 AT news DOT albasani DOT net> <j3a54s$uc9$1 AT speranza DOT aioe DOT org> <j3a807$aj4$1 AT news DOT albasani DOT net>
|
NNTP-Posting-Host: | 65.13.115.246
|
Mime-Version: | 1.0
|
X-Trace: | posting.google.com 1314478469 6090 127.0.0.1 (27 Aug 2011 20:54:29 GMT)
|
X-Complaints-To: | groups-abuse AT google DOT com
|
NNTP-Posting-Date: | Sat, 27 Aug 2011 20:54:29 +0000 (UTC)
|
Complaints-To: | groups-abuse AT google DOT com
|
Injection-Info: | u26g2000yqu.googlegroups.com; posting-host=65.13.115.246; posting-account=p5rsXQoAAAB8KPnVlgg9E_vlm2dvVhfO
|
User-Agent: | G2/1.0
|
X-Google-Web-Client: | true
|
X-Google-Header-Order: | HUALESNKRC
|
X-HTTP-UserAgent: | Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0,gzip(gfe)
|
Bytes: | 4412
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
X-MIME-Autoconverted: | from quoted-printable to 8bit by delorie.com id p7RLF1Gc014868
|
Reply-To: | djgpp AT delorie DOT com
|
Hi again,
On Aug 27, 2:58 am, Mok-Kong Shen <mok-kong DOT s DOT DOT DOT AT t-online DOT de> wrote:
> Am 27.08.2011 09:08, schrieb Rod Pemberton:
>
> > "Mok-Kong Shen"<mok-kong DOT s DOT DOT DOT AT t-online DOT de> wrote:
> >> However I can't have an initialization list
> >> e.g.
>
> >> int arr[sz]={ 0,1,2 };
>
> > Why would you need a variable-length array with a fixed quantity of array
> > items? Does that make sense to you?
>
> > Is there something wrong with:
>
> > int arr[3]={0,1,2};
>
> My C knowledge is very meagre.
It's all very confusing, I admit. I don't claim to have a good grasp
on it either.
> Since I have
>
> const int sz=3;
>
> doesn't this mean that the array "int arr[sz]" has a constant, hence
> fixed, size?
Apparently not. "#define" is traditionally used for compile time
constant. "const" (unlike traditional Pascal declaration) is a
variable qualifier which just means "read-only", and it's still a
runtime variable allocated on the stack (see "gcc -S -masm=intel",
which for me says "mov DWORD PTR [ebp-4], 3"). The only difference
(vs. non-const, normal variable) is that you can't modify it. K&R C
didn't have "const", only ANSI (C89), presumably as a hint to
optimization or stricter checking to avoid errors.
But you're not totally wrong in theory, of course. GNU Pascal (also
optionally available for DJGPP) with Extended Pascal (ISO 10206)
supports such non-constant sized things (at runtime) with schemas, for
both types and variables, even with initialization ("value",
"otherwise").
http://www.gnu-pascal.de/gpc/Schema-Types.html
http://www.prosperosoftware.com/EPIntro.html
Even REXX (ahem, Regina for DJGPP) has 'blah. = 5' to make the default
stem value 5 for all members of the array.
#!/usr/bin/env rexx
blah. = 1
blah.3 = 3
blah.6 = 6
do j=10 to 1 by -1
call charout ,blah.j ; call charout ,' '
end
say
/* /* EOF */ */
1 1 1 1 6 1 1 3 1 1
> Certainly not shown in my tiny example, my original
> intention was not to provide a complete intialization list, but only
> a certain fixed leftmost portion, with the rest to be done via code.
You're not wrong, and I see what you're saying, but I can't honestly
understand which solution is what you want (or even what specifically
you're trying to do here: save memory? faster runtime speed? syntactic
sugar? runtime allocation?).
VLAs are claimed to be similar to alloca, which DJGPP (thanks to GNU)
also supports (stdlib.h __builtin_alloca). But honestly it may be
better to just take the easy way out and explicitly use malloc and
standard manual initialization.
> (The leftmost portion has certain characteristics not shared by the
> rest of the elements of the array, so it is convenient to specify them
> individually in the initialization list.)
If your runtime array is big but you don't want to waste space with
compile-time initialization, maybe?? you should just define a small
(compile-time) initialized array with the defaults, (string.h)
"memset" the (runtime) big array with 0, and "memcpy" the small array
to the bigger (runtime) array. (Or am I way off base?)
- Raw text -