LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2011, 09:52 PM   #1
youxi600
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Rep: Reputation: Disabled
Smile return -1 of main but get exit code is 255? why? pls advise.


I learn C++ reading the <C++ primer 4th>, question1.2 let me test if return -1 how the compiler handle this exit code.
and in gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52), I get this show:

[youxi600@Arthas C++ Primer]$ cat q1_2.cpp
int main()
{
return -1;
}
[youxi600@Arthas C++ Primer]$ gcc q1_2.cpp -o q1_2
[youxi600@Arthas C++ Primer]$ ./q1_2
[youxi600@Arthas C++ Primer]$ echo $?
255
[youxi600@Arthas C++ Primer]$
anyone can give me an advise? Many thanks!
 
Old 06-28-2011, 10:02 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Because 255 == 0xff == -1 for an 8-bit signed value
 
1 members found this post helpful.
Old 06-28-2011, 10:12 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Yes, the exit status is effectively an unsigned char. The relevant definition in /usr/include/bits/waitstatus.h is:

Code:
#define	__WEXITSTATUS(status)	(((status) & 0xff00) >> 8)
 
1 members found this post helpful.
Old 06-28-2011, 10:20 PM   #4
youxi600
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by paulsm4 View Post
Because 255 == 0xff == -1 for an 8-bit signed value
But my main function return type is "int" not an 8-bit signed. Confused!
 
Old 06-28-2011, 10:21 PM   #5
youxi600
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
Yes, the exit status is effectively an unsigned char. The relevant definition in /usr/include/bits/waitstatus.h is:

Code:
#define	__WEXITSTATUS(status)	(((status) & 0xff00) >> 8)
why? I have assume it's return type to "int".
 
Old 06-28-2011, 11:14 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I will defer to paulsm4 for the C / C++ side as he is very experienced in these matters, but I think maybe you might be mixing apples and oranges.
Irrelevant of what is occurring within your code, the exit status in the shell can ONLY be between 0 and 255. So if you create a one line bash script:
Code:
#!/bin/bash

exit -1
This will give you the same result when looking at the exit code using $?. For that matter if you change it to -2 it will be 254 as it treats it like
a loop once past zero.

I am sure there is official information that others can give, but I just wanted to point out that the shell is the one here doing the translation and not your program
(even though it may be as well)
 
1 members found this post helpful.
Old 06-29-2011, 01:16 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Contrary to appearances, 'int' is not really a basic type; more accurately 'int' defaults to come combination of 8, 16, 32, 64 (or larger multiples of 2) bit, signed or unsigned.
Check your compiler manual & eg http://www.dummies.com/how-to/conten...bers-in-c.html

Last edited by chrism01; 06-29-2011 at 01:18 AM.
 
Old 06-29-2011, 01:35 AM   #8
youxi600
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So I will defer to paulsm4 for the C / C++ side as he is very experienced in these matters, but I think maybe you might be mixing apples and oranges.
Irrelevant of what is occurring within your code, the exit status in the shell can ONLY be between 0 and 255. So if you create a one line bash script:
Code:
#!/bin/bash

exit -1
This will give you the same result when looking at the exit code using $?. For that matter if you change it to -2 it will be 254 as it treats it like
a loop once past zero.

I am sure there is official information that others can give, but I just wanted to point out that the shell is the one here doing the translation and not your program
(even though it may be as well)
Thanks.
I think I can get your means. No matter which type the main returned, the "echo $?" shell command just ONLY get a unsigned 8-bit exit code.
And I find one book may be help explain this viewpoint.

The New Kornshell Command and Programming Language by Morris Bolsky and David Korn says:
Quote:
0 Normal exit.
1-125 Failure.
126 A command was found but cannot be executed.
127 A command could not be found.
128-255 Failure.
256 and above A command has exited because of reciept of a signal.
Version: With the 11/16/88 version of ksh, 129-160 indicated that a command had exited because of a signal....A command that could not be found or could not execute had a return value of 1.
 
Old 06-29-2011, 01:38 AM   #9
youxi600
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Wink

Quote:
Originally Posted by chrism01 View Post
Contrary to appearances, 'int' is not really a basic type; more accurately 'int' defaults to come combination of 8, 16, 32, 64 (or larger multiples of 2) bit, signed or unsigned.
Check your compiler manual & eg http://www.dummies.com/how-to/conten...bers-in-c.html
Thanks. I will check it.
 
Old 06-29-2011, 07:55 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Actually, the unsigned 8-bit exit code limitation is part of the kernel, not the shell.

Last edited by MTK358; 06-29-2011 at 10:56 AM.
 
Old 06-29-2011, 10:49 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The only access to that "int" from main() is the value that is returned when the parent performs a wait() system call, which returns a 16-bit quantity with two 8-bit fields. One field is the low-order 8 bits of the exit status; the other field is the terminating signal. That's all that is available to any program.
 
1 members found this post helpful.
Old 06-29-2011, 10:57 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by rknichols View Post
The only access to that "int" from main() is the value that is returned when the parent performs a wait() system call, which returns a 16-bit quantity with two 8-bit fields. One field is the low-order 8 bits of the exit status; the other field is the terminating signal. That's all that is available to any program.
Does that mean that my above post is wrong?

Does the other field with the terminating signal have anything to do with main()'s return value?
 
Old 06-29-2011, 07:41 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I believe it's a bit of both.
Inside C, 'int' could be almost anything (see my post #7) if its setup to do so, but once you pass the value back to the shell, it can only represent/present an 8 bit value. Not sure if that's the shell or the kernel.
If it was the kernel, in theory it should be able to use anything up to the bit size of the kernel eg 32 or 64.
I suspect( and could be wrong) that it's a shell limit.

Answers on a postcard pls ...
 
Old 06-30-2011, 02:14 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by MTK358 View Post
Does that mean that my above post is wrong?

Does the other field with the terminating signal have anything to do with main()'s return value?
No, it's just a signal number, and would be zero for a process that exited normally.
Code:
#define __WTERMSIG(status)      ((status) & 0x7f)
#define __WIFEXITED(status)     (__WTERMSIG(status) == 0)
It appears that the kernel might actually store the whole integer within the task_struct structure. It's just that the wait() system call returns only the low-order 8 bits of the exit code plus an 8-bit signal number all packed into the low order 16 bits of an integer. The macros for testing and extracting those fields are described in the wait(2) manpage and defined in /usr/include/bits/waitstatus.h.

Looking at that manpage, I see a slightly newer system call, waitid(), that looks like it might return the entire integer in the si_status member of the siginfo_t structure. For a C (or C++) program, it might be worth taking a look at that if you really need to see more than those low 8 bits of the exit status. I've never used waitid(2), so I'm not sure what is really returned.

Last edited by rknichols; 06-30-2011 at 02:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Single Command To Close Window and Return Exit Code jagooch Programming 3 01-02-2009 03:33 AM
What is this -> SRC=0.0.0.0 DST=255.255.255.255 LEN=328 TOS=0x00 PREC=0x00 TTL=128 ID carves Linux - Networking 5 08-17-2008 09:26 PM
Logs full of hits to 255.255.255.255; how to stop logging? mac_phil Mandriva 2 02-23-2004 10:25 AM
Return code from main() using gcc Meatwad Programming 13 01-27-2004 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration