dynamic array

This is a discussion on dynamic array within the C++ Programming forums, part of the General Programming Boards category; Assume that Disk and Tower are both classes. Furthermore assume the following header and constructor for class Triad: Code: class ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question dynamic array

    Assume that Disk and Tower are both classes. Furthermore assume the following header and constructor for class Triad:

    Code:
    class Triad
    {
    	friend std::ostream& operator <<(std::ostream& lhs, Triad& rhs);
    
    public:
    	Triad(int diskCount = 3);
    	~Triad();
    
    	void run();
    	void run(Tower* source, Tower* waypoint, Tower* dest, int toMove);
    
    private:
    	Tower hanoi[3];
    	Disk* disks;
    	int diskNo;
    
    	void sort_disks(); // sorts disks from greatest to least
    };
    
    Triad::Triad(int diskCount)
    :diskNo(diskCount)
    {
    	disks = new Disk[diskNo];
    	sort_disks();
    }
    I have already included <iostream>, <string>, and <iomanip>, though I'm not so sure that's making any difference here.

    What I want to ask is when that constructor is called with diskCount being 2 or higher, why in the world would disks not get more than one element?

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It would always get more than one element. Why do you think it doesn't?

    BTW, there's really no reason to use a dynamic array here unless you have to for learning purposes. A vector is a much better solution in C++.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    It doesn't get more than one element. I kept on having the thing crash on me, so i went through the code with the debugger. As I was checking the debugger, I learned that it did not get more than one element.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    100
    Nevermind about what I said. Apparently the debugger isn't very good about showing the contents of dynamic arrays, and its serious readability problems on that issue misled me. Thanks Daved.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by jsrig88 View Post
    Nevermind about what I said. Apparently the debugger isn't very good about showing the contents of dynamic arrays, and its serious readability problems on that issue misled me. Thanks Daved.
    pointer,m
    in VS shows 16 byter after the pointer... there are other modifiers as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-03-2005, 11:13 PM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21