Ok, I am currently writing a CustomUInt64 class, this class is just a way to represent an unsigned int of 64 bit.
My problem is that while testing some of its functionality I wrote a little test, one part of this test involved some arrays and to make everything easier I wrote some functions to modify these arrays.
In my main function I have some arrays of different types, the declaration looks like this:
CustomUInt64 U64Array[200];
UInt8 U8Array[25];
UInt16 U16Array[25];
UInt32 U32Array[25];
SInt8 S8Array[25];
SInt16 S16Array[25];
SInt32 S32Array[25];
Float32 F32Array[25];
Float64 F64Array[25];
These types are just "typedef"s
UInt8/UInt16/UInt32 = unsigned char/unsigned short/unsigned int
SInt8/SInt16/SInt32 = signed char/signed short/signed int
Float32/Float64 = float/double
As I said these are declared inside main, so no function should be able to access them without having a reference or pointer passed right? (I have no global variables) My problem is that a function which get passed U64Array and U16Array modify U8Array, when I later call the same function with the other arrays U8Array is also modified. The other arrays is also modified, but in different calls.
The function which changes U8Array is this:
template<typename T,typename T2,size_t Size>
void CopyArray(T *p_DestArray,const T2 (&p_SrcArray)[Size],UInt32 p_DestStart = 0,UInt32 p_SrcStart = 0)
{
for(int i = 0;i<Size;++i)
{
p_DestArray[i+p_DestStart] = static_cast<T>(p_SrcArray[i+p_SrcStart]);
}
}
The function is called in this code block, I have added some comment to describe when the arrays become invalid, when I say this I mean the function call on the same line, so "this changes x" means that x is a different value on the next line.:
CopyArray<UInt8,CustomUInt64> (U8Array, U64Array, 0, 0); // This changes all values of U64Array
CopyArray<UInt16,CustomUInt64> (U16Array, U64Array, 0, 25); // This changes all values of U64Array and U8Array
CopyArray<UInt32,CustomUInt64> (U32Array, U64Array, 0, 50); // This changes all values of U16Array, U64Array and U8Array
CopyArray<SInt8,CustomUInt64> (S8Array, U64Array, 0, 75); // This changes all values of U32Array, U16Array, U64Array and U8Array
CopyArray<SInt16,CustomUInt64> (S16Array, U64Array, 0, 100);
CopyArray<SInt32,CustomUInt64> (S32Array, U64Array, 0, 125);
CopyArray<Float32,CustomUInt64> (F32Array, U64Array, 0, 150);
CopyArray<Float64,CustomUInt64> (F64Array, U64Array, 0, 175);
Before these calls to CopyArray I also called them, but nothing went wrong there, those function calls looked like this:
CopyArray<CustomUInt64,UInt8>(U64Array,U8Array,0);
CopyArray<CustomUInt64,UInt16>(U64Array,U16Array,25);
CopyArray<CustomUInt64,UInt32>(U64Array,U32Array,50);
CopyArray<CustomUInt64,SInt8>(U64Array,S8Array,75);
CopyArray<CustomUInt64,SInt16>(U64Array,S16Array,100);
CopyArray<CustomUInt64,SInt32>(U64Array,S32Array,125);
CopyArray<CustomUInt64,Float32>(U64Array,F32Array,150);
CopyArray<CustomUInt64,Float64>(U64Array,F64Array,175);
So does anyone have an idea of what could have went wrong?