Click here to Skip to main content
15,912,021 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: stuck with a trivia Pin
Stuart Dootson3-Feb-09 21:12
professionalStuart Dootson3-Feb-09 21:12 
GeneralRe: stuck with a trivia [modified] Pin
Smith#3-Feb-09 21:24
Smith#3-Feb-09 21:24 
GeneralRe: stuck with a trivia Pin
Stuart Dootson3-Feb-09 22:01
professionalStuart Dootson3-Feb-09 22:01 
GeneralRe: stuck with a trivia Pin
Smith#3-Feb-09 22:14
Smith#3-Feb-09 22:14 
GeneralRe: stuck with a trivia Pin
Smith#3-Feb-09 22:18
Smith#3-Feb-09 22:18 
AnswerRe: stuck with a trivia Pin
Emilio Garavaglia3-Feb-09 23:13
Emilio Garavaglia3-Feb-09 23:13 
GeneralRe: stuck with a trivia Pin
Smith#4-Feb-09 7:44
Smith#4-Feb-09 7:44 
GeneralRe: stuck with a trivia [modified] Pin
Emilio Garavaglia4-Feb-09 21:32
Emilio Garavaglia4-Feb-09 21:32 
It seems you're confusing two well distinct concepts (don't worry: it takes about three years to me to get away from that confusion...).
Let's reconsider your points:

Smith# wrote:
As you know pointers & arrays both move consecutive

No. Pointers "move" (in th sense they can receive another address as their own stored value). Array just "are". Array indexes "move" (in the sense they can be used to indicate other array cells).

Smith# wrote:
how come it be totally different

Because they ARE totally different. They simple have a "similar" external interface (same operators).If you don't get this point may be I was not able to explain the first point, but until you don't get it you cannot move away from your trivia. So loop over and over until you get it, find other sources than me (if you find my way to explain is not clear) but there is no way to move over until this crucial point is clear.

Smith# wrote:
Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array,

You know, but compiler doesn't.
int** for the compiler is just 4 bytes of memory containing the address of an int* that is another 4 byte of memory, somewhere else, containing the address of an int.
Whatever you think about this is irrelevant to the compiler.

int[] is a consecutive bounce of integers at some place in memory. This place has an address.
When you do
int a[2] = { 0,1 };
int* pa = a;

you assign the address of the array head to the pointer.
When you do
*pa = 5;

the compiler assign 5 to the memory whose address is stored in "pa".
This accidentally is a[0] (but it is not something the compiler is aware of).
When you do
pa[1] = 6;

the compiler assign 6 to the memory whose address is given by pa+1*sizeof(int), that accidentally is a[1] (again you know, not the compiler: there is nothing in "pa" that makes the compiler aware of the fact that it is NOW pointing to an array and sometime later on to an individual integer and some later on to an integer inside a class or struct.
The design of the [] operator appllied to poitner or to array makes this operation giving a same result just because for monidimensional array, the same calculation is done.

Bidimensional array are different:
int a[2][2] = { 1,2,3,4 };
a[1,1] = 5;

means "set to 5 the cell "1" of the subarray "1" of the bidimensional array.
But
int **ppa = a;
ppa[1][1] = 8;

means: "take the value of "ppa", add 1*sizeof(int*) (note: int*, not int) fetch the value stored at such address, add 1*sizeof(int) (yes: now it's int) and this is the address where to store 8.

Now: since ppa contains the address of a[0][0] and sizeof(int*) is equal to sizeof(int) (at least on 32 bit Win-Intel), ppa[1][1] means:
take the address of a[0][0] add 1*sizeof(int*) (gives incidentally the address of a[1][0]), get the value (it's 2) add 1*sizeif(int) (gives 6) and store 8 to the memory whose address starts at "0x00000006" (6). That's out of your executable address space, hence the crash.

The compiler translate statements one by one without keeping memory of what a statement "means" being after another one.
int[][] is one thing, int** anoter, and the [] operator on a array is different to the [] operator on a pointer. Assigning an array address to a "pointer to pointer" doesn't change its nature of "pointer".

mondimensional array and simple pointer when applied to the [] operator give same results because in this degeneral case, the "resolution formula" is the same, since it start to be different from the second therm, that for one-only indirection, is not present.


2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:


modified on Monday, February 9, 2009 2:11 AM

GeneralRe: stuck with a trivia Pin
Smith#7-Feb-09 16:13
Smith#7-Feb-09 16:13 
GeneralRe: stuck with a trivia Pin
Emilio Garavaglia4-Feb-09 21:52
Emilio Garavaglia4-Feb-09 21:52 
Questionfatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h></windows.h> [modified] Pin
Kiran Pinjala3-Feb-09 19:38
Kiran Pinjala3-Feb-09 19:38 
AnswerRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
SandipG 3-Feb-09 20:49
SandipG 3-Feb-09 20:49 
GeneralRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
Kiran Pinjala3-Feb-09 21:32
Kiran Pinjala3-Feb-09 21:32 
AnswerRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
«_Superman_»3-Feb-09 20:59
professional«_Superman_»3-Feb-09 20:59 
GeneralRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
Kiran Pinjala3-Feb-09 21:34
Kiran Pinjala3-Feb-09 21:34 
GeneralRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
Smith#3-Feb-09 21:44
Smith#3-Feb-09 21:44 
GeneralRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
Kiran Pinjala3-Feb-09 22:24
Kiran Pinjala3-Feb-09 22:24 
QuestionRe: fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include Pin
David Crow4-Feb-09 3:32
David Crow4-Feb-09 3:32 
QuestiontimeSetEvent only in Windows XP? Pin
nm_1143-Feb-09 19:25
nm_1143-Feb-09 19:25 
AnswerRe: timeSetEvent only in Windows XP? Pin
Stuart Dootson3-Feb-09 21:14
professionalStuart Dootson3-Feb-09 21:14 
QuestionEnsuring DialogBox is fully initialized Pin
prashantbhatia0073-Feb-09 19:03
prashantbhatia0073-Feb-09 19:03 
AnswerRe: Ensuring DialogBox is fully initialized Pin
SandipG 3-Feb-09 20:51
SandipG 3-Feb-09 20:51 
AnswerRe: Ensuring DialogBox is fully initialized Pin
prasad_som4-Feb-09 0:48
prasad_som4-Feb-09 0:48 
AnswerRe: Ensuring DialogBox is fully initialized Pin
Iain Clarke, Warrior Programmer4-Feb-09 1:28
Iain Clarke, Warrior Programmer4-Feb-09 1:28 
Questiondrives list Pin
K. Sushilkumar3-Feb-09 18:44
K. Sushilkumar3-Feb-09 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.