how this is possible in c(const variable)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    #16
    Originally posted by weaknessforcats
    The const qualifer does make the space immutable. You are correct that you cannot use the const identifier to change the value of the const. You are incorrect that you can change it by other means. If you try that the results are indeterminate.

    Consider this code:
    Code:
    const int var = 10;
    int* ptr = (int*)&var;
    *ptr = 20;
    cout << var << " " << *ptr << endl;
    cout << &var << " " << ptr << endl;
    Running this code on Visual Studio.NET 2008 I get:

    10 20
    0012FF40 0012FF40

    It appears the 10 and the 20 are at the same location in memory.

    That means the part of my program that uses var sees 10 and the part that uses *ptr sees 20. This is classic indeterminate results. Other compilers may behave differently.

    There are safe ways to cast off a const but this isn't it.
    Read my reply in post #13 :-P

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #17
      This thread is getting pretty long ... and I think we're mostly saying the same thing. But I'll go ahead and add a little more.

      The const-ness of a variable can vary from one part of a program to another. Consider the following snippet. Variable i is generally non-const, but it cannot be altered via the alias variable p from within function inc (that is, it is effectively const from within inc).
      Code:
      int i, j;
      i = 10;
      j = func(&i);
      ...
      int inc(const int *p) {
      return (*p) + 1;
      }
      I'm playing a little fast and loose here. Any particular variable is either const or non-const in accordance with its declaration. As you create pointer aliases for a variable you can choose whether each such pointer alias is const or non-const [almost] without regard to the original declaration of the variable.

      I say "almost" because you shouldn't be allowed to assign a pointer to non-const to the address of a const variable. Every compiler I've used since C89 has enforced that constraint, but I don't know whether the Standard explicitly requires it.

      An interesting and arguably pathological variant is to define a non-const global variable in one file and then declare a const extern to that variable in another file.

      "const" is a compiler keyword. The C Standard does not require it to have any meaning beyond the scope of a single translation unit. Nevertheless, it is to your advantage to make your software as understandable as possible. Playing games with const-ness will certainly be confusing ... and it may cause run-time errors.

      Comment

      • vinayvaka
        New Member
        • Nov 2007
        • 13

        #18
        thanks a lot for all ur replys..

        after reading all messages(9,12,1 5)
        the const variable which is declare in main will save in either code sigment or in flashmem(i accept this two :-)

        no my question is:
        in case of flashmemory we cannot change it because it is read only so leave that part
        in case of codesigment:
        after refering all C books " the variable wich is declare as CONST is saved in Codesigment" so if we changing the const variable with some pointer(offcour se as a programer we should not do this, for better clarification in c i am doing this) it should give sigmentation fault(bacause we are trying to change the value in the variable which is saved in code sigment).

        lets see the example below:
        void main()
        {

        const int i=10;
        const int j=20;
        int *p=&j;
        *p=30;
        printf("%u%u%u% d%d",&i,&j,p,i, *p);
        }

        here i and j are saved in codesigment( after seeing the address i come to know this)
        finally pointer changed the codesigment value..

        HERE MY QUESTION IS WHY I DIDNT GET THE ERROR LIKE SIGMENTATION FAULT??

        ur replys will helps not only for me for all who will see this........... ...............
        so please talk in practicle way (last 6 months i am searching the ans for this but i didnt get the ans).

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #19
          Originally posted by vinayvaka
          HERE MY QUESTION IS WHY I DIDNT GET THE ERROR LIKE SIGMENTATION FAULT??
          What you got was undefined behaviour as Tassos Souris mentioned earlier.

          The problem with undefined behaviour is that it is just as it name says, undefined. Anything could happen, this includes a segmentation fault but it also includes the program apparently running correctly and a host of other possible results.

          Undefined behaviour can be extremely hard to detect, for example once early in my programming career I was sent fix a problem with a customers system. The computer system crashed when located at one end of the office and not at the other (literally the customer was blaming ley lines). I after much head scratching and debugging I finally tracked the problem to an assignment to a NULL pointer. The pointer variable in question was had automatic scope (was on the stack of a function) and the file the function was in had not changed in a year and half.

          You would normally expect an assignment to a NULL pointer to cause a segmentation fault but it is actually undefined behaviour and in this case the problem lay dormant miraculously working every time for over a year and a half.


          You need to know what causes undefined behaviour and avoid it at all costs. Modern compilers help, they use static analysis to detect a whole load of errors and potential problems that compilers didn't used to detect and static analysis tools can help also especially if the compiler you happen to be using does not do a lot of static analysis for it-self.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by Tassos Souris
            ...
            Let's see what is the difference between ( *p )++ and what you wrote *p++:

            *p++:
            Let's look at the precedence here: ++ has higher precendence over * thus it is evaluated first. ..
            Same precedence. ++ is evaluated first because they associate right to left.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #21
              Originally posted by vinayvaka
              the const variable which is declare in main will save in either code sigment or in flashmem
              Not True!

              The C Standard places no requirement on where const variables are located in the executable image. For that matter, the C Standard does not require compilers to have any such thing as "code segment", "data segment", "flash memory", etc.

              You have no reason to be so confident that your const variable is located in either code segment or flashmem. In fact, lack of a segmentation fault when you wrote to the variable is a strong indication that your confidence is misplaced.

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #22
                Originally posted by r035198x
                Same precedence. ++ is evaluated first because they associate right to left.
                Yes right!!!!!!!!!!

                on the next i explained it right though :-P :-P

                Comment

                Working...