Originally posted by weaknessforcats
how this is possible in c(const variable)
Collapse
X
-
-
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).
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.Code:int i, j; i = 10; j = func(&i); ... int inc(const int *p) { return (*p) + 1; }
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
-
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
-
What you got was undefined behaviour as Tassos Souris mentioned earlier.Originally posted by vinayvakaHERE MY QUESTION IS WHY I DIDNT GET THE ERROR LIKE SIGMENTATION FAULT??
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
-
Same precedence. ++ is evaluated first because they associate right to left.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. ..Comment
-
Not True!Originally posted by vinayvakathe const variable which is declare in main will save in either code sigment or in flashmem
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
-
Yes right!!!!!!!!!!Originally posted by r035198xSame precedence. ++ is evaluated first because they associate right to left.
on the next i explained it right though :-P :-PComment
Comment