C Error Return Values
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers c error handling best practices or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Error Handling In C++
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it c error codes only takes a minute: Sign up What is the best way to return an error from a function when I'm already returning a value? up vote 12 down vote favorite 6 I wrote a function in C that converts
Error.h C
a string to an integer and returns the integer. When I call the function I also want it to let me know if the string is not a valid number. In the past I returned -1 when this error occurred, because I didn't need to convert strings to negative numbers. But now I want it to convert strings to negative numbers, so what is the best way to report the error? In case I wasn't clear about this: I don't return error c++ want this function to report the error to the user, I want it to report the error to the code that called the function. ("Report" might be the wrong word to use...) Here's the code: s32 intval(const char *string) { bool negative = false; u32 current_char = 0; if (string[0] == '-') { negative = true; current_char = 1; } s32 num = 0; while (string[current_char]) { if (string[current_char] < '0' || string[current_char] > '9') { // Return an error here.. but how? } num *= 10; num += string[current_char] - '0'; current_char++; } if (negative) { num = -num; } return num; } c return share|improve this question asked Nov 15 '08 at 0:04 Jeremy Ruten 93.3k26133172 Heh. No multiple-value-bind in C :) –Svante Nov 15 '08 at 0:44 add a comment| 7 Answers 7 active oldest votes up vote 21 down vote There are several ways. All have their pluses and minuses. Have the function return an error code and pass in a pointer to a location to return the result. The nice thing about this there's no overloading of the result. The bad thing is that you can't use the real result of the function directly in an expression. Evan Teran suggested a variation of this that has the caller pass a pointer to a success variable (which can be optionally NULL if the caller doesn't care) and returns the actual value f
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
C Exit With Error Message
workings and policies of this site About Us Learn more about Stack
Types Of Error In C
Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users c stderr Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes http://stackoverflow.com/questions/291828/what-is-the-best-way-to-return-an-error-from-a-function-when-im-already-returni a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Should I always return an error code from C functions? up vote 5 down vote favorite 1 I have the following code in many places in a large application: if (datastruct.data_ok http://programmers.stackexchange.com/questions/276520/should-i-always-return-an-error-code-from-c-functions && cur_time > datastruct.start_time && cur_time < datastruct.end_time) { do_something(); } Now, I wanted to eliminate repeated code by defining a function time_ok(...) which would simplify the repeated code to: if (time_ok(&datastruct, cur_time)) { do_something(); } In my opinion, this is a well-made refactoring and should be therefore done. If the definition of the condition changes in the future, code maintenance becomes easier as only one location needs to be changed. However, several team members noticed that the function can be abused by giving it a NULL argument, which crashes the program. They also complained that the return value is a boolean and not an error code, and all functions should according to them return an error code. So, he refactoring would according to them be done in the following way: bool time_ok_condition; int err; err = time_ok(&datastruct, &time_ok_condition, cur_time); if (err != 0) { fprintf(stderr, "Error %d at file %s line %d", err, __FILE__, __LINE__); exit(1); } if (time_ok_condition) { do_something(); } However, in my opinion this results in excessively long code and
of a library call. The functions strerror and perror give you the standard error message for a given error code; the variable program_invocation_short_name gives you convenient access to the name of the program http://www.gnu.org/s/libc/manual/html_node/Error-Messages.html that encountered the error. Function: char * strerror (int errnum) Preliminary: | MT-Unsafe race:strerror | AS-Unsafe heap i18n | AC-Unsafe mem | See POSIX Safety Concepts. The strerror function maps the error code (see Checking for Errors) specified by the errnum argument to a descriptive error message string. The return value is a pointer to this string. The value errnum normally comes from the variable errno. You should not c error modify the string returned by strerror. Also, if you make subsequent calls to strerror, the string might be overwritten. (But it’s guaranteed that no library function ever calls strerror behind your back.) The function strerror is declared in string.h. Function: char * strerror_r (int errnum, char *buf, size_t n) Preliminary: | MT-Safe | AS-Unsafe i18n | AC-Unsafe | See POSIX Safety Concepts. The strerror_r function works like strerror but instead c error return of returning the error message in a statically allocated buffer shared by all threads in the process, it returns a private copy for the thread. This might be either some permanent global data or a message string in the user supplied buffer starting at buf with the length of n bytes. At most n characters are written (including the NUL byte) so it is up to the user to select a buffer large enough. This function should always be used in multi-threaded programs since there is no way to guarantee the string returned by strerror really belongs to the last call of the current thread. The function strerror_r is a GNU extension and it is declared in string.h. Function: void perror (const char *message) Preliminary: | MT-Safe race:stderr | AS-Unsafe corrupt i18n heap lock | AC-Unsafe corrupt lock mem fd | See POSIX Safety Concepts. This function prints an error message to the stream stderr; see Standard Streams. The orientation of stderr is not changed. If you call perror with a message that is either a null pointer or an empty string, perror just prints the error message corresponding to errno, adding a trailing newline. If you supply a non-null message argument, then perror prefixes its o