Answer the question
In order to leave comments, you need to log in
What should the NULL function return if nothing is found?
Question about pointers, or rather, NULL pointers, i.e. pointer with null content. In C, this is a fairly common thing. If the pointer is null, then after checking it for zero, a conclusion is made about the incorrectness of something. For example, if FILE* is NULL when opening a file, then the file did not open for some reason. It seems to me that null pointers are absolute evil. Because they forget to check it. But let's say we have a function that looks for something. For example, an element in an array. If it finds it, it should return a pointer to the element. This is how the function is defined: int* find (int* array, int is the target). But what should the function return if nothing is found? After all, the return value must be of type int*. That's the question. Please give a detailed answer with an explanation.
Answer the question
In order to leave comments, you need to log in
So... let's correct your understanding of the subject a little.
In C / C ++ there is such a data type as a pointer to data. Its syntax looks like this:
Writing literally means that 'value' will store the address of the data area with the supposed typing into a four-byte signed integer. Attentively! 'value' stores only a pointer, there is no data about the block size of this data, there are no strict reservations about the type of this data (only the assumption that it is 'int').
When forming a variable, it is usually not initialized in any way. That is, after defining our 'value', its value contains any unthinkable garbage. Attentively! There is no way (other than calling and catching AV/SIGSEGV) to determine that 'value' refers to the correct address. Therefore, this very garbage, which is contained in 'value', can be safely used as the address of a data block and, when accessed to this address, get dealt with by the OS.
Question! How to avoid it?
There is a very simple and very old way out - to define some kind of magic constant that could definitely symbolize the purity of the pointer (that the pointer does not contain an address). This is exactly the kind of constant 'NULL' is.
The address for the data block can be absolutely anything! It may even be 0xA0L. But if 0 is written in the pointer value (this is NULL), then the pointer is clean - it does not contain the address to the data block.
So! NULL is not evil. NULL is a sign of a clean pointer!
Now let's move on to the "subtleties and nuances of the zero"...
Try to assemble the following code:
typedef int* p_int;
p_int value = p_int();
printf( "ptr : 0x%08x\n", value );
int* value = NULL;
delete value;
If the function at the output should return a pointer to something, but this something does not exist, then NULL is returned in the general case. This is normal behavior for c/c++. There is no evil here. NULL is not a data type, so int* foo = NULL is correct. And "forget to check" is a sign of unprofessionalism.
If these checks scare you so much, you can do it a little differently. Make a function
bool find(int* array, int value, int* output).
The function returns true if something was found, or false if it wasn't. The result, if found, is placed in output. If nothing is found - Output is not touched.
If the value in the array is not found and it is impossible to continue without it, then the best option is
Above, where you call the search function, in try {} catch(...) {} you catch this exception and process it, otherwise in the code you get tired of handling your NULL or -1.
The function should return what is written in the terms of reference.
In this case, I would suggest returning -1
In general, my opinion is this: if a pointer is returned and you need to show the caller that it is not necessary to use it, the function should return a "magic constant". For example, 0, -1, 0xDEADC0DE and so on. Since a pointer is actually an address in memory, returning the "left" address when accessing it will result in an exception rather than continuing the program, which is good. There is a BUT: different operating systems allocate memory differently, somewhere the chances of the validity of the 0xDEADBEEF address are much greater than zero. Therefore, it is ideal to use 0 or -1: such addresses will be available to the program with a minimum probability.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question