Re: Please help optimize (and standarize) this code...

From: gtippery (gtippery_at_altavista.net)
Date: 03/12/05


Date: 11 Mar 2005 15:12:24 -0800

Mark F. Haigh wrote:

> Well, here's something shorter and more similar to what you'd
encounter
> in practice. For something like this, not many people are going to
> #define the size of the members of struct NameExt, since you can get
at
> them with sizeof. Also, many times people are going to use a
sentinel
> value to signal the end of the array rather than maintain a separate
> variable to do so.
>

I don't see any particular advantage either way on either of these
changes:
  -- See an earlier posting for another opinion on #define's vs.
casting sizeof's to int's for printf; and there may be other places
that need to use the values.
  -- A sentinel's easier if you're using pointers (or doing
comparisons), but what's its advantage if you're indexing? And that's
assuming the count's not needed for anything else, of course.

> "(i + 1) % MAXCOL" is marginally idiomatic for producing columned
> output in situations where MAXCOL is a power of two or "small enough"
> for the compiler to play fancy tricks with.
>

What kind of "tricks" did you have in mind that the compiler might do
with, say, 5 columns per line? Isn't it either going to have to divide
or to maintain an internal modulo counter (like I'm doing explicitly)?

I used a separate column counter have the column # directly available
for other purposes (not shown) and debugging. Also, there are
additional fields in each column in the working program, so putting the
column logic separately seemed easier. I _will_ see if I can use the
%s / ? combo.

The rest of your code doesn't quite do the same thing as mine - the
"input" data's formatted differently and so's the output.

If you just wanted to show something similar, that's fine, and it was
interesting. But the reason I didn't use an array of strings in the
first place is that in the real program, the data's coming from an
existing packed data structure in memory (returned by the OS), and
would have to be converted to strings to use your sentinel and printing
techniques; so as far as I can see I'd need the memcpy()'s anyway.

Then to get the right output, it needs the changes I show below to
allow for the fact that the structure members now each include '\0'.
Also, the array's larger now (by two bytes per element), but that won't
actually be a problem in my case.

>
> ---snip---
> #include <stdio.h>
>
> #define MAXCOL 2
>
> struct NameExt
> {
> char name[9];
> char ext[4];
> };
>
>
> int main(void)
> {
> /* Note sentinel at end */
> struct NameExt list[] = {
> { "BillyBob", "123" },
> { "Steve", "456" },
> { "Tom", "789" },
> { "Joe", "012" },
> { "Sal", "345" },
> { "", "" },
> };
>
> /* Print the array out */
> size_t i;

What's the reason for using size_t instead of just int? I don't see
anywhere you use i as a size.

> for(i = 0; *list[i].name; i++)

*list[i].name is the same as list[i].name[0], right? You're checking
if the first char is zero?

> printf("%-*s%-*s%s",

        printf("%-*s.%-*s%s",

> sizeof(list[i].name), list[i].name,
> sizeof(list[i].ext), list[i].ext,

            (int)sizeof(list[i].name) - 1, list[i].name,
            (int)sizeof(list[i].ext) - 1, list[i].ext,

> (i + 1) % MAXCOL ? "" : "\n");

            (i + 1) % MAXCOL ? " " : "\n");

>
> return 0;

I've been meaning to ask about that. Older references seem to imply
that main() defaults to returning 0, and I haven't seen anything newer
that says differently, but I wouldn't be surprised. I downloaded a
copy of a "late draft" of the spec, but it was a .bz and I couldn't
open it. 8(

> }
> ---snip---
>
> [mark@icepick ~]$ gcc -Wall -O2 -ansi -pedantic foo.c -o foo
> [mark@icepick ~]$ ./foo
> BillyBob 123 Steve 456
> Tom 789 Joe 012
> Sal 345
> [mark@icepick ~]$
>

Should look like this:

BillyBob.123 Steve .456
Tom .789 Joe .012
Sal .345

>
> Mark F. Haigh
> mfhaigh@sbcglobal.net



Relevant Pages

  • Code critique please
    ... struct SortOrder1: SortOrder ... bool operator()(const A &lhs, const A &rhs) ... class Mark ... void Tree::Insert ...
    (comp.lang.cpp)
  • [PATCH] mm: use-once cleanup
    ... struct page *cached_page; ... Mark a page as having seen activity. ... goto activate_locked; ...
    (Linux-Kernel)
  • Re: Fedora Core 4 Sucks! (vpn fix)
    ... > Mark O. Schlegel wrote: ... I doubt these are kernel related. ... The cisco vpn ... >> struct sk_buff { ...
    (comp.os.linux.misc)
  • Re: Fedora Core 4 Sucks! (vpn fix)
    ... Mark O. Schlegel wrote: ... I just updated to kernel 2.6.14-1.1656_FC4, and still there are numerous problems, as reported by many others. ... compile my Cisco VPN client under the last 4 kernel versions. ... I think the old struct had two 32 bit timeval or ...
    (comp.os.linux.misc)
  • Re: Structure size directives
    ... When you target a specific architecture, the alignment requirements are know apriori, so system developers do make use of explicit alignment and knows what they are doing. ... struct foo obj; ... offset of 4 bytes, and with the #pragma pack, it would be allocated at ... There are really two different cases here, you talk about the usage of "pack" to minimize padding in a struct, while I talk about the "pack " usage, which specify alignmentof struct members. ...
    (comp.lang.c)