|
If you try to do
p = &a;
in this example the compiler will complain that it can not convert from const int* to int* !
Ant.
|
|
|
|
|
V. wrote:
int* p = 100;
=> *p is the value AND p is the pointer
Yes but he is assigning the adress of a in p just before:
int* p=(int*)&a;<br />
So p and &a are the same (in this case, the adress of &a).
|
|
|
|
|
and if you output *(int*)&a rather than a?
Ant.
|
|
|
|
|
where do you see a difference between *(int*)&a and a you, except in confusing the reader ??
TOXCCT >>> GEII power
|
|
|
|
|
Try it in VC 6 you will be amazed, anyway see my other post WRT casting away const-ness.
Ant.
|
|
|
|
|
To all:
const int a=10;//Declare an int constant and init 10
int* p=(int*)&a; //Declare an int pointer p and point to address of a.
*p=100;//Change where p pointed to 100
int i=a;//Declare an int variable i and init i to value of a.
cout << "p=" << p << endl;//Print p value (address)
cout << "&a=" << &a << endl;//Print address of a
cout << "*p=" << *p << endl;//Print value at where p pointed to
cout << "a=" << a << endl;//Print value of a
cout << "i=" << i << endl;//Print value of i
And the result:
p=0012FF7C //Point to the same address of a
&a=0012FF7C //Point to the same address of p
*p=100 //But one value is 100
a=10 //And here the other value is 10
i=10 //And here the value is 10 too.
p and a have the same memory address. But it's value is difference. Why that?
PS: if I write: int* p=&a, compiler will compain as an error. So I have used a trick by casting int* p=(int*)&a. In this case, compiler ignore.
|
|
|
|
|
Well it gives a==100 on my compiler! But i==10 . I'd imagine that the problem was set to explain why i != a .
You're right that the compiler effectively replaces all references to a with the value 10, and that's why i == 10 (because the compiler sees this line as being i = 10 ) but the value a does have a physical address and using the two lines:
<br />
int* p=(int*)&a;<br />
*p=100;<br />
should change a .
If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this:
<br />
1844: const int a=10;<br />
0050D1BF mov dword ptr [a],0Ah<br />
1845: int* p=(int*)&a;<br />
0050D1C6 lea eax,[a]<br />
0050D1C9 mov dword ptr [p],eax<br />
1846: *p=100;<br />
0050D1CC mov ecx,dword ptr [p]<br />
0050D1CF mov dword ptr [ecx],64h<br />
1847: int i=a;<br />
0050D1D5 mov dword ptr [i],0Ah<br />
Here it's clear that a will be changed to 100 in line 1846 and also that i will be set to 10 in line 1847.
By the way, const isn't magic, it just tells the compiler that you shouldn't be able to change a using a line like a = 20; it doesn't add some kind of 'lock' to a meaning it can never change.
Oh yeah, I've just spotted why a is 10 in your output. Because you output a which has been replaced at compile time with 10!!
|
|
|
|
|
Finally it's clear !!
Yes ok, I didn't know that the compiler really replace all occurences of a by it's value (so acting like a #define in fact).
What did you win ??
|
|
|
|
|
Yea it seems that the compiler has optimised itself here since a was declared const!
Ant.
|
|
|
|
|
good catch! here: enjoy on me
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
|
nguyenvhn wrote:
const int a=10;
This simply means that you cannot modify memory address 0x0012FF7C through variable a . The address is not read-only, the variable is. make sense?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
teu teu teu, read the other posts before saying so, especially "Christopher Lloyd"'s one
TOXCCT >>> GEII power
|
|
|
|
|
In which case he states that "it just tells the compiler that you shouldn't be able to change a using a line like a = 20" In my post, I stated that "you cannot modify memory address 0x0012FF7C through variable a"
What's your point?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
yes you can
const is only a specification for the compiler!!!
if you had read all the posts here, you would have seen that we already said that also... and i even that i was thinking like you !
TOXCCT >>> GEII power
|
|
|
|
|
Then how would you propose changing the value through variable a ?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
by a pointer to its address. int* p = &a; . this works, but badly, the compiler replaced each occurence of a by its value (10 here).
that is why, even if the value contained at the adress of a is changed, the compiler used each a uses as a #define . int d = a; will affect 10 to d, even if the line before was *p = 100; .
Do we now understand better ?
TOXCCT >>> GEII power
|
|
|
|
|
int *p = &a;
*p = 100; This does not change the value through variable a . The value is changed through variable p . Do you still find my original "...you cannot modify memory address 0x0012FF7C through variable a. The address is not read-only, the variable is" statement to be false.
I *think* we are saying the same thing, so I'm not quite sure why this thread is six posts longer than necessary.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
#include <iostream>
using namespace std;
int main(){
int a=10;
int* p=(int*)&a;
*p=100;
int i=a;
cout << "p=" << p << endl;
cout << "&a=" << &a << endl;
cout << "*p=" << *p << endl;
cout << "a=" << a << endl;
cout << "i=" << i << endl;
return 0;
}
the running result
p=0012FF7C
&a=0012FF7C
*p=100
a=100
i=100
|
|
|
|
|
why are you tampering code by removing const while declaring variable a.
it should be const int a=10;
The chosen One
|
|
|
|
|
this is because of compiler optimization. Just have a look at assembly code.
const int a=10;
0041B1CE mov dword ptr [a],0Ah
int* p=(int*)&a;
0041B1D5 lea eax,[a]
0041B1D8 mov dword ptr [p],eax
*p=100;
0041B1DB mov eax,dword ptr [p]
0041B1DE mov dword ptr [eax],64h
int i=a;
0041B1E4 mov dword ptr [i],0Ah
Also while printing on console...
cout << "a=" << a << endl;
0041B291 push offset std::endl (4194BAh)
0041B296 push 0Ah
0041B298 push offset string "a=" (44E0C8h)
0041B29D push offset std::cout (457668h)
0041B2A2 call std::operator<<<std::char_traits<char> > (419A96h)
0041B2A7 add esp,8
0041B2AA mov ecx,eax
0041B2AC call std::basic_ostream<char,std::char_traits<char> >::operator<< (4195DCh)
0041B2B1 mov ecx,eax
0041B2B3 call std::basic_ostream<char,std::char_traits<char> >::operator<< (419AC3h)
You will not experience this behavour with all the compilers.
Thanks,
Anand
The chosen One
|
|
|
|
|
When I try to load any DLL with LoadLibraryEx, it gives a NULL-handle and the last error (GetLastError) is ERROR_SHARING_VOILATION. I'm using LoadLibraryEx, because I want to load a DLL that I found using CFileFind (or however it's called).
Does someone know how to solve that? Even looping (+::Sleep(50)) until the last error is not ERROR_SHARING_VIOLATION won't help (it will hang the application).
|
|
|
|
|
Are you sure that GetLastError() is returning the correct code? Before calling LoadLibraryEx() , do a SetLastError(0); to make sure.
Also, check which other app has loaded the DLL.
Bikram
|
|
|
|
|
Another perfect example of my stupid moves:
First I load the DLL with the CFile object, and then with LoadLibraryEx. What did I do wrong? I forgot to tell CFile to CFile::shareDenyNone.
Tnx.
|
|
|
|
|
HI..
I want to use DirectShow to capture viceo from tv tunner card and want to save it on disk in compressed format. which format is more compressed .. AVI is ahviear on disk
Can i do it ,, how if yes...
wt is difference between this and simple capturing using USB cam..
reply
also code if any abut tv tunner capturing
thanx
|
|
|
|