Some hints:
1. See solution 1 and 2 about the two memory related errors.
2. You allocate memory for just one integer with the statement
int*p = new int(100);
The round paranthesis indicate that the value passed is used as an initializer, i. e. the int you allocated will be initialized with the value 100. Was that your intention? If you wanted to allocate an array of 100 integers, then you must use square brackets, like this:
int* p = new int[100];
If that was indeed your intention, in order to deallocate p properly, you must use
delete [] p;
3. It is generally good design to deallocate memory at the 'same location' where the allocation occurs. In case of classes, this can be anywhere in the class (usually a dedicated cleanup function), but in case of global functions it should preferably be inside the very same function. The main reason is exception safety: if anything goes amiss in your function, or in a function it calls, and it throws an exception, then no other function will ever know about the memory you allocated and failed to deallocate - therefore you must are responsible to deallocate the memory in case an exception occurs after successfully allocating it.
4. p is just a pointer and holds no size information, so I don't know how you intend to divine the 'Number of Elements' in your lib function from it. Not to mention that you access p
after freeing it, so if this text refers to the number originally stored in p, trying to access that number will result in a runtime error!
5. A little pet-peeve of mine: don't use 'magic numbers', i. e. integer constants with arbitrary values. I realize this was just a small example code, but as a rule of thumb, it's always a good idea to e. g. store array sizes in a variable, or, prefarably, a constant, in case this value is needed again.
Example code:
const int ARRAY_SIZE = 100;
int main(...) {
int result(0);
int* p = new int[ARRAY_SIZE];
try {
fn1(p, ARRAY_SIZE);
delete [] p;
}
catch(...) {
delete [] p;
result = 1;
}
return result;
}
void fn1(int* p, size_t size) {
puts("This is lib func");
}
Ok, I admit that making this excepton safe is
way beyond your question, but I thought I added it as a demonstration on why you should deallocate memory in the same function (or class) where you allocate it.