views:

74

answers:

2

Possible Duplicate:
Modified a constant in c

const int z = 420;
const void *v;
v = &z;

printf("\n%d | %d",z,*(int *)v);
//420 | 420

printf("\n%d | %d",*(char *)&z,*(char *)v); //0th-Bit same value
//-92 | -92

printf("\n%d | %d",*((char *)&z+1),*((char *)v+1) );    //1st-Bit same value    
//1 | 1

/***********************************************/
*((char *)&z+1) = 21;   //I change value for the 1st-Bit
                            //see v is not touched here.

printf("\n%d | %d -(note)-successfully corrupted (z+1) and change reflected in (v+1)",*((char *)&z+1),*((char *)v+1) );
//21 | 21
//yes  change is reflected in v after corruption of z

/****************the problem******************/

printf("\n%d | %d",z,*(int *)v);    //but now value of v is courrupt...while that of z is same
//420 | 5540
printf("\n%u | %u",&z,v);               //same address different values?
//1310548 | 1310548


/*************additional info*******************/

printf("\n%d | %d",*(&(*(&z+1))-1),*(int *)v);
//5540 | 5540

printf("\n%u | %u",(&(*(&z+1))-1),v);
//1310548 | 1310548

1>

void pointer pointing to "z"

when dereferenced gives corrupted value

but when z is used directly it gives original value.

so same address is holding 2 different values

2>

when z is subjected to an identity pointer transformation

(i.e. increment and decrement back)

z will now output the corrupted value!

but z when subjected to normal or no transformations

like "*(&z)" will still give the original value.

+2  A: 

If you really want to prevent data from being modified, use your operating system to declare its memory page non-modifiable.

const in C and C++ is a conceptual safety mechanism and a weak verification tool, not a security measure. It provides guarantees to programmers who follow certain rules. If the rules are broken, no guarantees. (Depending how severely, no guarantee it doesn't crash. Your program is allowed to crash or provide inconsistent values for the "constant.")

Oh, your real question is how there can "be" two different values at the address. The answer is that if the compiler decides it knows you're referring to the constant, it won't look at the address and just gives the value instead. After all, that's what you were supposedly telling it is OK.

Potatoswatter
its not like I want something secured...my Qs is that how can 1 address store different values...and why does pointer arithmetic make a difference, on which value is actually outputed.
bakra
+1  A: 

Stop asking this question ;-)

If it helps, you can assume that the compiler has taken code like this:

const int z = 420;
...
printf("%d\n", z);

And replaced it with:

const int z = 420;
...
printf("%d\n", 420);

That's not guaranteed, you can't rely on it, but it's the kind of thing compilers do, and it would account for what you're seeing.

You also take the address of z, but the compiler won't/can't necessarily track the use of that pointer, and replace all accesses through it in the same way. That's a much harder job than just recognising that the symbol z refers to a const object. So when you invalidly modified that const object, one of the ways in which undefined behavior has manifested is the inconsistencies you're seeing.

If you want to know what your compiler has actually done, and you won't follow James' advice, then you're out of luck. Nobody here knows for sure exactly what your compiler has done. Nobody even knows what compiler you're using. Different compilers do different things.

Steve Jessop
compiler visual studio...anyways, constants are inlined...that could be the soln....Quote:"The keyword const doesn't turn a variable into a constant! A symbol with the const qualifier merely means that the symbol cannot be used for assignment. This makes the value read -only through that symbol; it does not prevent the value from being modified through some other means internal (or even external) to the program. It is pretty much useful only for qualifying a pointer parameter, to indicate that this function will not change the data that argument points to, but other functions may."
bakra
@bakra: I don't know where that quote came from. I don't think all of it is true. Using `const` on an `int` variable doesn't formally turn a variable into a constant, as far as legal C syntax is concerned. So you can't use it as a case in a `switch` statement. However, it does permit the compiler to optimize the program using the "fact" that the value doesn't change (assuming it's not also `volatile`). That's because any program which does attempt to change it has undefined behavior anyway, so it doesn't matter if the optimizations "break" it.
Steve Jessop