Computer Science Canada Multi-Dimensional arrays in Class |
Author: | copthesaint [ Thu Oct 18, 2012 11:07 am ] | ||||||
Post subject: | Multi-Dimensional arrays in Class | ||||||
I am having difficulty making a two dimensional array in my class. This is the output to my consol screen when the program runs: EDIT: sorry but I cant put this character in code Tag ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //it repeats for the width set These are the errors I get:
And here is my Source code:
Im just trying to learn how too, Because I would like to learn something while the rest of my class are working on random. Thanks if you spot anything. |
Author: | copthesaint [ Thu Oct 18, 2012 3:27 pm ] | ||||||
Post subject: | Re: Multi-Dimensional arrays in Class | ||||||
So I spent some time to figure out why my code wasn't working, more specifically the dynamic arrays and after testing came up with a set of rules for dynamic arrays.
Using these rules I can up with this:
The problem I am getting now is when I try to delete the dynamic array. with test1.~Event(); or test1.DeleteEvent(); it creates an error, anyone know how I can solve this? |
Author: | Dreadnought [ Thu Oct 18, 2012 6:59 pm ] |
Post subject: | Re: Multi-Dimensional arrays in Class |
For a start consider using new[] and delete[] to allocate and free arrays on the heap. |
Author: | TerranceN [ Thu Oct 18, 2012 7:19 pm ] | ||
Post subject: | RE:Multi-Dimensional arrays in Class | ||
Your code runs fine in g++ 4.6.1 (after I included <cstdlib> for the system function), but g++ is fine with deleting already deleted memory, and even deleting null pointers, so you should try the following. When you create something on the stack, its destructor (~Event in this case) is called automatically when it goes out of scope. So if you call test1.~Event() manually, that method will end up being called twice, meaning you try to free memory that's already been free'd. Also a couple things with your code you can improve:
What's the point of suppressing allocation errors when you create the array if you're just going to check right after? And in general if you get a null back from creating an array you're going to get a null dereference sooner or later so you might as well keep the error somewhere where it's easy to see the cause. Also IIRC those errors almost never happen unless the system is out of memory or you allocate something huge. And you also use eChar and eNum as parallel arrays. You can just abstract that into its own class/struct and just have a list of those. @Dreadnought: Isn't that what's he already doing, just inside a class? |
Author: | Dreadnought [ Thu Oct 18, 2012 7:40 pm ] |
Post subject: | Re: Multi-Dimensional arrays in Class |
Sorry, you're right, the code does the same thing, I'm just used to using new[] and delete[] for arrays. please disregard my previous post. |
Author: | copthesaint [ Thu Oct 18, 2012 9:53 pm ] |
Post subject: | RE:Multi-Dimensional arrays in Class |
Thanks Terrance I appreciate the answer and thanks for explaining the destructor. |