Is enum a suitable way to implement a "local define?"

Discussion in 'C Programming' started by partremmaps, Apr 5, 2014.

  1. partremmaps

    James Kuyper Guest

    On 04/09/2014 09:32 AM, David Brown wrote:
    File scope isn't minimal; it's the largest scope available. And "no
    linkage" is more minimal than "internal linkage".
    James Kuyper, Apr 9, 2014
    1. Advertisements

  2. partremmaps

    James Kuyper Guest

    On 04/09/2014 09:38 AM, David Brown wrote:
    I've found BartC's messages annoying, sufficiently so that I've
    considered adding him to my message filters - but I don't think "troll"
    is a correct description for the reason I'm annoyed by those messages.

    He has his own ideas about what a good programming language is like,
    which embody a very different philosophy that that used to design C. He
    is working to implement those ideas in C. None of that would be a
    problem, except that:

    * He doesn't know C well enough to do a good job of implementing his own
    language. That makes it a very odd choice, unless it's the language he
    understands best, it which case it's odd that he feels competent to
    design his own language.

    * Many of his criticisms of C reflect his ignorance of the language.

    * He tends to think of his own language design preferences as being
    superior to the conflicting preferences behind the design of C, rather
    than being simply alternative preferences. There's room in the IT world
    for many different languages with many widely different styles; his
    criticisms are seldom worded in a way that reflects awareness of that fact.

    * Most annoying, he'll frequently say things that are actually about his
    language, in contexts where any reasonable person, knowing this is
    "comp.lang.c", would expect his comments to be referring to C. Often,
    the only clue that he's not talking about C is that his comments don't
    correctly describe C - but that's unfortunately not a reliable way of
    distinguishing them from his comments that are actually intended to be
    about C.
    James Kuyper, Apr 9, 2014
    1. Advertisements

  3. I thought it was four or five, but I haven't been counting.
    But most of them don't have the meaning that "static" should

    (big snip)
    I usually use static for look-up tables, that is, initialized
    arrays. Partly that is from the K&R days where auto arrays couldn't
    have initializers.

    It probably is true on many current processors that access to
    stack (auto) data is faster than static, but then the auto
    array has to be reinitialized (copied from somewhere) on each
    entry to the function.

    Well, in other languages it is something like "external" that
    gives external linkage. I was never quite sure where the funny C
    rules on "extern" came from, as other languages don't have that rule,
    and still manage to work. Well, partly it is a side effect of
    static variables always being initialized.

    -- glen
    glen herrmannsfeldt, Apr 9, 2014
  4. The top of the stack is almost certainly in cache whereas the static
    data won't be unless you've used it recently, but OOO mechanisms on most
    "current processors" should be able to hide that difference.

    If both are in (or both out) of cache, though, access speed should be
    identical unless your lookup tables are stored in ROM or Flash, which is
    a whole 'nother matter entirely.

    Stephen Sprunk, Apr 9, 2014
  5. partremmaps

    Kaz Kylheku Guest

    BartC seems to be here to discuss the features of some other language than C,
    which he is working on, with the evident side goal of avoiding learning C as
    much as possible while proposing improvements to it.

    No concrete, complete spec of the other language is ever given, so it is
    impossible to discuss properly; this aspect keeps the discussions
    quasi-topical, allowing the focus to stay on BartC's years-long learning
    disability with regard to C.

    BartC is somewhat like a higher-level Bill Cunningham.

    (And note how the initials of both characters are B.C.!)

    There are strong parallels in the trolling theme: a person who is toying with
    some projects that don't seem to advance from year to year (as far as we can
    glean their status from the fleeting descriptions), and who exhibits
    anterograde amnesia with regard to absorbing new material from other posters.
    Kaz Kylheku, Apr 9, 2014
  6. That's largely true, but there are a few "holes" in the type system.
    You *can* modify a const object without any casts, for example like

    const int c = 0;
    int *ip = memchr(&c, 0, 1);
    *ip = 42;

    The result is still undefined, of course, but you do sometimes see bugs
    caused by the inadvertent loss of const this way.

    By the way, I agree with what you are saying overall and const vs. a new
    constant declaration. The whole trend on modern programming language
    design is towards clever compilers and expressive languages.

    Ben Bacarisse, Apr 9, 2014
  7. partremmaps

    Ian Collins Guest

    C++ had to go through a few hoops to fix this particular library bug!
    Ian Collins, Apr 9, 2014
  8. partremmaps

    Kaz Kylheku Guest

    I believe C added holes to void * when it was borrowed from C++.

    The fix is simple: don't allow conversions out of void * without a cast.

    No big hoop.
    Kaz Kylheku, Apr 10, 2014
  9. The makes the hole smaller, but the problem is that various library
    function strip the const qualifier from the returned pointer. Simply
    removing void * from the language would not be enough if functions like
    strchr still return at char * rather than a const char *. You'd only be
    able to modify a char object without a cast, but the hole would still
    not be closed.
    Ben Bacarisse, Apr 10, 2014
  10. partremmaps

    David Brown Guest

    I am aware of the technical standards-language details here, and I agree
    that the two cases can't be combined at that level. But most C
    programmers don't understand about linkage - they just know that some
    data can be accessed from other C files, and some cannot. It can be
    helpful to give such inexperienced programmers an idea that links the
    two uses of "static" - it helps familiarise them with the keyword and
    its uses. I am not suggesting it as an accurate definition.

    So if you don't like the explanation of "static" that I gave, that's
    fine. If someone relatively new to C finds it helpful in getting to
    grips with some of C's concepts and keywords, then that's fine too.
    David Brown, Apr 10, 2014
  11. But programmers also say "that variable is a constant".
    It's referenced by a symbol rather than a raw value, but the value doesn't change during the lifetime of
    the program. However it might change if the program is compiled with different settings.
    Malcolm McLean, Apr 10, 2014
  12. partremmaps

    James Kuyper Guest

    On 04/10/2014 05:03 AM, David Brown wrote:
    That's the key point of disagreement - I don't think it can be helpful.
    I think it would be far more helpful to teach them how to clearly
    distinguish the two meanings. That means you'll have to teach them not
    only about linkage and storage duration, but also about scope, since
    that's what determines which of the two meanings applies. That is a lot
    to learn for a beginner - but those three topics are also very important
    things that a beginner should learn about fairly early.
    James Kuyper, Apr 10, 2014
  13. partremmaps

    David Brown Guest

    That's true - but if you are initialising the stack table, you need to
    copy all the data from a source into the stack. The source is unlikely
    to be in cache in advance, so this copying will take some time.
    David Brown, Apr 10, 2014
  14. partremmaps

    David Brown Guest

    Maybe you are right here. Which method is the most helpful at any given
    time will vary, but I will consider your points next time the occasion
    arises. Giving people a single sort-of explanation of "static" has
    helped in the past, but as they say "past performance is no guarantee of
    future results" :)
    David Brown, Apr 10, 2014
  15. If most C programmers don't understand linkage, then they need to learn.
    I honestly fail to see how any explanation of the two meanings of
    "static" that doesn't (a) acknowledge that the two meanings are entirely
    different, and (b) explain what those two meanings are could be useful.

    Hypothetically, if C used two different keywords for the two uses, would
    you even consider trying to explain them as if they were a single
    Keith Thompson, Apr 10, 2014
  16. (snip)
    Why use words instead of random combinations of meaningless letters?
    (Well, I don't know about non-english speakers, who might consider
    them meaningless.) The words are supposed to remind us of the
    meaning and use for that keyword. If a keyword has a meaning
    unrelated to its normal meaning (or even CS meaning) that is
    confusing to users.

    -- glen
    glen herrmannsfeldt, Apr 10, 2014
  17. partremmaps

    James Kuyper Guest

    On 04/10/2014 12:36 PM, glen herrmannsfeldt wrote:
    That's precisely why using "static" to give an identifier "internal
    linkage" was a bad idea - but the deed has been done, the need to
    maintain backwards compatibility prevents fixing it, so now we have to
    deal with it.
    James Kuyper, Apr 10, 2014
  18. It's interesting to find out that C never defines the term.
    I thought that C defined it in an analogous way to how C++ defines it:

    "A /variable/ is introduced by the declaration ((of a reference
    other than a non-static data member or)) of an object.
    The variable’s name denotes the ((reference or)) object."

    excluding the irrelevant parts about references (marked with (( )) by me),
    of course. So a key feature of a variable would be a named, declared thing.
    With the definition discussed above, they are not the same;
    e.g. malloc(1) would create a char-sized object but not a variable.
    I agree that using the term "object" is sufficient in many cases.
    Seungbeom Kim, Apr 10, 2014
  19. partremmaps

    James Kuyper Guest

    More precisely, it is a named, declared object.

    Most occurrences of "variable" in the C standard are as an adjective,
    but every use of the term "variable" as a noun in the C standard is
    consistent with the C++ definition. However, most of the time the C
    standard uses "variable" in ways that would also apply to unnamed
    objects, so perhaps in C, "variable" is simply a synonym for "object"?

    I've informally adopted the C++ definition even in C contexts. I admit,
    it's a bit counterintuitive to describe something as "variable" even if
    it happens to be a constant.
    James Kuyper, Apr 10, 2014
  20. partremmaps

    Kaz Kylheku Guest

    ANSI Common Lisp uses "variable", and nonchalantly introduces the
    oxymoron "constant variable" as a normative term in its glossary:

    constant variable n. a variable, the value of which can never change; that is,
    a keyword or a named constant. ``The symbols t, nil, :direction, and
    most-positive-fixnum are constant variables.''

    Cringe-inducing abuse of English. As adjectives, "constant" and "variable" are
    diametric opposites. To be constant is not to vary.
    Kaz Kylheku, Apr 10, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.