- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Segmentation error with c program
Operating System - Linux
1752218
Members
10958
Online
108785
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2006 11:20 PM
10-21-2006 11:20 PM
Hi guys,
I a writing a c program. but whenever try to run the file without arguments it will give segmentation. How can I catch this error before it terminate the progam. Thanks.
Best regards
Henry
I a writing a c program. but whenever try to run the file without arguments it will give segmentation. How can I catch this error before it terminate the progam. Thanks.
Best regards
Henry
Solved! Go to Solution.
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2006 04:58 AM
10-22-2006 04:58 AM
Solution
"Segmentation violation" means your program is trying to access memory outside the areas reserved to it. With C programs, this usually means you've made some sort of error with pointer variables.
You should check the number of command line arguments (int argc) before trying to read the argument strings (char **argv).
The argv is an 1-dimensional array of pointers to strings (1-dimensional arrays of characters): its type can be expressed equivalently as "char **argv", "char *argv[]" or "char argv[][]". I wouldn't recommend the last form though, because it easily leads you to think in the wrong way: argv definitely isn't a two-dimensional array, although it may seem somewhat like it.
Normally, when you run a program without arguments, argc is usually 1. It means you'll have the name that was used to start the program in *argv[0]. If argc is equal to 1, you must not try to access *argv[1], because argv[1] is either NULL or random nonsense and trying to dereference either of those will produce the error message you're getting.
In some special situations, the program can be started with argc equal to 0. Never just assume that you can read *argv[0] without checking the value of argc first.
You should check the number of command line arguments (int argc) before trying to read the argument strings (char **argv).
The argv is an 1-dimensional array of pointers to strings (1-dimensional arrays of characters): its type can be expressed equivalently as "char **argv", "char *argv[]" or "char argv[][]". I wouldn't recommend the last form though, because it easily leads you to think in the wrong way: argv definitely isn't a two-dimensional array, although it may seem somewhat like it.
Normally, when you run a program without arguments, argc is usually 1. It means you'll have the name that was used to start the program in *argv[0]. If argc is equal to 1, you must not try to access *argv[1], because argv[1] is either NULL or random nonsense and trying to dereference either of those will produce the error message you're getting.
In some special situations, the program can be started with argc equal to 0. Never just assume that you can read *argv[0] without checking the value of argc first.
MK
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP