A Hole in the C++ Type System
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: A Hole in the C++ Type System

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    Talking A Hole in the C++ Type System

    Finish. Thanks everyone
    Last edited by Cesc van Fei; November 24th, 2010 at 10:04 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: A Hole in the C++ Type System

    Quote Originally Posted by Cesc van Fei View Post
    Explain why, and give an example program that demonstrates the problem.
    Is that a demand? Why is it bolded?

    It's your homework, not ours, so let's see your answer and we will comment on it.

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: A Hole in the C++ Type System

    This sounds like homework.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: A Hole in the C++ Type System

    One of the "Effective" series covers this specific problem in an Item, but I can't recall which one. Probably Effective C++. The item would be called something like "Don't use polymorphic arrays".

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: A Hole in the C++ Type System

    A few clues....

    array[i] = *(array + i)

    try sending an array of derived to a function expecting an array of base. What happens? Can you work out why?
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: A Hole in the C++ Type System

    Unfortunately, we can't really give you a hint as to why the behavior is bad without actually telling you. Try this

    Code:
    class A {
       public:
       A(){i = 0;}
       int i;
    };
    
    class B : A {
       public:
       B(){j = 0xFFFFFFFF;}
       unsigned int j;
    };
    Create an array of A and an array of class B, look carefully at the memory. Why you can't do that should become obvious.

  7. #7
    Join Date
    Nov 2010
    Posts
    2

    Re: A Hole in the C++ Type System

    Code:
    #include <iostream.h>
    
    class B {
    public:
    	char dataB;
    	B(char x) {
    		dataB=x;
    	}
    	void showB() {
    		cout << dataB;
    	}
    };
    
    class D: public B {
    public:
    	char dataD;
    	char dataX;
    	D(char x, char y):B(x) {
    		dataD=y;
    	}
    	void show() {cout << dataB << " " << dataD <<'\n';}
    };
    
    void process(B* p) {
    	p->dataB='Z';
    }
    
    void process(B* a, int n) {
    	a[1].dataB='H';
    }
    
    int main() {
    	int n=2;
    	D ob[]={D('B','D'),D('P','Q')};	
    	//process(ob+1);
    	process(ob,n);
    	for (int i=0;i<n;i++) {
    		ob[i].show();
    	}
    	return 0;
    }
    Sorry, I forgot posting my solution. Any comments for me?
    Does someone have some hints for me?
    Thanks very much!

  8. #8
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: A Hole in the C++ Type System

    iostream.h is very very deprecated. The correct header is <iostream>

    We can see whats happening in your code, but your mission is to explain why and we do not see any explanation for the exhibited behaviour.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: A Hole in the C++ Type System

    I wrote some code that shows you the behavior you are talking about, do you see why the output is the way that it is?

    Code:
    #include <iostream>
    
    class A {
    public:
        A(){i = 0;}
        unsigned int i;
    };
    
    class B : public A {
    public:
        B(){j = 0xFFFFFFFF;}
        unsigned int j;
    };
    
    
    static void printA(A * array, size_t len){
        for(size_t k = 0; k < len; ++k, ++array){
    	   std::cout << (*array).i << std::endl;
        }
    }
    
    static void printB(B * array, size_t len){
        for(size_t k = 0; k < len; ++k, ++array){
    	   std::cout << (*array).i << std::endl;
        }
    }
    
    int main(void){
        B testArray[10];
        printA((A*)testArray, 10);
        printB(testArray, 10);
        
        return 0;
    }

  10. #10
    Join Date
    Apr 1999
    Posts
    27,446

    Re: A Hole in the C++ Type System

    Quote Originally Posted by Cesc van Fei View Post
    Code:
    #include <iostream.h>
    
    class B {
    public:
    	char dataB;
    	B(char x) {
    		dataB=x;
    	}
    	void showB() {
    		cout << dataB;
    	}
    };
    
    class D: public B {
    public:
    	char dataD;
    	char dataX;
    	D(char x, char y):B(x) {
    		dataD=y;
    	}
    	void show() {cout << dataB << " " << dataD <<'\n';}
    };
    
    void process(B* p) {
    	p->dataB='Z';
    }
    
    void process(B* a, int n) {
    	a[1].dataB='H';
    }
    
    int main() {
    	int n=2;
    	D ob[]={D('B','D'),D('P','Q')};	
    	//process(ob+1);
    	process(ob,n);
    	for (int i=0;i<n;i++) {
    		ob[i].show();
    	}
    	return 0;
    }
    Sorry, I forgot posting my solution. Any comments for me?
    Does someone have some hints for me?
    Thanks very much!
    1) What is an array? Given that, how does the compiler know where each array element starts? For example, if you have an array of Base, and Base[0] is the first element in memory, how does the compiler know where Base[1] is in memory, Base[2], Base[3], etc.? Please explain this.

    2) Given your answer for 1) above, if the compiler expects an array of Base, but the array has actually an array of Derived objects (where Derived is derived from Base), and that Derived objects are larger in size than Base objects (a huge clue), what do you think the compiler will do, given your answer above?

    That, in a nutshell, is your answer.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 22nd, 2010 at 07:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center