From: "Charlie Gordon" <gmane@chqrlie.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: complex variable
Date: Wed, 8 Sep 2004 11:59:48 +0200 [thread overview]
Message-ID: <chml6d$kri$1@sea.gmane.org> (raw)
In-Reply-To: E3E30069B061524E90BCEE4417E30661138525@monm207.monmouth.army.mil
"Huber, George K RDECOM CERDEC STCD SRI" <George.K.Huber@us.army.mil> wrote
in message
news:E3E30069B061524E90BCEE4417E30661138525@monm207.monmouth.army.mil...
> #include<stdlib.h>
> #include<complex.h>
> #include<stdio.h>
> #include<errno.h>
>
> int main()
> {
> FILE* fp=NULL;
The extra initialization to NULL is useless, as JBG points out, as a few
other ones (i, in) and even some unused variables (x)
> 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.
>
> 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) {
> {
> 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.
> 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.
> {
> 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.
> }
> else
> {
> fprintf(stderr, "Read failed. error: %d\n%s\n", errno,
> strerror(errno));
> }
> }
> else
> {
> fprintf(stderr, "Failed to allocate space for buffer\n");
> }
> }
> else
> {
> fprintf(stderr, "Failed to open %s. Error %d\n%s\n",
> szName, errno, strerror(errno));
> }
>
> return 0;
> }
Cheers,
Chqrlie.
next prev parent reply other threads:[~2004-09-08 9:59 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 [this message]
2004-09-08 12:06 ` Jan-Benedict Glaw
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='chml6d$kri$1@sea.gmane.org' \
--to=gmane@chqrlie.org \
--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).