In order: Yes it would (with risks), and no it won't.
For the first, you need to remember (as always) how many elements are in the array so you don't run off the end. One each way might be to use a structure:
typedef struct {
int len;
int* contents;
} FiniteArray;
Then make int** arrays
actually be an array of FiniteArray
structures, itself a FiniteArray*
.
If you're 100% set on using arrays, you still can, just keep lengths around at all times. Don't forget, of course: you need the number of elements in arrays
too!
For the second part with delete[]
, that will delete your array of arrays but will leave the rest of the memory allocated!
You are correct in that, when you use new
for an array, you use delete[]
. Otherwise, for objects, use delete
, etc etc. See this question. Yes, I know it's a duplicate, but I thought it explains this better than what it's marked duplicate of.
Anyway, you will need to first deallocate the individual arrays if you want to delete arrays
. In your case,
delete[] arrays[0];
delete[] arrays[1];
delete[] arrays;
arrays = NULL; // always a good idea!
Just to delete one array, say, the first one,
delete[] arrays[0];
arrays[0] = NULL; // always a good idea!