Question

LANGUAGE: C

Below we are dissecting the structure of atoi and its pitfalls using invalid calls

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 : -nj

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 conceptually understand why these calls are invalid. We already know they are invalid.

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" will cause the program to crash (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?)? If not, please tell me why this program crashes when passed "num" but "&num" doesn't crash the program.

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?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

atoi (e num cai (num pe mitmatch ond teuoux au ead Ag -the output 4 2,rhe -tece Output ù 2 with ASC it Value ttable and astaa

1o0 ひん os DDstive , The tbloudiq ㅨ tee lgdeゃ, n aladees as PoG.tve. it n , eo, (ose Case you both Cod, itve tu hame sslt

Add a comment
Know the answer?
Add Answer to:
LANGUAGE: C Below we are dissecting the structure of atoi and its pitfalls using invalid calls...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • Ensure the following compiles 5. Variable scope (1 mark) Some variables are only accessible while executing...

    Ensure the following compiles 5. Variable scope (1 mark) Some variables are only accessible while executing specific code. Global variables are often thought of as evil because the state of a system can be altered making functions execute differently when it isn't expected. There is also the issue of block scope. For an example of block scope, see the file below. Notice that you don't get an error because the code uses the correct syntax. Although the syntax is correct,...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Need this in C Code is given below e Dots l lah dit Problem 1. (30...

    Need this in C Code is given below e Dots l lah dit Problem 1. (30 points) Fre bendord.cto obtain the free in decimal representation For ATY. this problem we complete the code in i tive long inte ens (W written the following positive long term 123, 40, 56, 7, 8, 9, 90, 900 the frequencies of all the digits are: 0:4, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7: 1. 8: 1.9: 3 In this example, the free ency of...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT