I'm developing a C++ DLL that allocate an array for the main application. The function return an error code and not the pointer to the new created array, so the address of the first member will be written in a parameter of the function. Example:
int foo(int** arrayPtr) {
int* array = new int[10];
*arrayPtr = array;
return 0;
}
So, in the main I call the function that way:
int* myArray;
int ret;
ret = foo(&myArray);
Now myArray points to the new created array.
QUESTION 1: Is there a better way to do this?
Than the more interesting question. If I pass NULL as parameter for foo, I generate an Access Violation exception because
*arrayPtr = array;
will try to write in 0x00000.
So, I added a try-catch block
int foo(int** arrayPtr) {
int* array = new int[10];
try {
*arrayPtr = array;
} catch(...) {
return 1;
}
return 0;
}
I expect that , when I call foo with NULL as parameter, it will return 1. Not true! It generate an exception.
QUESTION 2: Why the try-catch block in the DLL doesn't work?
Thanks to everyone!
P.S.: using try-catch for generating the same exception directly in the main doesn't generate an exception (or better, it's correctly handled by the try-catch block).