473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing NULL to atoi() Function results in Segmentation Fault

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char* x = NULL;
  4.     int i;
  5.     i = atoi(x);
  6.     printf("i = %d\n",i);
  7.     return 0;
  8. }
This will results in Segmentation Fault.

when we pass NULL to atoi(or any functions like this) it is giving segmentation fault. My question is why they didnt check for NULL case( which should be given first priority) in such inbuilt implementations.
Jan 23 '13 #1

✓ answered by Banfa

Because the specification states that you will pass in a valid pointer to a string. Given that specification the check is just superfluous processing and in a language that is designed for portability and speed that is undesired.

The problem is them but rather the code that calls the function with data that is outside of the functions stated specification.

If there is a chance that the pointer is NULL the calling code should be checking for it.

2 15990
Banfa
9,065 Expert Mod 8TB
Because the specification states that you will pass in a valid pointer to a string. Given that specification the check is just superfluous processing and in a language that is designed for portability and speed that is undesired.

The problem is them but rather the code that calls the function with data that is outside of the functions stated specification.

If there is a chance that the pointer is NULL the calling code should be checking for it.
Jan 23 '13 #2
syntax of atoi() is as follow

int atoi ( const char * str );

Because NULL is not Type Safe its giving you segmentation fault

Expand|Select|Wrap|Line Numbers
  1. // Sample program to explain atoi()
  2.         #include<stdio.h>
  3.     #include<stdlib.h>
  4.  
  5.     int main ()
  6.     {
  7.         int i;
  8.         char buffer [256];
  9.  
  10.         printf ("Enter a number: ");
  11.         fgets (buffer, 256, stdin);
  12.  
  13.         i = atoi (buffer);
  14.         printf ("The value entered is %d.",i);
  15.  
  16.         return 0;
  17.     }
  18.  
Jan 24 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
10
by: Peter Dragun | last post by:
I am generally new to programming under Unix, but know how to code under Windows. I have to create a simple program, that takes the following information from a file using redirection: 4 4 4 4 4...
13
by: N.S. du Toit | last post by:
Just having a bit of trouble programming with C under FreeBSD 5.1 using the gcc compiler. I'm a bit new to C so my apologies if the answer to my question appear obvious :) Basically I've...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
2
by: luqman | last post by:
I have defined custom binding in DetailView Textbox. <%# ShowParent(Eval("ParentID")) %> Its calling a ShowParent() function which is as under: The problem is that if ParentID field is...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.