LANGUAGE: C Below we are dissecting the structure of atoi and its pitfalls using
ID: 3878569 • Letter: L
Question
LANGUAGE: C
Below we are dissecting the structure of atoi and its pitfalls using invalid calls
Below are invalid calls to atoi
Please answer the following questions just in the context/frame provided based on your understanding of how this function works. Yes there are warnings when you run this code, but I am just trying to understand the return values.
a)
&num returns 2, I am aware that 'num' -'0' has a value of 2 but not why isdigit fails to recognize that & is not a digit, and moreover why it fails to recognize that n, u, or m isn't a digit. Is it because isdigit accepts an int as a parameter so we are casting that value? Then again, why didn't isdigit stop at &? isdigit then seems like a fairly counterintuitive function to use, but I still need light shed on this call.
b)
On a somewhat similar note, num causese the program to crash "Program terminated with signal SIGSEGV, Segmentation fault." Is this because we are attempting to access the memory address at the value "num" (or are we attempting to access the memory address at 50?) since we are dealing with a pointer in atoi? If not, please tell me why this program crashes when passed num.
C)
-2147483649 is the equivalent of INT_MIN -1, and so when passed in it overflows and returns INT_MAX because we are storing n into type int as we convert it, this makes sense. Why, then, is there a comment in the code saying we calculate it as a negative value to prevent INT_MIN overflow, even though INT_MIN overflow appears to be exactly what happened? Please explain why we calculated it as a negative AND why it overflowed if we have this supposed safety mechanism in place? What then is the safety mechanism for?
int atoi(const char *s) int n-e, neg-0; while (isspace(*s)) s++; switch (*s) ( case -': neg-1; case s++; * Compute n as a negative number to avoid overflow on INT_MIN while (isdigit(*s)) n 10*n (*s++ '0' ); - - return neg ? n : -njExplanation / Answer
Explaination:
int num =50;
atoi(&num);
atoi(num);
atoi("-2147483669");
a) Actually atoi(const char* n) accepts only strings (i.e char*), but when you use atoi(&num), parameter sent is int* which is a type mismatch and should throw an error. As you mentioned that you got output as 2, your compiler automatically typecasted (int*) to (char*) as int and char are interconvertible(valid till 0-255(ASCII values)).So you are getting output 2 which has ASCII value of 50. Coming to your main question, when you use atoi(&num), its the address( of variable) that goes into the function but not the variable name.All the variables are stored in sybmol table (data structure used to store variables and symbols during compilation and execution process) and either address of variable or value is used accordingly.So neither of "&","n","u","m" are sent to function atoi.
b) Similarly as explained above, you cannot send (int) instead of (char*). In recent compilers, it throws an error of type mismatch, but your program crashed because (int) and (char*) didnt match and thus without throwing error, the program started using variable s(char*). As it is not pointing to any memory value, performing pointer arthematics(s++,s--,etc) gives very bad results(SIGSEGV, Segmentation fault). So always remember when parameters are sent to function, the parameter value/reference only goes but not the parameter name;
c) Actually there is no logical reason why n is calculated negative. As you have already encountered, there is no safety mechanism involved by doing this way. Even though n calculated as positive, it results in same output.Here is code for n calcuated in positive. If you run both of these codes, it gives the same result.
CODE:
int atoi(const char* s){
int n=0, neg = 0;
while(isspace(*s)) s++;
switch(*s){
case '-':neg = 1;
case '+':s++;
}
while(isdigit(*s)) n = 10*n + (*s++ - '0');
return neg ? -n : n;
}
If you have any other doubts regarding the above answers, Please comment and let me know so that i can help.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.