I have a function that allocates memory and then returns the pointer to that memory address. I'm allocating an array of char, the argument that they pass into the function will be the array size. Now the problem is that if they pass 0 as the argument for the size to dynamically allocate, then I want to exit/return out of that function, but the function itself returns a char * so I am not able to do something like return -1;, how would I go around this, while at the same time keeping the function and doing logic to allocate the memory in there? Is there a way?
char *alloate_memory(int x) { // This will assume you are allocating memory for a C-style string
if (x == 0)
return ;
char *mem{ new char[x] };
return mem;
}