Click here to Skip to main content
15,886,518 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionOOP - Error | Unhandled exception at 0x0FD2CCC8 (msvcp110d.dll) Pin
Member 1164129910-Jun-15 2:23
Member 1164129910-Jun-15 2:23 
QuestionRe: OOP - Error | Unhandled exception at 0x0FD2CCC8 (msvcp110d.dll) Pin
Richard MacCutchan10-Jun-15 21:32
mveRichard MacCutchan10-Jun-15 21:32 
QuestionEnabling ListControl pane in MFC (C++) Pin
Yateesh KR12-May-15 22:14
Yateesh KR12-May-15 22:14 
QuestionRe: Enabling ListControl pane in MFC (C++) Pin
Richard MacCutchan13-May-15 2:55
mveRichard MacCutchan13-May-15 2:55 
QuestionInheritance tricky question Pin
Amrit Agr12-May-15 9:30
Amrit Agr12-May-15 9:30 
AnswerRe: Inheritance tricky question Pin
Sascha Lefèvre12-May-15 11:38
professionalSascha Lefèvre12-May-15 11:38 
GeneralRe: Inheritance tricky question Pin
Amrit Agr13-May-15 7:47
Amrit Agr13-May-15 7:47 
AnswerRe: Inheritance tricky question Pin
Sascha Lefèvre13-May-15 9:12
professionalSascha Lefèvre13-May-15 9:12 
Hi Amrit,

I'll try to explain it with some diagrams.

First, the simple case - you probably know this, but I'll show it for completeness. The class Foo has a single member of type int (int32), so it has a size of 4 bytes.
C++
# 3 instances of Foo in memory with a Foo* pointer pointing to it:

Foo myFoo[3];      // in _tmain(..)

Foo* ptr = myFoo;  // calling display(..)   (I renamed *obj to *ptr here)

#                  ptr       ptr+1     ptr+2      // the loop in display(..)
#                  (100)     (100+4)   (100+8)
#                  |         |         |
#                  v         v         v
# Memory address:  100..103  104..107  108..111   // (artificially chosen address)
# Instance:        --Foo1--  --Foo2--  --Foo3--
# Members:         ---i----  ---i----  ---i----


Now with the class Bar (8 bytes) and your original approach:
C++
# 3 instances of Bar in memory with a Foo* pointer pointing to it:

Bar myBar[3];      // in _tmain(..)

Foo* ptr = myBar;  // calling display(..)

#                  ptr    ptr+1    ptr+2     // the loop in display(..)
#                  (100)  (100+4)  (100+8)   // here's why it doesn't work: 
#                  |      |        |         // ptr is Foo* so ptr+1 still means adding the size of Foo (4) !
#                  v      v        v
# Memory address:  100........107  108........115  116........123
# Instance:        -----Bar1-----  -----Bar2-----  -----Bar3-----
# Members:         ---i---___j___  ---i---___j___  ---i---___j___

# Value:              0      1        0   // and there's your strange output! ;-)


And now with pointers to pointers. Let's say we're on a 32bit-system here, then a pointer has the size of 4 bytes (32 bit). So, when doing pointer arithmetic with pointers to pointers, adding 1 means always increasing its pointed-to-address by 4 bytes. It doesn't have anything to do with the size of the class that's being pointed to by the pointer that's being pointed to Big Grin | :-D
C++
# 3 instances of Bar in memory with a Foo** pointer pointing to Bar* pointers:

Bar myBar[3];       // in _tmain(..)
Foo *myBaz[3] = { &myBar[0], &myBar[1], &myBar[2] };
# Value:           100        108        116

Foo** pptr = myBaz; // calling display2(..)          (for clarity, I named it pptr here)

#                  pptr       pptr+1     pptr+2
#                  (200)      (200+4)    (200+8)     // see explanation above (32 bit..)
#                  |          |          |
#                  v          v          v
# Memory address:  200        204        208         // (artificially chosen address)
# Pointer:         &myBar[0]  &myBar[1]  &myBar[2]
# Value:           100        108        116
#                  |            \           \__
#                  |             \             \__
#                  | *pptr        \ *(pptr+1)     \ *(pptr+2)  // resolving from pptr to ptr
#                  v               v               v
# Memory address:  100........107  108........115  116........123
# Instance:        -----Bar1-----  -----Bar2-----  -----Bar3-----
# Members:         ---i---___j___  ---i---___j___  ---i---___j___


Does this answer your questions?

/Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

Questionenglish language implementation Pin
Member 936602117-Apr-15 3:58
professionalMember 936602117-Apr-15 3:58 
AnswerRe: english language implementation Pin
Member 936602117-Apr-15 4:00
professionalMember 936602117-Apr-15 4:00 
Questionmultiple Pin
memoarfaa16-Apr-15 1:28
memoarfaa16-Apr-15 1:28 
GeneralRe: multiple Pin
Richard MacCutchan16-Apr-15 2:20
mveRichard MacCutchan16-Apr-15 2:20 
QuestionHow to swap address of pointers of 2 variables without using a temporary variable? Pin
Amrit Agr13-Apr-15 20:36
Amrit Agr13-Apr-15 20:36 
AnswerRe: How to swap address of pointers of 2 variables without using a temporary variable? Pin
jschell14-Apr-15 13:17
jschell14-Apr-15 13:17 
AnswerRe: How to swap address of pointers of 2 variables without using a temporary variable? Pin
zhijzan7-Jun-15 20:40
zhijzan7-Jun-15 20:40 
AnswerRe: How to swap address of pointers of 2 variables without using a temporary variable? Pin
Shao Voon Wong28-Jun-15 22:13
mvaShao Voon Wong28-Jun-15 22:13 
Questionhow to migrate CORBA to Web Service in C++ Pin
VC_RYK13-Apr-15 20:20
VC_RYK13-Apr-15 20:20 
Questionconsumer.C: In function âint main(int, char**)â: consumer.C:69: error: invalid conversion from âvoid (*)(buffer_t*, char)â to âvoid* (*)(void*)â consumer.C:69: error: initializing argument 3 of âint pthread_create(pthread_t*, const pthread_attr_t*, Pin
Member 1160464113-Apr-15 15:29
Member 1160464113-Apr-15 15:29 
AnswerRe: consumer.C: In function âint main(int, char**)â: consumer.C:69: error: invalid conversion from âvoid (*)(buffer_t*, char)â to âvoid* (*)(void*)â consumer.C:69: error: initializing argument 3 of âint pthread_create(pthread_t*, const pthread_attr Pin
Richard MacCutchan13-Apr-15 21:33
mveRichard MacCutchan13-Apr-15 21:33 
Questionint **p = (int**)new int(5); Confusing !!! Pin
Amrit Agr5-Apr-15 8:32
Amrit Agr5-Apr-15 8:32 
AnswerRe: int **p = (int**)new int(5); Confusing !!! Pin
Richard Andrew x645-Apr-15 10:14
professionalRichard Andrew x645-Apr-15 10:14 
AnswerRe: int **p = (int**)new int(5); Confusing !!! Pin
Zabir Al Nazi Nabil5-Apr-15 11:34
professionalZabir Al Nazi Nabil5-Apr-15 11:34 
Questionsolitaire game Pin
Member 115602925-Apr-15 4:19
Member 115602925-Apr-15 4:19 
AnswerRe: solitaire game Pin
Richard MacCutchan5-Apr-15 5:39
mveRichard MacCutchan5-Apr-15 5:39 
QuestionFast object tracking code Pin
Member 1051522512-Mar-15 22:35
professionalMember 1051522512-Mar-15 22:35 

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.