473,467 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A Case of BUS Error !

Hi everybody ! I have this (very) simple program written in C which when
run, gives me a "Bus Error". I did a lot of googling around on "Bus
Error" every where it was all about misaligned integer/double access or
accessing inexistent memory devices. But neither case matches my code.
Can any one please help me find the very-obvious thing I have missed ?

Code
----

unsigned char *x = "text";
int main(){
x[2] = 's';
}

Heres the gdb output
--------------------

GNU gdb 5.2.1 (FreeBSD)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-undermydesk-freebsd"...
Core was generated by `x'.
Program terminated with signal 10, Bus error.
Reading symbols from /usr/lib/libc.so.5...done.
Loaded symbols for /usr/lib/libc.so.5
Reading symbols from /usr/libexec/ld-elf.so.1...done.
Loaded symbols for /usr/libexec/ld-elf.so.1
#0 0x080484b0 in main () at test.c:4
4 x[2] = 's';
(gdb) run
Starting program: /usr/home/vivek/projects/haxle/core/x

Program received signal SIGBUS, Bus error.
0x080484b0 in main () at test.c:4
4 x[2] = 's';
(gdb) bt
#0 0x080484b0 in main () at test.c:4
#1 0x080483e5 in _start ()

Thank You
Vivek

Nov 13 '05 #1
4 12483
Thank you for your reply. But forgive me for my newbie questions :)
because if you are right I think I've messed up my concepts somewhere.

But from what I understand its only a variable you can declare a
constant right ? Can the memory contents such as the strings in this
case be constant ?
x points to a text string which is constant.

you try to change the constant -> kaboom

Moreover, if thats the case then how is it that when I used "unsigned
char x[] = ..." the program works perfectly. Also what I get is a BUS
Error, what does that mean, exactly, in this context.

Thank you again,
Vivek


Jörgen


Nov 13 '05 #2


Vivek Mohan wrote:
Thank you for your reply. But forgive me for my newbie questions :)
because if you are right I think I've messed up my concepts somewhere.

But from what I understand its only a variable you can declare a
constant right ? Can the memory contents such as the strings in this
case be constant ? When u say,
unsigned char *x = malloc(10);
You are allocating 10 bytes of memory for x.
When u say,
strcpy(x, "text");
You are copying some text ("text" in this case) starting
from the memory location pointed to by the variable x.
As you have already allocated enough memory to hold this
string and also because 10 bytes starting from the address
pointed to by the variable x is allocated for this process
on the heap, there is no error or mistake in the pair of
operations.

read on.....
x points to a text string which is constant.
you try to change the constant -> kaboom
>


Moreover, if thats the case then how is it that when I used "unsigned
char x[] = ..." the program works perfectly. Also what I get is a BUS
Error, what does that mean, exactly, in this context.

But, when you say
unsigned char *x[] = "text";
In this case the string "text", is a compile time constant string,
i.e, the contents of the actual string that is going to be present
inside the variable at run-time is already known in its entirity at
compile-time itself. So the compiler has the right to implement this as
it feels comfortable and efficient with.
The C compiler stores this string in a read-only part of
the process. This means, the variable x will be made to point
to this memory location that is deemed a read-only memory area
by the compiler. But, when you try to change the memory contents
inside this read-only memory location, there is a bus error!

Thank you again,
Vivek


Jörgen



Bye,
../Chaitanya Atreya

Nov 13 '05 #3
Simon Biber wrote:
"Atreya, Chaitanya" <at****@india.hp.com> wrote:
But, when you say
unsigned char *x[] = "text";

Are you sure you want an array of pointers here? If so, the
initialiser should have braces around it. Also, you can't
convert from (char *) to (unsigned char *) without a cast,
they are incompatible types.


and the case when it works fine is

unsigned char x[] = "text";

Vivek

Nov 13 '05 #4


Simon Biber wrote:
"Atreya, Chaitanya" <at****@india.hp.com> wrote:
But, when you say
unsigned char *x[] = "text";

Thank you, simon, for the correction.

Obsivously, I meant to say:
unsigned char *x = "text";

Thank you,
../Chaitanya Atreya


Are you sure you want an array of pointers here? If so, the
initialiser should have braces around it. Also, you can't
convert from (char *) to (unsigned char *) without a cast,
they are incompatible types.


Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave | last post by:
I am getting a syntax error on Select Case Now Case > "2/2/2004 12:30:00 AM" response.redirect "votingover.asp" case else m=0 End Select
9
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am...
5
by: Ryan | last post by:
I'm struggling with a Case statement. The problem I has is with doing >= I can use any value in there, but need to check if it's greater or equal to 1. I'm sure I'm missing something but can't...
29
by: SysSpider | last post by:
Hi again, This is my problem: when i try to compile the code that contains the function below, i get this: -- gcc:21: error: case label does not reduce to an integer constant gcc:24: error:...
7
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
11
by: quakewang | last post by:
hi, I have define in a head file like this: #define GLUT_BITMAP_9_BY_15 ((void*)2) #define GLUT_BITMAP_8_BY_13 ((void*)3) #define GLUT_BITMAP_TIMES_ROMAN_10 ...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
4
by: makinha | last post by:
Hello, I am getting a syntax error Microsoft VBScript compilation error '800a0400' Expected statement line 49 Case 164 To 269 ---------------^ (The error references...
4
by: clairelee0322 | last post by:
My program has an error C2360: initialization of 'i' is skipped by 'case' label. I don't know why. Please help me out! thks!! the error occurs in Division case 3. //Claire's Calculator...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.