hi, im just wondering if there is some way to do ''safe'' arrays in c++? specifically i want them to delete themselves after they are not used, and also dont want to allow access to out of range items, but i also want them to be as identical as possible to the standard array usage..
this is what i have so far (keeping it simple without automated-delete):
template class SafeArray {
public:
SafeArray();
SafeArray(int size) {
array = new T[size];
array_size=size;
}
~SafeArray() {
delete [] array;
}
void Set(int pos, T val) {
array[pos % array_size]=val;
}
T Get(int pos) {
return array[pos % array_size];
}
T operator [] (int x) {
lastaccess=x % array_size;
return array[lastaccess];
}
void operator = (T val) {
array[lastaccess]=val;
}
int lastaccess;
int array_size;
T *array;
};
used in the following way:
SafeArray sa(5);
sa.Set(3,7.5f);
printf("%g\n",sa.Get(3));
//sa[3]=5.5f doesnt work..
sa[3];
sa=5.5f;
printf("%g\n",sa[3]);
printf("%g\n",sa[3+5]);
//output == 7.5,5.5,5.5
so the problem is that i cant simply say sa[3]=5.5f; or whatever.. anyone know if there is a solution to this?
(other than just using arrays&pointers correctly..)
thanks.
kool, thanks, i cant believe i forgot about that. i have another question: i want to be able to ''incorporate'' existing code.. eg: double darray[5]; .... SafeArray sad; sad.SetArray(darray,5); printf("%g\n",sad[1]); with: void SetArray(T *old_array, int size) { array=old_array; array_size=size; lastaccess=0; }
but i have the issue, that i dont want to have to tell it the size of the array (sizeof doesnt work right?) is there some way around this? and also, ~SetArray will try to delete the array, is there some way to find out whether the array was dynamically created or not? (ie: if i need to delete it or not..) automatically? thanks.
quote:Original post by aboeing but i have the issue, that i dont want to have to tell it the size of the array (sizeof doesnt work right?) is there some way around this?
No. C++ pointers don''t know anything about what they point to.
quote:is there some way to find out whether the array was dynamically created or not?
There''s no portable way, but a hacky way that depends on how the stack/heap works on your OS. You don''t want to take that route. You could have a "transfer ownership"-flag in your SafeArray, which tells if the SafeArray owns the pointer, or the code that uses SafeArray. Depending on that, SafeArray would choose whether to delete the pointer or not. You''d have to set this flag manually.
{ int darray[5]; SafeArray sad; sad.SetArray(darray, 5, SafeArray::DONT_TRANSFER_OWNERSHIP); SafeArray sad2;
int* darray2 = new int[5]; sad2.SetArray(darray2, 5, SafeArray::TRANSFER_OWNERSHIP); }
quote:Original post by aboeing hi, im just wondering if there is some way to do ''safe'' arrays in c++? specifically i want them to delete themselves after they are not used, and also dont want to allow access to out of range items, but i also want them to be as identical as possible to the standard array usage..
You mean, like the classes in the <vector> and <valarray> headers?
which is less complex than the non-stl test program i compiled.
also, i think the safearray has a more-familiar syntax as you dont really need to know anything (much) about templates.. but thanks for your suggestion anyway!
quote:Original post by aboeing using stl: -rw-r--r-- 1 boein-aj student 317772 May 20 21:39 a.out not using stl: -rw-r--r-- 1 boein-aj student 131228 May 20 21:40 a.out
Wow. I tried building that same STL program with g++ 3.2 on Linux, and ended up with a 4092 byte executable:
g++ -O3 -o test test.cpp strip test
EDIT: In other words, perhaps you need a better compiler or STL implementation.
Update to gcc 3.2.x if you can, 2.95 did not perform nearly as many optimizations as 3.x does.
Also, check that you are using -O (or O1,O2,O3) when compiling. You can also add -DNDEBUG to remove assertion code. -O3 will probably make the exe bigger, so don''t use that if size is your concern (it turns on loop-unrolling).
quote:Original post by petewood how much did it bloat your executable?
Think exception handling, RTTI, and the abstraction penalty which is significant on non-optimizing compilers. ''course, we''re adding the abstraction penalty right back it with the safe array (perhaps more of it).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform.
You can accept analytics or continue with necessary cookies only.
Learn more