you have a poit there. i had not seen the pointer issue... But since i never call this function i dont see how it can create a problem in my situation.
would this work?btw i will give you post rating tomorow (24 limit :()Code:*Array = *p_Array;
Printable View
you have a poit there. i had not seen the pointer issue... But since i never call this function i dont see how it can create a problem in my situation.
would this work?btw i will give you post rating tomorow (24 limit :()Code:*Array = *p_Array;
Fix your array bounds in the calling functions. The issue is not Size() itself, but the fact that your are writing past the array bounds.
Arrays in C++ are [0 ... n-1] when created with new T[ n ];Code:p_Map.Size(Layers);
// ...
while(L <= Layers)
p_Map[L].Size(Width);
This is wrong in all your loops by the way. So loops should be like:
Code:for (L = 0; L < Layers; ++L)
You seem to be making string classes and array classes from all of your posts. Is this really what you were assigned from school? Was the assignment to make a dynamic array class, or are you really writing a larger application where you would like to use a dynamic array? If it is the latter, then you should stop wasting time making your own class and just use std::vector. That's what it's there for, so that you have a ready-made class and you can concentrate on other parts of your application.Quote:
Originally Posted by Mitsukai
I guess that "IsoMap" is what you're really trying to accomplish, and not really coding dynamic arrays. You have a buggy IsoMap because your dynamic array is buggy -- you have no idea if IsoMap works or not because the component that it relies on is faulty. This is why you should just use vector -- it works, and has no bugs. Then you can code your IsoMap with confidence that the underlying component is not at fault.Code:IsoMap(long Layers = 0, long Width = 0, long Height = 0)
Also, it seems you don't have a full grasp of the C++ language. Dynamic array classes are properly coded by persons who understand the C++ language, and really shouldn't be attempted by beginners. If you can't get pointers correct, then you won't be able to make progress without others practically writing the code for you. In any event, dynamic array classes are rarely even coded by professional programmers, since they have std::vector<> to do this.
In general, you are really missing out on learning C++ properly. That includes learning to use the standard library for things such as strings and dynamic arrays instead of creating your own.
But looking at your Array class, it contains no user-defined copy constructor and no assignment operator. You were told what the consequences are of leaving both of these out.
Regards,
Paul McKenzie
or i could fix it by doing new[n+1]...
yeah it worked.. thnx.. but now there the same error at closing...
i tested:
and it worked :DCode:int* TEST1 = new int[1];
int* TEST2 = new int[1];
TEST1[0] = 1;
TEST2[0] = 2;
*TEST1 = *TEST2;
MsgBox(TEST1[0]);
delete[] TEST1;
delete[] TEST2;
i dint see notice the pointer faults. they are fixed now. and i havent made the copy contructors yet becus i havent came around that yet and dint need it yet. this is my final assignment for school :) (a big one)
Yes it does work, but hopefully you see it is not necessary:Quote:
Originally Posted by Mitsukai
One word of advice: A C++ program that excessively uses "new" and "delete" does not make it an advanced program -- it makes it a poorly designed program.Code:int Test1 = 1;
int Test2 = 2;
Regards,
Paul McKenzie
so CString and vector doesnt use new?
you would be using std::string not CString.
As it happens std::vector does not use "new". It uses an allocator which is a template parameter. That allocator may use new or it may use malloc if it really wants. You don't need to worry about how it does it, just that it does.
It is a bit like if you are wiring up a house with electrical equipment. You will probably use standard components, rather than design your own switches.
Maybe, maybe not. The issue is if you use new or not, not whether the standard components use new.Quote:
Originally Posted by Mitsukai
Also it isn't your concern what is being used internally. All that you need to know is that these classes work. Also, for standard C++, the class is std::string, not CString.
I have a dynamic array of string, and added 4 strings to the array. This is a complete program. How many calls to "new" or "delete" do you see here? The answer is none, regardless of what vector or string do internally. Since this code does not make calls to new and delete, this moves the management of memory from the code that I wrote to the vector and string classes. Since the vector and string classes are correct, then this entire program works.Code:#include <string>
#include <vector>
int main()
{
std::vector<std::string> StringVector;
StringVector.push_back("This");
StringVector.push_back("is");
StringVector.push_back("a");
StringVector.push_back("test");
}
Here's the bottom line: If we were both to build the same application, I get more time to complete the more important parts of the program and I have built my program on a stable foundation since I used the standard classes. In your case, you're still trying to fixing bugs in your home-made array and string classes and basing the application on these buggy components. Now which is more productive, my approach or your approach?
If your coding your own array class to be used in a real program and you're using C++, then seriously, this type of work you're doing should be done only as an exercise, and should not be used in production code.
Regards,
Paul McKenzie
Whether they use new or not, does not matter.Quote:
Originally Posted by Mitsukai
These classes encapsulate their feature.
And they can replace new/delete.
And, with the principle : "never write the same thing twice", you should not use new/delete in two different classes where vector can be used.
Instead, you must use std::vector.
And if std::vector didn't existed, you would have written it, or found in on Internet, but only once in your life!
The only thing you need to know about std::vector is that if it is used on a UDT it will call that UDT's constructors.
Which ones will it call? Normally the default constructor and the copy constructor, so your class must have those defined.
If you initialise the vector with a parameter, it will call the constructor that takes that parameter and the copy constructor, and the one-parameter constructor may be explicit.
It also may call operator= on your class so you will need that defined.
It may call
which has a default behaviour of a default constructor and 3 assignments, but you can specialise that function to call a member-swap if your class has one (or implement the swap direct in that function, making it a friend if necessary). This should be done if your class has collections or other members that can be copied but would be more optimal to swap.Code:std::swap( YourClass &, YourClass & );