From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: complex variable
Date: Wed, 8 Sep 2004 14:06:58 +0200 [thread overview]
Message-ID: <20040908120658.GZ6985@lug-owl.de> (raw)
In-Reply-To: <chml6d$kri$1@sea.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 3064 bytes --]
On Wed, 2004-09-08 11:59:48 +0200, Charlie Gordon <gmane@chqrlie.org>
wrote in message <chml6d$kri$1@sea.gmane.org>:
> > char szName[] = "test_data.dat";
>
> This is quite inefficient : declaring a local initialized array will produce
> equivalent code to:
>
> char szName[sizeof("test_data.dat")];
> memcpy(szName, "test_data.dat", sizeof("test_data.dat"));
>
> Whereas declaring a const char* fileName = "test_data.dat" will likely get
> optimized by the compiler as not even storing the address into a local
> variable.
But remember there's a subtle difference between
char filename[] = "/path/to/file";
and
char *filename = "/path/to/file";
You may write to the first one (eg. later on use snprintf and friends
with it), but you may not write to the 2nd one!
> > if(NULL != (fp = fopen(szName, "rb")))
>
> This style is really ugly! there is really no advantage at combining the
> assignment and the test:
> fp = fopen(fileName, "rb");
> if (fp != NULL) {
That's somewhat personal style. Also notice that he's got the NULL at
the beginnnig of the comparison, not at the end. That's something that
seems to be tought these days, because assignments (if you mis-write
"!=" as "=!" or "|=") will produce compile-time errors (NULL isn't a
lvalue).
> > {
> > int N=32, i=0;
> > complex x, *in = NULL;
>
> Are you sure about the complex type implementation ?
> Are the real and imaginary parts float, double, of even long double ?
> Reading them in bulk from the file is definitely not advisable, because of
> this and because of byte ordering issues.
For now, ignore the fact that in[] is wrongly accessed. It'll somewhat
work (while being non-portable), I guess this code is here to only have
a useful test program :-)
> > if(NULL != (in=malloc(sizeof(complex)*N)))
>
> N as the uppercase name implies seems to be a constant in this code...
> why not make it a const outside this block and declare in as a local array
> instead of allocating it off the heap.
A #define would have served, too, as well as a const int. Test code
*needs* to be a bit ugliysh to qualify as test code :-)
> > {
> > if(N == fread(in, sizeof(complex), N, fp))
> > {
> > for(i=0; i< N; i++)
> > printf("%lf\t", in[i]);
>
> What does this mean ?
> C99 makes no difference between %f and %lf
> I'm not sure what passing a complex to printf will actually do. Most lilely
> not what you want and definitely not portable.
Works for now, see the ugliness clause above. (Sure, you don't want that
code in your program's final version, though...)
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
next prev parent reply other threads:[~2004-09-08 12:06 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-07 15:37 complex variable Huber, George K RDECOM CERDEC STCD SRI
2004-09-07 16:05 ` Jan-Benedict Glaw
2004-09-08 9:59 ` Charlie Gordon
2004-09-08 12:06 ` Jan-Benedict Glaw [this message]
2004-09-08 16:25 ` Ankit Jain
2004-09-08 21:54 ` Charlie Gordon
2004-09-10 20:05 ` Jan-Benedict Glaw
2004-09-13 19:32 ` Charlie Gordon
2004-09-13 21:57 ` Jan-Benedict Glaw
2004-09-14 8:16 ` Charlie Gordon
-- strict thread matches above, loose matches on Subject: below --
2004-09-08 18:40 Huber, George K RDECOM CERDEC STCD SRI
2004-09-10 20:09 ` Jan-Benedict Glaw
[not found] <20040906134525.33692.qmail@web52908.mail.yahoo.com>
2004-09-06 16:25 ` Ankit Jain
2004-09-06 16:37 ` Jan-Benedict Glaw
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040908120658.GZ6985@lug-owl.de \
--to=jbglaw@lug-owl.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).