ROM Qualifier vs Const?

This is a discussion on ROM Qualifier vs Const? within the C Programming forums, part of the General Programming Boards category; A friend asked me what rom meant, when used as a qualifier, I did not know so I looked into ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    ROM Qualifier vs Const?

    A friend asked me what rom meant, when used as a qualifier, I did not know so I looked into it. I gathered, from what little documentation exists about this qualifier, that it simply puts it into the "rom" (program space) rather than the data space ("RAM") as a standard variable would be.

    One question, why?

    Why would you want something to be in the program space, surely this doesn't make it persistent across instances, does it?

    Why would you want to do:
    Code:
    rom int = 5;
    In which it's in program space and can only be read opposed to:

    Code:
    const int = 5;
    In which it's in the data space and only can be read..which goes further, since you can pack qualifiers onto a var declaration, to say, what happens if you do:

    Code:
    const rom int = 5;
    It's already const by definition of the "rom" qualifier, and it's more or less "rom" by definition of the const qualifier (sure it's in the data segment, but it's still "read only").

    Information on this is scarce as ........ for some reason, most keyword, reserved word, or qualifier lists (as I checked all of them I could find) don't even list them, and some that do don't have any info for them (it's just a blank).

    Anybody know what I'm missing here?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    rom is not a C keyword. Perhaps your friend saw someone do something like

    Code:
    #define rom const
    where every instance of "rom" is replaced with "const" by the preprocessor.

    At any rate, making the code const does not necessarily mean it is read only. By using the const qualifier, you make it so that you cannot change the contents of the variable with the particular variable name associated with the const qualifier. You may quite easily change the contents of the variable by using a different identifier though.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by kermit View Post
    rom is not a C keyword. Perhaps your friend saw someone do something like

    Code:
    #define rom const
    where every instance of "rom" is replaced with "const" by the preprocessor.
    I suppose it's possible, he didn't give me the usage, but I've seen some samples of it out there, meaning it is used...and if there's a pre-processor directive for it, that means it IS being used for SOMETHING, even if it's just replacing it with const. There's a reason they'd change the name, there's a reason somebody would use that name.

    C doesn't have many (any?) keywords, they're all reserved, I believe.

    Quote Originally Posted by kermit View Post
    At any rate, making the code const does not necessarily mean it is read only. By using the const qualifier, you make it so that you cannot change the contents of the variable with the particular variable name associated with the const qualifier. You may quite easily change the contents of the variable by using a different identifier though.
    So you're saying if you made a pointer to it and then dereferenced it you could change the value? I mean I suppose there's no stopping you there...and there's no pass by ref in C so you'd most likely be using a pointer, in which all bets of protecting anything are off...so are you saying if you did something like:
    Code:
    cont int c = 5;
    int b = c;
    c = 10; // invalid - won't compile -- assume removed
    b = 10; // valid, won't complain
    printf("%d\n", c);
    printf("%d\n", b);
    
    --------------------------------------
    
    prints:
    10
    10
    Will work?

    I don't know, I don't use const, to me it's an embarrassment of the language.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Syndacate View Post
    I suppose it's possible, he didn't give me the usage, but I've seen some samples of it out there, meaning it is used...and if there's a pre-processor directive for it, that means it IS being used for SOMETHING, even if it's just replacing it with const. There's a reason they'd change the name, there's a reason somebody would use that name.
    For my part, it was merely a semi-educated guess at the use of the word "rom." Never used it myself, and I am not likely to do so. ISO C does not have that keyword.

    Quote Originally Posted by Syndacate View Post
    C doesn't have many (any?) keywords, they're all reserved, I believe.
    ?? C has as many keywords as specified by the standard. They are reserved in the sense that you may not use them as identifiers for anything else, e.g, :

    Code:
    int var1; /* OK */
    int char; /* Not OK */
    Quote Originally Posted by Syndacate View Post
    So you're saying if you made a pointer to it and then dereferenced it you could change the value? I mean I suppose there's no stopping you there...and there's no pass by ref in C so you'd most likely be using a pointer, in which all bets of protecting anything are off...so are you saying if you did something like:
    Code:
    cont int c = 5;
    int b = c;
    c = 10; // invalid - won't compile -- assume removed
    b = 10; // valid, won't complain
    printf("%d\n", c);
    printf("%d\n", b);
    
    --------------------------------------
    
    prints:
    10
    10
    Will work?

    I don't know, I don't use const, to me it's an embarrassment of the language.
    I was thinking of something more like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const int i = 5;
        int *p = &i;
    
        i = 4      /* !! Wrong.   Cannot use identifier i to change contents of variable. */
        *p = 4;    /* We _can_ use any other identifier that points to the same place. */
    
        printf("The value of i is %d\n", i);
    
        return 0;
    }
    Now the compiler will complain when you try to compile, unless of course you do something evil, like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const int i = 5;
        int *p = (int *) &i;
    
        *p = 4;
    
        printf("The value of i is %d\n", i);
    
        return 0;
    }
    You can crank up the warnings, and the compiler will not give so much as a peep.
    Last edited by kermit; 10-04-2010 at 06:32 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kermit View Post
    rom is not a C keyword. Perhaps your friend saw someone do something like

    Code:
    #define rom const
    where every instance of "rom" is replaced with "const" by the preprocessor.

    At any rate, making the code const does not necessarily mean it is read only. By using the const qualifier, you make it so that you cannot change the contents of the variable with the particular variable name associated with the const qualifier. You may quite easily change the contents of the variable by using a different identifier though.
    I've done stuff like this at times. Mostly it's to remind me that something is special in some way.

    Aliasing const as rom might be because the variables pointed to were in a chip and the programmer wanted a reminder.

    Other than that... it's a distinction without a difference.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    It´s more likely that you have a vendor specific C compiler which has a number of additional keywords for supporting programming of some embedded device.

    Now what ´rom´ actually means in terms of any given compiler is dependent on that compiler. But chances are it means what has been hinted - that the value in question will be burned into the ROM when the device is programmed, and will be truly fixed for all time (and be available when the program starts).

    A const in C just means ´warn me if I try to modify this´ (which is a bit different from the C++ idea of const).
    A const value in C would still be in RAM and would have to be initialised by some mechanism.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by kermit View Post
    ?? C has as many keywords as specified by the standard. They are reserved in the sense that you may not use them as identifiers for anything else, e.g, :

    Code:
    int var1; /* OK */
    int char; /* Not OK */
    I thought a simple keyword (not a reserved word) was usable as a variable, but also meant something to the language when used in different context(s). A reserved word isn't allowed to be used as a variable. No?

    Quote Originally Posted by kermit View Post
    I was thinking of something more like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const int i = 5;
        int *p = &i;
    
        i = 4      /* !! Wrong.   Cannot use identifier i to change contents of variable. */
        *p = 4;    /* We _can_ use any other identifier that points to the same place. */
    
        printf("The value of i is %d\n", i);
    
        return 0;
    }
    Now the compiler will complain when you try to compile, unless of course you do something evil, like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const int i = 5;
        int *p = (int *) &i;
    
        *p = 4;
    
        printf("The value of i is %d\n", i);
    
        return 0;
    }
    You can crank up the warnings, and the compiler will not give so much as a peep.
    Yeah, that's what I meant by using pointers, and by "So you're saying if you made a pointer to it and then dereferenced it you could change the value? I mean I suppose there's no stopping you there...and there's no pass by ref in C so you'd most likely be using a pointer, in which all bets of protecting anything are off..."

    Pointers give you infinite power. I meant a non "cheat" method, as there's no pass by ref in C.[/quote]

    Quote Originally Posted by CommonTater View Post
    I've done stuff like this at times. Mostly it's to remind me that something is special in some way.

    Aliasing const as rom might be because the variables pointed to were in a chip and the programmer wanted a reminder.

    Other than that... it's a distinction without a difference.
    Fair enough, I'm fairly against that sort of practice, but the compiler won't care.

    Quote Originally Posted by Salem View Post
    It´s more likely that you have a vendor specific C compiler which has a number of additional keywords for supporting programming of some embedded device.

    Now what ´rom´ actually means in terms of any given compiler is dependent on that compiler. But chances are it means what has been hinted - that the value in question will be burned into the ROM when the device is programmed, and will be truly fixed for all time (and be available when the program starts).

    A const in C just means ´warn me if I try to modify this´ (which is a bit different from the C++ idea of const).
    A const value in C would still be in RAM and would have to be initialised by some mechanism.
    Yeah, I guess that makes sense. How is const different in C than in C++? I was unaware of this.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Syndacate
    How is const different in C than in C++?
    I believe that it is the enforcement that is different, i.e., warning versus error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by laserlight View Post
    I believe that it is the enforcement that is different, i.e., warning versus error.
    I know C++ will stop you if you try to modify a const, that's why I've become best buddies with const_cast, I just tried modifying a const value in C and gcc told me it was read only.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Syndacate
    I know C++ will stop you if you try to modify a const, that's why I've become best buddies with const_cast
    You should not be best buddies with const_cast unless you are stuck working with some library that fails miserably in the const correctness department.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by laserlight View Post
    You should not be best buddies with const_cast unless you are stuck working with some library that fails miserably in the const correctness department.
    As I said earlier, I am firmly against the use of const (at all) - I hate it and think it's an embarrassment to the language. I've never had const save me, only give me grief. Const cast is probably the best thing around because of people who decide that EVERY accessor NEEDS to return a const value.

    It should be up to the programmer to NOT screw with things he or she shouldn't. Not the compiler. That kind of protection is expected at higher levels such as Java, it has no place for C nor C++.

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    So you would rather have runtime bugs than easily detectable compile time issues. Your call I guess.
    Woop?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Syndacate
    I thought a simple keyword (not a reserved word) was usable as a variable, but also meant something to the language when used in different context(s). A reserved word isn't allowed to be used as a variable. No?
    There is an academic distinction between keywords and reserved words, but with respect to C, the terms mean the same thing.

    Quote Originally Posted by Syndacate
    As I said earlier, I am firmly against the use of const (at all) - I hate it and think it's an embarrassment to the language. I've never had const save me, only give me grief.
    My experience is the opposite: I am more confident reasoning about my program when I know that due to const correctness a function that is not supposed to modify an object will not modify that object through a pointer or reference parameter, even if the object is not of a class that is designed to be immutable.

    Quote Originally Posted by Syndacate
    Const cast is probably the best thing around because of people who decide that EVERY accessor NEEDS to return a const value.
    You run the risk of undefined behaviour by using const_cast in this way, e.g., if you decide to use const_cast on a pointer to const that points to something that really is constant, an attempted write through the result of the cast could result in a crash, etc. Using const_cast on a const non-pointer (and non-reference) returned by an accessor is generally unnecessary.

    Quote Originally Posted by Syndacate
    It should be up to the programmer to NOT screw with things he or she shouldn't. Not the compiler. That kind of protection is expected at higher levels such as Java, it has no place for C nor C++.
    You might be able to argue that successfully with C, but that is absurd in C++, where type safety is one of the goals of the language, however limited it may be.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > How is const different in C than in C++?
    Incompatibilities Between ISO C and ISO C++

    In particular, you can do this in C++ (but not in C)
    Code:
    const int size = 100;
    int array[size];
    You can use such consts in C++ in all the places you would have to use #define in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by Syndacate View Post
    As I said earlier, I am firmly against the use of const (at all) - I hate it and think it's an embarrassment to the language. I've never had const save me, only give me grief. Const cast is probably the best thing around because of people who decide that EVERY accessor NEEDS to return a const value.

    It should be up to the programmer to NOT screw with things he or she shouldn't. Not the compiler. That kind of protection is expected at higher levels such as Java, it has no place for C nor C++.
    I politely declare that it's more a case that you're an embarrasment to C or C++ programmers.

    Forget about trying to retrofit it, just learn to use it when making a new project. It is not that hard to get to the point where it is occasionally useful rather than hindering.

    It's throw specifiers that are an actual embarrasment to the language.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21