ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You probally forgot to return 0 at the end of your main function
Code:
#include <stdio.h>
int main()
{
short a ;
scanf("%d",&a);
printf("\n a= %d\n",a);
return 0; //Forgot to do this
}
Also if that still does not fix the problem you may want to use %h instead of %d, because %h is the flag for short. The bus error may be occuring cause it is trying to read and save a 4 byte integer into a 2 byte space and isnt converting right.
Last edited by stumpster123; 07-14-2005 at 11:33 AM.
void main()
{
short a;
scanf("%d",&a);
printf("\n a= %d",a);
}
also works for me running Fedora Core 3 and gcc 3.4.3. Your post stated that you were using a 64 bit SunOs box. I wonder if the c library, or the gcc compiler for that port has a problem in either the scanf or the printf support functions when dealing with misaligned data types. For grins you could try either one (or both) of the following tests:
Code:
Test 1:
void main()
{
int a;
scanf("%d", &a);
printf("\n a = %d\n", a);
}
Test 2:
void main()
{
short a;
scanf("%hd", &a);
printf("\n a = %hd\n", a);
}
I would be interested in hearing of your results if you run those tests.
rstewart, regarding your queries both of your suggested changes work fine.
I was just wondering why the misaligned data types are giving bus error.
I have a doubt with stumpster's suggestion:
"The bus error may be occuring cause it is trying to read and save a 4 byte integer into a 2 byte space and isnt converting right."
c doesn't check for array bounds so it could have safely written over the 4 bytes starting from the address &a - then why it is giving this bus error ?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Code:
short a ;
scanf("%d",&a);
is definitely buggy.
It should be either "int a;" or "scanf("%h",&a);"
Quote:
c doesn't check for array bounds so it could have safely written over the 4 bytes starting from the address &a - then why it is giving this bus error ?
You cannot write "safely" 4 bytes where only 2 are expected.
- first you are writing to undefined space
- alignment may matter depending on the architecture, what is yours ?
>You cannot write "safely" 4 bytes where only 2 are expected.
> - first you are writing to undefined space
R: So, at worst I should have got a segmentation fault, bt why SIGBus?
> - alignment may matter depending on the architecture, what is yours ?
R: mine is a 64 bit SunOs
>You cannot write "safely" 4 bytes where only 2 are expected.
> - first you are writing to undefined space
R: So, at worst I should have got a segmentation fault, bt why SIGBus?
> - alignment may matter depending on the architecture, what is yours ?
R: mine is a 64 bit SunOs
What CPU is running the 64 bit SunOS?
Alignment does matter depending upon the CPU and how misaligned data is handled. I remember that either the old Motorola 68030s or 68040s would throw a bus error exception if you tried to do a 32 bit data access and the memory address did not start on a "long word" boundary. My guess is that you are seeing the same sort of exception handling. I think that it is probably the scanf that was throwing the exception and not the printf, you could go back to your original code, remove the printf and try a test with just the scanf. You could also go back to your original code, compile it and have the compiler preserve the assembly source. Then recompile the code that works, having the compiler save the assembly code, and take a look at both versions of the assembly code to try and identify what the CPU is really trying to execute. Then take a look at the CPU's assembly code programmers manual and look up wha each of the instructions are supposed to do and what kind of exceptions they can throw. (Not sure if you want to take those steps, but it could go a long way to helping you understand the hardware architecture a little better )
I used gdb to find out the addresses being allocated to a "short int" and an "int".
I saw that a short int is allocated memory at 2 byte boundary, and an int is allocated memory at 4-byte boundary.
some post I came across in the net said that to read a n-byte entity the starting address should be at n-byte boundary.
till now I have assumed the above statement to be true.
Okay, that makes perfect sense. In your original failure case you had allocated a short int (thus being allocated on a 2 byte boundary), however your scanf was attempting to access an int (I presume that your ints are either going to be 4 or possibly even 8 bytes, but are definitely not 2 bytes) therefore that access should either require a 4 or an 8 byte address boundary. Since your data was defined using a 2 byte boundary, and your access to that memory was presuming a 4 or 8 byte boundary - bingo bus error exception.
Once you switched to either of the two examples I suggested, your access alignment matched your definition alignment and everything was okay.
I have programmed the 32 bit Sun Sparc CPUs, they are "quirky" when it comes to alignment problems. I think that the 64 bit versions are probably just as quirky if not more so.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.