A Case of BUS Error !

  • Thread starter Atreya, Chaitanya
  • Start date
A

Atreya, Chaitanya

Vivek said:
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

Bye,
../Chaitanya Atreya
 
V

Vivek Mohan

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
 
V

Vivek Mohan

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
 
V

Vivek Mohan

Simon said:
Atreya said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top