I need multiple multi-dimensional arrays to be dynamically allocated in a function. The required size is determined inside the function and their values are then assigned there too. My issue is that i am not able to access the assigned array values outside the function. Running the code below I get EXC_BAD_ACCESS
// returns the size (required; the caller needs to know this)
std::size_t ini_arrays( int*& a1, int*& a2 ) // pointers are passed by reference
{
std::size_t size = 4 ; // determined inside the function
a1 = newint[size] {} ;
try
{
a2 = newint[size] {} ;
return size ;
}
catch( ... )
{
// first, clean up the mess
delete[] a1 ;
a1 = a2 = nullptr ;
throw ; // then rethrow the original exception
}
}
> and why the curly brackets are necessary?
They are not necessary; but using them may be a good idea (initialize all the values to zeroes).
1 2 3
int* a = nw int[4] ; // no initialisation is performed
int* b = newint[4] {} ; // empty initializer: value initialisation (zero initialisation).
int* c = newint[4] { 1, 2, 3, 4 } ; // list initialisation (aggregate initialisation)